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:
So you can create your own validator by just specifying a function for example:
Create simple validators from predicates
A simpler way is to use some of the built in helpers.
Use custom errors and filter them by type
Reuse validators
Use composition to build up more complex validators.
Validate and transform
Validators are also capable of transforming their input, so for instance we can do
parsing and validation in one go.
Field validations
Given a model, for instance a user:
Additional checks can be performed on the object and its fields by chaining a series of check
and checkField
methods.
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.
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.
Form validation
Often times you will have modeled your error type similar to:
In these scenarios its convenient to be able to group errors by field.
The solution verify provides for this is:
Sequencing
A slightly different API can be used to achieve the same results as the inOrder
composition function.
This way you have quick access to errors segmented by field.