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

NAME

Valiant::Validations - Addos a validation DSL and API to your Moo/se classes

SYNOPSIS

    package Local::Person;

    use Moo;
    use Valiant::Validations;

    has name => (is=>'ro');
    has age => (is=>'ro');

    validates name => (
      length => {
        maximum => 10,
        minimum => 3,
      }
    );

    validates age => (
      numericality => {
        is_integer => 1,
        less_than => 200,
      },
    );

Validators on specific attributes can be added to the has clause if you prefer:

    package Local::Person;

    use Moo;
    use Valiant::Validations;

    has name => (
      is => 'ro',
      validates => [
        length => {
          maximum => 10,
          minimum => 3,
        },
      ],
    );

    has age => (
      is => 'ro',
      validates => [
        numericality => {
          is_integer => 1,
          less_than => 200,
        },
      ],
    );

Using validations on objects:

    my $person = Local::Person->new(
        name => 'Ja',
        age => 300,
      );

    $person->validate;
    $person->valid;     # FALSE
    $person->invalid;   # TRUE

    my %errors = $person->errors->to_hash(full_messages=>1);

    # \%errors = +{
    #   age => [
    #     "Age must be less than 200",
    #   ],
    #   name => [
    #     "Name is too short (minimum is 3 characters)',   
    #   ],
    # };

See Valiant for overall overview and Valiant::Validates for additional API level documentation.

DESCRIPTION

Using this package will apply the Valiant::Validates role to your current class as well as import several class methods from that role. It also wraps the has imported method so that you can add attribute validations as arguments to has if you find that approach to be neater than calling validates.

You can override several class methods of this package if you need to create your own custom subclass.

IMPORTS

The following subroutines are imported from Valiant::Validates

validates_with

Accepts the name of a custom validator or a reference to a function, followed by a list of arguments.

    validates_with sub {
      my ($self, $opts) = @_;
    };

    valiates_with 'SpecialValidator', arg1=>'foo', arg2=>'bar';

See validates_with in either Valiant or Valiant::Validates for more.

validates

Create validations on an objects attributes. Accepts the name of an attributes (or an arrayref of names) followed by a list of validators and global options. Validators can be a subroutine reference, a type constraint or the name of a Validator class.

    validates name => sub {
      my ($self, $attribute, $value, $opts) = @_;
      $self->errors->add($attribute, "Invalid", $opts) if ...
    };

    validates name => (
      length => {
        maximum => 10,
        minimum => 3,
      }
    );

See validates in either Valiant or Valiant::Validates for more.

METHODS

The following class methods are available for subclasses

default_role

Roles that are applied when using this class. Default is Valiant::Validates. If you are subclassing and wish to apply more roles, or if you've made your own version of Valiant::Validates you can override this method.

default_exports

Methods that are automatically exported into the calling package.

ADDING VALIDATIONS TO OBJECTS

Generally for best performance you will want to add validations to your classes, that way we can searching and precompile all the validations for optimized runtime. However you can add validations to objects after they are initialized and they will DTRT (add those validations only to the instance and not to the class).

    my $object = Local::Test::User->new(age=>5);
    $object->validates(age => (numericality => {greater_than => 10}));

Please note that you should expect some performance hit here since we need to search for and prepare the validation. So don't use this in hot parts of your code. Ideally you won't really need this feature and can work around using validation contexts but I saw now reason to prevent this from working for those unusual cases where it might be worth the price.

SEE ALSO

Valiant, Valiant::Validates

AUTHOR

See Valiant

COPYRIGHT & LICENSE

See Valiant