Valiant::Validator::Numericality - Validate numeric attributes
package Local::Test::Numericality; use Moo; use Valiant::Validations; has age => (is => 'ro'); has equals => (is => 'ro', default => 33); validates age => ( numericality => { only_integer => 1, less_than => 200, less_than_or_equal_to => 199, greater_than => 10, greater_than_or_equal_to => 9, equal_to => \&equals, }, ); validates equals => (numericality => [5, 100]); my $object = Local::Test::Numericality->new(age=>8, equal=>40); $object->validate; warn $object->errors->_dump; $VAR1 = { age => [ "Age must be equal to 40", "Age must be greater than 10", "Age must be greater than or equal to 9", ], };
Validates that your attributes have only numeric values. By default, it will match an optional sign followed by an integral or floating point number. To specify that only integral numbers are allowed set only_integer to true.
only_integer
There's several parameters you can set to place different type of numeric limits on the value. There's no checks on creating non sense rules (you can set a greater_than of 10 and a less_than of 5, for example) so pay attention.
greater_than
less_than
All parameter values can be either a constant or a coderef (which will get $self as as argument). The coderef option exists to make it easier to write dynamic checks without resorting to writing your own custom validators. Each value also defines a translation tag which folows the pattern "${rule}_err" (for example the greater_than rules has a translation tag greater_than_err). You can use the message parameter to set a custom message (either a string value or a translation tag).
$self
greater_than_err
message
Besides an overall test for either floating point or integer numericality this validator supports the following constraints:
When set to a true value will require the value to be some sort of integer. If you set this to 1 then the value must be generally an integer. However you can also set it to the following to get more limited integer types:
validates attribute => ( numericality => { only_integer => 'positive_integer' }, ... ); validates attribute => ( numericality => { only_integer => 'negative_integer' }, ... ); # Lets you require the integer to conform to Postgresql Serial or Bigserial types validates attribute => ( numericality => { only_integer => 'pg_serial' }, ... ); validates attribute => ( numericality => { only_integer => 'pg_bigserial' }, ... );
Accepts numeric value or coderef. Returns error message tag greater_than if the attribute value isn't greater.
Accepts numeric value or coderef. Returns error message tag greater_than_or_equal_to_err if the attribute value isn't equal or greater.
Accepts numeric value or coderef. Returns error message tag equal_to_err if the attribute value isn't equal.
Accepts numeric value or coderef. Returns error message tag other_than_err if the attribute value isn't different.
Accepts numeric value or coderef. Returns error message tag less_than_err if the attribute value isn't less than.
Accepts numeric value or coderef. Returns error message tag less_than_or_equal_to_err if the attribute value isn't less than or equal.
Accepts a two item arrayref, where the first is an inclusive lower number bound and the second is an inclusive upper number bound.
Accepts numeric value or coderef. Returns error message tag even_err if the attribute value isn't an even number.
Accepts numeric value or coderef. Returns error message tag odd_err if the attribute value isn't an odd number.
Accepts numeric value or coderef. Returns error message divisible_by_err if the attribute value is not evenly divisible by the value. For example if the attribute value is 15 and the divisible value is 5 that is true (its divisible) but of the divisible value was 4 that woule be false and generate an error message.
divisible_by_err
Accepts numeric value or coderef. Returns error message tag decimals_err if the attribute value doesn't contain exactly the requird number of places after the decimal point.
A number greater or equal to zero
A number less than zero
This validator supports the follow shortcut forms:
validates attribute => ( numericality => [1,10], ... );
Which is the same as:
validates attribute => ( numericality => { greater_than_or_equal_to => 1, less_than_or_equal_to => 10, }, );
If you merely wish to test for overall numericality you can use:
validates attribute => ( numericality => +{}, ... );
You can require various integer types as well:
validates attribute => ( numericality => 'integer', ... ); validates attribute => ( numericality => 'positive_integer', ... ); validates attribute => ( numericality => 'negative_integer' ... ); validates attribute => ( numericality => 'pg_serial' ... ); # Postgresql Serial validates attribute => ( numericality => 'pg_bigserial' ... ); # Postgresql Bigserial
Misc shortcuts:
validates attribute => ( numericality => 'positive' ... ); # a positive number validates attribute => ( numericality => 'negative' ... ); # a negative number validates attribute => ( numericality => 'even' ... ); # an even number validates attribute => ( numericality => 'odd' ... ); # an odd number
This validator supports all the standard shared parameters: if, unless, message, strict, allow_undef, allow_blank.
if
unless
strict
allow_undef
allow_blank
Valiant, Valiant::Validator, Valiant::Validator::Each.
See Valiant
9 POD Errors
The following errors were encountered while parsing the POD:
Deleting unknown formatting code V<>
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.