Valiant::Validator::Array - Verify items in an arrayref.
package Local::Test::Car; use Moo; use Valiant::Validations; has ['make', 'model', 'year'] => (is=>'ro'); validates make => ( inclusion => [qw/Toyota Tesla Ford/] ); validates model => ( length => [2, 20] ); validates year => ( numericality => { greater_than_or_equal_to => 1960 }); package Local::Test::Array; use Moo; use Valiant::Validations; has status => (is=>'ro'); has name => (is=>'ro'); has car => (is=>'ro'); validates name => (length=>[2,5]); validates car => ( array => { validations => [object=>1] } ); validates status => ( array => { max_length => 3, min_length => 1, validations => [ inclusion => +{ in => [qw/active retired/], }, ] }, ); my $car = Local::Test::Car->new( make => 'Chevy', model => '1', year => 1900 ); my $object = Local::Test::Array->new( name => 'napiorkowski', status => [qw/active running retired retired aaa bbb ccc active/], car => [$car], ); $object->validate->invalid; # TRUE $object->car->[0]->invalid; # TRUE # Error Messages my $all_errors = +{ $object->errors->to_hash(full_messages=>1) }; # $all_errors = { # car => [ # "Car Is Invalid", # ], # "car.0" => [ # "Car Is Invalid", # ], # "car.0.make" => [ # "Car Make is not in the list", # ], # "car.0.model" => [ # "Car Model is too short (minimum is 2 characters)", # ], # "car.0.year" => [ # "Car Year must be greater than or equal to 1960", # ], # name => [ # "Name is too long (maximum is 5 characters)", # ], # status => [ # "Status Is Invalid", # ], # "status.1" => [ # "Status is not in the list", # ], # "status.4" => [ # "Status is not in the list", # ], # "status.5" => [ # "Status is not in the list", # ], # "status.6" => [ # "Status is not in the list", # ], # }; # Errors just on the car array item my $car_errors = +{ $object->car->[0]->errors->to_hash(full_messages=>1) }; # $car_errors = { # make => [ # "Make is not in the list", # ], # model => [ # "Model is too short (minimum is 2 characters)", # ], # year => [ # "Year must be greater than or equal to 1960", # ], # };
Validations for arrays (really arrayrefs since that's how Moo attrivbutes work). Allows you to define validations on the array as a whole (such as set a maximum or minimum array length) as well as define validations on the array individual items. Can be used with the Valiant::Validator::Object validator to deeply nest arrays of objects.
This validator defines the following attributes
The maximum size of the array. For example "@a = (1,2,3)" has size of 3.
The minimum size of the array.
The errors associated with the minimum or maximum array size errors. Defaults are translatio tag 'max_length_err' and 'min_length_err'.
The message returned when an array is generically invalid. An array becomes invalid should any validations you define on array items fail to validate. Default is translation tag 'invalid'.
An arrayref of validations that are run on each item in the list. Keep in mind the performance inplications of this should the list be very long.
This validator supports the follow shortcut forms:
validates attribute => ( array => [ presence=>1, length=>[2,10] ], ... );
Which is the same as:
validates attribute => ( validations => [ presence => 1, length => [2,10], ], );
This validator supports all the standard shared parameters: if, unless, message, strict, allow_undef, allow_blank.
if
unless
message
strict
allow_undef
allow_blank
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.