verify 0.1.0 verify: ^0.1.0 copied to clipboard
Validations made simple. An extension based validation DSL.
Validations made simple
A fp inspired validation DSL. For Dart and Flutter projects.
Requirements #
The implementation of Verify relies heavily on dart extension methods, which are available for Dart versions >= 2.6
Features #
- Completely extensible (create your own combinators, validator primatives, etc)
- Flexible Verify is an extension based API (There is not single class created its all pure functions)
- Customizable (Define you own error types) organize validators how ever you want
- Plays well with bloc
Examples #
Create simple validators from predicates
final contains@ = Verify.property(
(String email) => email.contains('@'),
error: Error('email has to contain @')
);
final notEmpty = Verify.propery<String>((str) => !str.isEmpty, error: Error('field required'));
Reuse validators
final Validator_<String> emailValidator = Verify.all([ contains@, notEmpty ])
Validate model correctness
Given a model, for instance a user:
class User extends Equatable {
final String phone;
final String mail;
final int age;
User(this.phone, this.mail, this.age);
@override
List<Object> get props => [phone, mail, age];
}
Create some validations on its fields
final userValidator = Verify.empty<User>()
.checkProperty((user) => !user.phone.isEmpty, error: Error('phone empty'))
.validatingField((user) => user.mail, emailValidator);
final someUser = User('','', 25);
final Either<List<Error>, User> validationResult = userValidator.verify(someUser);
Organize validators
Here is one approach to organize validations for specific type
class ValidateString {
static Validator_<String> length(int length,
{@required ValidationError error}) {
return Verify.property((s) => s.length == length, error: error);
}
}