Valiant::Validator::Format - Validate a value based on a regular expression
package Local::Test::Format; use Moo; use Valiant::Validations; has phone => (is=>'ro'); has name => (is=>'ro'); has email => (is=>'ro'); validates phone => ( format => +{ match => qr/\d\d\d-\d\d\d-\d\d\d\d/, }, ); validates name => ( format => +{ without => qr/\d+/, }, ); validates email => format => 'email'; my $object = Local::Test::Format->new( phone => '387-1212', name => 'jjn1056', ); $object->validate; warn $object->errors->_dump; $VAR1 = { 'phone' => [ 'Phone does not match the required pattern' ], 'name' => [ 'Name contains invalid characters' ], 'email' => [ 'Email is not an email address' ], };
Validates that the attribute value either matches a given regular expression (match) or that it fails to match an exclusion expression (without).
match) or that it fails to match an exclusion expression (without).
without
Values that fail the match condition (which can be a regular expression or a reference to a method that provides one) will add an error matching the tag invalid_format_match, which can be overridden as with other tags.
match
invalid_format_match
Values that match the without conditions (also either a regular expression or a coderef that provides one) with all an error matching the tag invalid_format_without which can also be overridden via a passed parameter.
invalid_format_without
A number of format shortcuts are built in to help you bootstrap faster. Use the text string in place of the regexp (for example:)
validates email => ( format => +{ match => 'email', } );
You can add to the prebuilts by adding keys to the global %prebuilt_formats hash. See code for more.
%prebuilt_formats
Uses a regexp to validate that a string looks like an email. This is naive but probably acceptable for most uses. For real detailed eail checking you'll need to build something on Email::Valid. Uses translation tag not_email.
not_email
Text can only be upper or lowercase letter. Uses translation tag not_alpha.
not_alpha
Text can only be upper or lowercase letters, numbers or '_'. Uses translation tag not_alpha_numeric.
not_alpha_numeric
Only letters and spaces. Error message default is translation tag not_words.
not_words
Must contain only a single word. Adds error not_word if fails.
not_word
Match US zipcodes. This is a pattern match only, we don't actually check if the zip is a true valid zipcode, just that it looks like one. zip5 matches a common 5 digit zipcode and return not_zip5 translation tag if not. zip9 looks for the 'Zip +4' extended zipcode, such as 11111-2222 (the separator can be either '-' or a space). Adds errors not_zip9 if not. Finally zip will match either zip or zip +4 and add error not_zip on failure to match.
zip5
not_zip5
zip9
not_zip9
zip
not_zip
Must contain only ASCII characters. Adds error not_ascii if fails.
not_ascii
This validator supports the follow shortcut forms:
validates attribute => ( format => qr/\d\d\d/, ... );
Which is the same as:
validates attribute => ( format => +{ match => qr/\d\d\d/, }, );
We choose to shortcut the 'match' pattern based on experiene that suggested it is more common to required a specific pattern than to have an exclusion pattern.
This also works for the coderef form.
validates attribute => ( format => \&pattern, ... );
validates attribute => ( format => +{ match => \&pattern, }, );
And of course its very handy for using one of the built in pattern tags:
validates email => (format => 'email');
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
2 POD Errors
The following errors were encountered while parsing the POD:
Unterminated C<...> sequence
You forgot a '=back' before '=head1'
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.