Valiant::Proxy - Create a validation ruleset dynamically
my $validator = Valiant::Class->new( validations => [ [ sub { unless($_[0]->is_active) { $_[0]->errors->add(_base=>'Cannot change inactive user') } } ], [ name => length => [2,15], format => qr/[a-zA-Z ]+/ ], [ age => numericality => 'positive_integer' ], ] );
You can also call an API to add validation rules
$validator ->validates(name => length => [2,15], format => qr/[a-zA-Z ]+/) ->validates(age => numericality => 'positive_integer') ->validates_with('UserValidator'); # Calls Local::Test::User::UserValidator
Then runs validation on it with an instance of a concrete class that has no validation rules of its own:
package Local::Test::User { use Moo; has ['name', 'age', 'is_active'], is=>'ro', required=>1; } # A user with several validation issues my $user = Local::Test::User->new( name=>'01', age=>-15, is_active=>0); my $result = $validator->validate($user); $result->invalid; # TRUE warn $result->errors->_dump; $VAR = { '_base' => [ 'Cannot change inactive user' ], 'age' => [ 'Age must be greater than or equal to zero' ], 'name' => [ 'Name does not match the required pattern' ] };
Create a validation runner for a given class or role. Useful when you need (or prefer) to build up a validation ruleset in code rather than via the annotations-like approach given in Valiant::Validations. Can also be useful to add validations to a class that isn't Moo/se and can't use Valiant::Validations or is outside your control (such as a third party library). Lastly you may need to build validation sets based on existing metadata, such as via database introspection or from a file containing validation instructions.
Please note that the code used to create the validation object is not speed optimized so I recommend you not use this approach in 'hot' code paths. Its probably best if you can create all these during your application startup once (for long lived applications). Maybe not ideal for 'fire and forget' scripts like cron jobs or CGI.
This object has the followed attributes
The class this validator is for. Used to load locale files and to look for custom validation objects. Should something that ISA or DOES of the class that you are going to run validations on (this currently isnt enforced but please to rely on that).
Defaults to Valiant::Result::Object. Needs to be something that does Valiant::Result. Write your own if you have an object with unusual attribute accessors.
Defaults to Valiant::Meta. Should be something that is a subclass of that. You probably won't overrride this unless you are doing extremely odd stuff.
Should be an arrayref of validation rules, where each rule is an arrayref containing the rules (where the rules are anything you'd pass to validates in Valiant::Validations
validates
This class does the following methods
Given an instance of the object to be validated, return a result objects that wraps it and provides any validation errors.
Adds validation rules.
Adds a validation object.
Valiant, Valiant::Validator, Valiant::Validator::Each.
See Valiant
To install Valiant, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Valiant
CPAN shell
perl -MCPAN -e shell install Valiant
For more information on module installation, please visit the detailed CPAN module installation guide.