The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Valiant::Validator::Numericality - Validate numeric attributes

SYNOPSIS

    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",
      ],
    };

DESCRIPTION

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.

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.

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).

CONSTRAINTS

Besides an overall test for either floating point or integer numericality this validator supports the following constraints:

only_integer

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' }, ... );
greater_than

Accepts numeric value or coderef. Returns error message tag greater_than if the attribute value isn't greater.

greater_than_or_equal_to

Accepts numeric value or coderef. Returns error message tag greater_than_or_equal_to_err if the attribute value isn't equal or greater.

equal_to

Accepts numeric value or coderef. Returns error message tag equal_to_err if the attribute value isn't equal.

other_than

Accepts numeric value or coderef. Returns error message tag other_than_err if the attribute value isn't different.

less_than

Accepts numeric value or coderef. Returns error message tag less_than_err if the attribute value isn't less than.

less_than_or_equal_to

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.

between

Accepts a two item arrayref, where the first is an inclusive lower number bound and the second is an inclusive upper number bound.

even

Accepts numeric value or coderef. Returns error message tag even_err if the attribute value isn't an even number.

odd

Accepts numeric value or coderef. Returns error message tag odd_err if the attribute value isn't an odd number.

divisible_by

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.

decimals

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.

positive

A number greater or equal to zero

negative

A number less than zero

SHORTCUT FORM

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

 

GLOBAL PARAMETERS

This validator supports all the standard shared parameters: if, unless, message, strict, allow_undef, allow_blank.

SEE ALSO

Valiant, Valiant::Validator, Valiant::Validator::Each.

AUTHOR

See Valiant

COPYRIGHT & LICENSE

See Valiant

9 POD Errors

The following errors were encountered while parsing the POD:

Around line 244:

Deleting unknown formatting code V<>

Around line 249:

Deleting unknown formatting code V<>

Around line 254:

Deleting unknown formatting code V<>

Around line 259:

Deleting unknown formatting code V<>

Around line 264:

Deleting unknown formatting code V<>

Around line 269:

Deleting unknown formatting code V<>

Around line 279:

Deleting unknown formatting code V<>

Around line 284:

Deleting unknown formatting code V<>

Around line 296:

Deleting unknown formatting code V<>