Valiant::Validator::Object - Verify a related object
package Local::Test::Address; use Moo; use Valiant::Validations; has street => (is=>'ro'); has city => (is=>'ro'); has country => (is=>'ro'); validates ['street', 'city'], presence => 1, length => [3, 40]; validates 'country', presence => 1, inclusion => [qw/usa uk canada japan/]; package Local::Test::Person; use Moo; use Valiant::Validations; has name => (is=>'ro'); has address => (is=>'ro'); has car => (is=>'ro'); validates name => ( length => [2,30], format => qr/[A-Za-z]+/, #yes no unicode names for this test... ); validates address => ( presence => 1, object => { nested => 1, isa => 'Local::Test::Address', } );
Runs validations on an object which is assigned as an attribute and aggregates those errors (if any) onto the parent object.
Useful when you need to validate an object graph or nested forms.
If your nested object has a nested object it will follow all the way down the rabbit hole Just don't make self referential nested objects; that's not tested and likely to end poorly. Patches welcomed.
This validator supports the following attributes:
A boolean that specifies if we should run 'validates' on the object. Default is false.
Reference to a Type::Tiny style type constraint. If specified then the object must pass the constraint.
The message we return when 'type_constraint' fails. We pass 'display_name' and 'error_message' as options to the tag.
The name of a class that the object should inherit from
The message we return when 'isa' fails. We pass 'parent' (the name of the class we require inheritance from) as options to the tag.
A role that the object is expected to consume
The message we return when 'role' fails. We pass 'rolw' (the name of the role we require to consume from) as options to the tag.
The error message returned when the object has nested validation errors.
The error returned when the value is not an object
This validator supports the follow shortcut forms:
validates attribute => ( object => 1, ... );
Which is the same as:
validates attribute => ( object => { nested => 1, } );
<Note: you can use the 'nested' alias for '1' here if you want.
You can also specify a type constraint:
use use Types::Standard 'Str'; validates attribute => ( object => Str, ... );
use use Types::Standard 'Str'; validates attribute => ( object => { type_constraint => Str, } );
When you nest a object with validations as in the following example any error messages in the nested object are imported into the parent object:
package Local::Test::Address; use Moo; use Valiant::Validations; has street => (is=>'ro'); has city => (is=>'ro'); has country => (is=>'ro'); validates ['street', 'city'], presence => 1, length => [3, 40]; validates 'country', presence => 1, inclusion => [qw/usa uk canada japan/]; package Local::Test::Person; use Moo; use Valiant::Validations; has name => (is=>'ro'); has address => (is=>'ro'); validates name => ( length => [2,30], format => qr/[A-Za-z]+/, #yes no unicode names for this test... ); validates address => ( presence => 1, object => { nested => 1, } ); my $address = Local::Test::Address->new( city => 'NY', country => 'Russia' ); my $person = Local::Test::Person->new( name => '12234', address => $address, ); my $address_errors = +{ $person->address->errors->to_hash(full_messages=>1) }; # $address_errors = +{ # 'country' => [ # 'Country is not in the list' # ], # 'city' => [ # 'City is too short (minimum is 3 characters)' # ], # 'street' => [ # 'Street can\'t be blank', # 'Street is too short (minimum is 3 characters)' # ], # }; my $person_errors = +{ $person->errors->to_hash(full_messages=>1) }; # $address_errors = +{ # name => [ # "Name does not match the required pattern", # ], # address => [ # "Address Is Invalid", # ], # "address.city" => [ # "Address City is too short (minimum is 3 characters)", # ], # "address.country" => [ # "Address Country is not in the list", # ], # "address.street" => [ # "Address Street can't be blank", # "Address Street is too short (minimum is 3 characters)", # ], };
When accessing errors for display you'll have to choose which access approach is best for your application.
Please note that you can have objects nested inside of objects so this can lead to very complex error messaging.
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.