dto_helpers 1.0.1 copy "dto_helpers: ^1.0.1" to clipboard
dto_helpers: ^1.0.1 copied to clipboard

Effortlessly validate Data Transfer Objects in your Dart/Flutter applications.

example/README.md

To leverage dto_helpers, declare a DTO class extending DTOValidate. Here's an example of a SignInDTO:

import 'package:dto_helpers/dto_helpers.dart';

class SignInDTO extends DTOValidate {
  late String name;
  int? signInCode;

  SignInDTO({required this.name, this.signInCode});

  // Factory constructor for JSON deserialization
  factory SignInDTO.fromJson(dynamic json) {
    return SignInDTO(name: json['name'], signInCode: json['signInCode']);
  }

  // Call this for an empty DTO
  SignInDTO.empty();

  @override
  ValidationResult validate(dynamic json) {
    return super.validateAll([
      IsString(
        value: json['name'],
        propertyName: 'name' // Provides meaningful error messages
      ),
      IsNumber(
        value: json['signInCode'],
        isOptional: true, // Signifies optional fields
        propertyName: 'signInCode'
      )
    ]);
  }
}

// Example usage:
final json = {'name': 'John Doe', 'signInCode': 12345}; // JSON from a network request
final ValidationResult validationResult = SignInDTO.empty().validate(json);
if (!validationResult.isValid) {
  throw Exception('Validation error ${validationResult.message}');
}

SignInDTO signInDTO = SignInDTO.fromJson(json);

Validators #

Each of the property validate class like IsString, IsNumber, IsBoolean, IsList and IsEnum can be used as the following also.

// each of these class has a method 'validate', which will
// compute validation result based on the options given.

final result=IsString(value: qrCode).validate();

2
likes
130
pub points
12%
popularity

Publisher

unverified uploader

Effortlessly validate Data Transfer Objects in your Dart/Flutter applications.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, validators

More

Packages that depend on dto_helpers