NAME

Valiant::Validator - A role to define the validator interface.

SYNOPSIS

package MySpecialValidator;

use Moo;
with 'Valiant::Validator';

sub validate {
  my ($self, $object, $options) = @_;
  # DO your custom validation here.  Remember if you want to support
  # strict and message you should pass $options to any errors:
  # $object->errors->add('_base', 'Invalid', $options);
  # This method doesn't have to return anything in particular.
}

DESCRIPTION

This is a base role for defining a validator. This should be a class that defines a validate method. Here's a more detailed example that shows using a custom validator with a validatable object:

package Local::Test::Validator::Box;

use Moo;
with 'Valiant::Validator';

has max_size => (is=>'ro', required=>1);

sub validate {
  my ($self, $record, $opts) = @_;
  my $size = $record->height + $record->width + $record->length;
  if($size > $self->max_size) {
    $record->errors->add(_base=>"Total of all size cannot exceed ${\$self->max_size}", $opts),
  }
}

package Local::Test::Box;

use Moo;
use Valiant::Validations;

has [qw(height width length)] => (is=>'ro', required=>1);

validates [qw(height width length)] => (numericality=>+{});

validates_with 'Box', max_size=>25;
validates_with 'Box', max_size=>50, on=>'big', message=>'Big for Big!!';
validates_with 'Box', max_size=>30, on=>'big', if=>'is_very_tall';

sub is_very_tall {
  my ($self) = @_;
  return $self->height > 30 ? 1:0;
}

When used with validates_with we filter any extra arguments outside the globals (on, if/unless, message, strict) and pass them as init args when creating the validator.

A Validator is created once when the class uses it and exists for the full life cycle of the validatable object.

Generally you would write a validator class like this when the validation is very complex and cannot be tied to a specific attribute. If it can be tied to an attribute then you might prefer to use Valiant::Validator::Each.

PREPACKAGED VALIDATOR CLASSES

The following attribute validator classes are shipped with Valiant. Please see the package POD for usage details (this is only a sparse summary)

Absence

Checks that a value is absent (undefinef or empty).

See Valiant::Validator::Absence for details.

Array

Validations on an array value. Has options for nested errors when the array contains objects that themselves are validatible.

See Valiant::Validator::Array for details.

Boolean

Returns errors messages based on the boolean state of an attribute.

See Valiant::Validator::Boolean for details.

Check

Use your existing Type::Tiny constraints with Valiant

See Valiant::Validator::Check for details.

Confirmation

Add a confirmation error check. Used for when you want to verify that a given field is correct (such as when a user submits a new password or an email address).

See Valiant::Validator::Confirmation for details.

Date

Value must conform to standard date format (default is YYYY-MM-DD or eg 2000-01-01) and be a valid date.

See Valiant::Validator::Date for details.

Exclusion

Value cannot match a fixed list.

See Valiant::Validator::Exclusion for details.

Format

Value must be a string tht matched a given format or regular expression.

See Valiant::Validator::Format for details.

Inclusion

Value must be one of a fixed list

See Valiant::Validator::Inclusion for details.

Length

Value must be a string with given minimum and maximum lengths.

See Valiant::Validator::Length for details.

Numericality

Validate various types of numbers.

See Valiant::Validator::Numericality for details.

Object

Value is an object. Allows one to have nested validations when the object itself can be validated.

See Valiant::Validator::Object for details.

OnlyOf

Validates that only one or more of a group of attributes is defined.

See Valiant::Validator::OnlyOf for details.

Presence

That the value is defined and not empty

See Valiant::Validator::Absence for details.

Unique

That the value is unique based on some custom logic that your class must provide.

See Valiant::Validator::Unique for details.

With

Use a subroutine reference or the name of a method on your class to provide validation.

See Valiant::Validator::With for details.

Scalar

Verifies that the value is a string or number, basically a Perl scalar.

See Valiant::Validator::Scalar for details.

Special Validators

The following validators are not considered for end users but have documentation you might find useful in furthering your knowledge of Valiant: Valiant::Validator::Collection, Valiant::Validator::Each.

SEE ALSO

Valiant, Valiant::Validator::Each. =head1 AUTHOR

See Valiant

COPYRIGHT & LICENSE

See Valiant