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

NAME

Valiant::Validator::Hash - Verify a related object

SYNOPSIS

    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,
      hash => [
        [street => presence => 1, length => [2,24] ],
        [city => presence => 1, length => [2,24] ],
        [zip => presence => 1, numericality => 'positive_integer', format => qr/\d\d\d\d\d/ ],
      ],
    );

    # long form example
    validates address => (
      presence => 1,
      hash => {
        validations => {
          street => [format => qr/[^\=]/, message => 'cannot have silly characters'],
          zip => [length => [5,5]],
        }
      }
    );

    my $person = Local::Test::Person->new(
      name => '12',
      address => +{
        street => '=',
        city => 'Elgin',
        zip => '2aa',
      },
    );

  $person->validate->invalid; # True, the object is invalid.

  warn $person->errors->_dump;

  $VAR1 = {
        address => [
          {
            street => [
              "Street is too short (minimum is 2 characters)",
              "Street cannot have silly characters",
            ],
            zip => [
              "Zip must be an integer",
              "Zip does not match the required pattern",
              "Zip is too short (minimum is 5 characters)",
            ],
          },
        ],
    'name' => [
              'Name does not match the required pattern'
            ]
    };

DESCRIPTION

Perform validations on values when the value associated with the attribute is a hashref. You can use this when inflating an object seems like silly work.

ATTRIBUTES

This validator supports the following attributes:

validations

Either 1 or an arrayref of validation rules. Each item in the arrayref is an arrayref that contains anything you may pass to validates. Typically this will be a key name for the hashref followed by a list of validation rules.

for / namespace

When defining an inline validation ruleset against an associated object that does not itself have validation rules, you must set this to something that ISA or DOES the class you are defining inline validations on. This is not currently strictly enforced, but this value is used to find any locale files or custom validator classes.

validator

This contains an instance of Valiant::Class or subclass. Default value does the right thing but you can override if you need a special subclass or you need to pass one in that's already constructed.

validator_class

Defaults to Valiant::Class, which value should be a subclass of. You probably only need this again if you are doing very custom validations. You probably only want do to this if there's no other idea.

validator_class_args

A hashref of args that get passed to the new method of validator_class. Defaults to an empty hashref. You might need this if you build a custom validator class and have special arguments it needs.

SHORTCUT FORM

This validator supports the follow shortcut forms:

    validates attribute => ( hash => \@validation_rules, ... );

Which is the same as:

    validates attribute => (
      hash => {
        validations => \@validation_rules,
      }
    );

But less typing and probably makes sense unless you need to set some of the more rarely used attributes such as validator_class etc.

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