validations made simple
a fp inspired validation dsl. for dart and flutter projects.
features
- completely extensible (create your own combinators, validator primitives, etc)
- flexible verify is an extension based api (there is not single class created its all pure functions)
- customizable (define you own error types if required) organize validators how ever you want
- bloc friendly (see examples for a concrete implementation)
- null safe (as a prerelease)
usage
creating validators
a validator is just a simple function alias:
// one of 2 variats depending on wether the validated subject will be transformed into another type or not
typedef validatort<s, t> = either<list<dynamic>, t> function(s subject);
typedef validator<s> = either<list<dynamic>, s> function(s subject);
so you can create your own validator by just specifying a function for example:
final validator<string> emailvalidator = (string email) {
return email.contains('@') ? right(email) : left('bad email format')
};
create simple validators from predicates
a simpler way is to use some of the built in helpers.
final [email protected] = verify.that(
(string email) => email.contains('@'),
error: 'bad email format'
);
use custom errors and filter them by type
reuse validators
use composition to build up more complex validators.
final validator<string> emailvalidator = verify.all([ [email protected], notempty ])
validate and transform
validators are also capable of transforming their input, so for instance we can do
parsing and validation in one go.
final validator<string, int> intparsingvalidator = (string str) => right(int.parse(str));
final validator = intparsingvalidator.onexception((_) => error('not an integer'));
field validations
given a model, for instance a user:
class user extends equatable {
final string? phone;
final string? mail;
final int? age;
const user(this.phone, this.mail, this.age);
@override
list<object> get props => [phone ?? '', mail ?? '', age ?? ''];
}
additional checks can be performed on the object and its fields by chaining a series of check
and checkfield
methods.
final uservalidator = verify.empty<user>()
.check((user) => !user.phone!.isempty, error: error('phone empty'))
.checkfield((user) => user.mail!, emailvalidator);
final someuser = user('','', 25);
final either<list<error>, user> validationresult = uservalidator.verify(someuser);
note: the difference between check and checkfield is that the later ignore the verification when the value is null,
this will likely change in next version supporting null safety.
run a validator
running a validator is a simple as passing in a parameter since its just a function.
to be a bit more eloquent a verify
method is provided, this method is special because besides
forwarding the argument to the calling validator it can also be used to filter the error list and
have it cast to a specific error type. just supply a specific type parameter.
final signupvalidation = verify.subject<signupstate>();
final errors = signupvalidation
.verify<signuperror>(newstate); // either<list<signuperror>, signupstate>
built in validators
verify doesn’t come with many built in validators, because they are so simple to create.
it does come with some regex shorthands.
final validator = verify.fromregex(regexp(r"(^d+$)", error: error('not just digits')) // validator<string, int>
form validation
often times you will have modeled your error type similar to:
enum formfield {
email,
password,
passwordconfirmation,
}
class signuperror extends validationerror {
final string message;
final formfield field;
signuperror(this.message, {required this.field});
@override
string get errordescription => message;
}
in these scenarios its convenient to be able to group errors by field.
the solution verify provides for this is:
final validator = verify.inorder<signupformstate>([
validatemail,
validatepassword,
validateconfirmation,
]);
final map<formfield, signuperror> errormap = validator
.verify<signuperror>(somestate)
.groupederrorsby((error) => error.field);
sequencing
a slightly different api can be used to achieve the same results as the inorder
composition function.
final numbervalidator = verify.subject<int>()
.then(verify.that(
(subject) => subject % 2 == 0,
error: error('not even'),
))
.then(verify.that(
(subject) => subject >= 10,
error: error('single digit'),
));
final errors2 = numbervalidator.errors(3); // yields 1 error
final errors = numbervalidator.errors(4); // yields 1 error
this way you have quick access to errors segmented by field.
Comments are closed.