Valiant::Validator - A role to define the validator interface.
package MySpecialValidator; use Moo; with 'Valiant::Validator'; sub validate { my ($self, $object, $options) = @_; # DO your custom validation here. Remember if you want to support # strict and message you should pass $options to any errors: # $object->errors->add('_base', 'Invalid', $options); # This method doesn't have to return anything in particular. }
This is a base role for defining a validator. This should be a class that defines a validate method. Here's a more detailed example that shows using a custom validator with a validatable object:
validate
package Local::Test::Validator::Box; use Moo; with 'Valiant::Validator'; has max_size => (is=>'ro', required=>1); sub validate { my ($self, $record, $opts) = @_; my $size = $record->height + $record->width + $record->length; if($size > $self->max_size) { $record->errors->add(_base=>"Total of all size cannot exceed ${\$self->max_size}", $opts), } } package Local::Test::Box; use Moo; use Valiant::Validations; has [qw(height width length)] => (is=>'ro', required=>1); validates [qw(height width length)] => (numericality=>+{}); validates_with 'Box', max_size=>25; validates_with 'Box', max_size=>50, on=>'big', message=>'Big for Big!!'; validates_with 'Box', max_size=>30, on=>'big', if=>'is_very_tall'; sub is_very_tall { my ($self) = @_; return $self->height > 30 ? 1:0; }
When used with validates_with we filter any extra arguments outside the globals (on, if/unless, message, strict) and pass them as init args when creating the validator.
validates_with
on
if/unless
message
strict
A Validator is created once when the class uses it and exists for the full life cycle of the validatable object.
Generally you would write a validator class like this when the validation is very complex and cannot be tied to a specific attribute. If it can be tied to an attribute then you might prefer to use Valiant::Validator::Each.
The following attribute validator classes are shipped with Valiant. Please see the package POD for usage details (this is only a sparse summary)
Checks that a value is absent (undefinef or empty).
See Valiant::Validator::Absence for details.
Validations on an array value. Has options for nested errors when the array contains objects that themselves are validatible.
See Valiant::Validator::Array for details.
Returns errors messages based on the boolean state of an attribute.
See Valiant::Validator::Boolean for details.
Use your existing Type::Tiny constraints with Valiant
See Valiant::Validator::Check for details.
Add a confirmation error check. Used for when you want to verify that a given field is correct (such as when a user submits a new password or an email address).
See Valiant::Validator::Confirmation for details.
Value must conform to standard date format (default is YYYY-MM-DD or eg 2000-01-01) and be a valid date.
See Valiant::Validator::Date for details.
Value cannot match a fixed list.
See Valiant::Validator::Exclusion for details.
Value must be a string tht matched a given format or regular expression.
See Valiant::Validator::Format for details.
Value must be one of a fixed list
See Valiant::Validator::Inclusion for details.
Value must be a string with given minimum and maximum lengths.
See Valiant::Validator::Length for details.
Validate various types of numbers.
See Valiant::Validator::Numericality for details.
Value is an object. Allows one to have nested validations when the object itself can be validated.
See Valiant::Validator::Object for details.
Validates that only one or more of a group of attributes is defined.
See Valiant::Validator::OnlyOf for details.
That the value is defined and not empty
That the value is unique based on some custom logic that your class must provide.
See Valiant::Validator::Unique for details.
Use a subroutine reference or the name of a method on your class to provide validation.
See Valiant::Validator::With for details.
The following validators are not considered for end users but have documentation you might find useful in furthering your knowledge of Valiant: Valiant::Validator::Collection, Valiant::Validator::Each.
Valiant, Valiant::Validator::Each. =head1 AUTHOR
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.