dto_helpers

Creator: coderz1093

Last updated:

0 purchases

dto_helpers Image
dto_helpers Images

Languages

Categories

Add to Cart

Description:

dto helpers

dto_helpers #
DTO(Data transfer objects) validators package for Dart.
Supports all platforms.
The dto_helpers package provides a robust set of validators for Data Transfer Objects (DTOs) in Dart applications, ensuring data integrity for various data types including strings, numbers, enums, booleans, and lists.
Getting Started 🚀 #
Enhance your Flutter or Dart project by adding dto_helpers to your dependency list:
dependencies:
...
dto_helpers:
copied to clipboard
Features ✨ #

String Validator: Ensure that your data meets specific string-based criteria.
List Validator: Validate list sizes and nested list elements.
Number Validator: Confirm numerical data integrity, ranges, and sign.
Enum Validator: Check that data matches one of the predefined enum values.
Boolean Validator: Verify boolean values accurately.

Usage #
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);

copied to clipboard
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();

copied to clipboard
IsString
Ensures the data is a string, with a plethora of checks such as email validation, URL checks, case sensitivity, and more for comprehensive string validation.
IsString(
value,
isOptional, // will ignore validation if value is null
isEmail,
contains,
notContains,
isUrl,
isAlpha,
isAlphanumeric,
isBase64,
isCreditCard,
isDate,
isJSON,
isLowercase,
isUppercase,
isUUID,
isIP,
isUppercase,
isIn,// String[],
maxLen,
minLen,
propertyName, // it will contruct a message whenever validation fails, if propertyName is passed
)
copied to clipboard
IsNumber
Ensures numerical data is within a specific range and meets criteria like being positive or divisible by a specific number.
IsNumber(
value,
isOptional, // will ignore validation if value is null
propertyName, // it will contruct a message whenever validation fails, if propertyName is passed
isNegative,
isPositive,
isDivisibleBy,
max,
min
)
copied to clipboard
IsNumber(
value: json['someNumber'],
min: 0,
max: 100,
propertyName: 'someNumber'
)
copied to clipboard
IsEnum
Validates if the provided data matches one of the specified enum values.
enum UserRole { admin, user, guest }

IsEnum(
value: json['role'],
values: UserRole.values,
propertyName: 'role'
)
copied to clipboard
IsBoolean
Checks if the data is a boolean value.
IsBoolean(
value: json['isActive'],
propertyName: 'isActive'
)
copied to clipboard
IsList
Ensures the provided list data meets size requirements and applies additional validators to each element.

IsList(
value,
maxSize,
minSize,
nested, // this property should be a function which will applied to each element of the array. Function should return any one of the propertValidate types, like IsString, IsNumber, IsBoolean, IsEnum or IsList. please see example below
isOptional, // will ignore validation if value is null
propertyName, // it will contruct a message whenever validation fails, if propertyName is passed
)

IsList(
value: json['tags'],
minSize: 1,
nested: (element) => IsString(value: element, maxLen: 20),
propertyName: 'tags'
)
copied to clipboard
Leveraging dto_helpers effectively 🚀 #
The dto_helpers package is designed to simplify data validation across your Dart and Flutter applications, enhancing code quality and data integrity.
Feel free to contribute or raise issues on Github.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.