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

NAME

Valiant::Filters - Adds a filter DSL and API to your Moo/se classes

SYNOPSIS

    package Local::Person;

    use Moo;
    use Valiant::Filters;

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

    filters name => (
      truncate => { max_length=>25 }
    );

    filters_with 'Trim';

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

    package Local::Person;

    use Moo;
    use Valiant::Filters;

    has name => (is => 'ro', filters => [ truncate=>25 ] );
    has age => (is => 'ro');

    filters_with 'Trim';

Using filters on objects:

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

    print $person->name; # "Ja" not " Ja      ";

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

DESCRIPTION

Using this package will apply the Valiant::Filterable 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 filters as arguments to has if you find that approach to be neater than calling filter.

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::Filters

filters_with

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

    filter_with sub {
      my ($self, $class, $attrs) = @_;
    };

    filter_with 'SpecialFilters', arg1=>'foo', arg2=>'bar';

See filters_with in Valiant::Filterable for more.

filters

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 filters in Valiant::Filterable for more.

METHODS

The following class methods are available for subclasses

default_role

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

default_exports

Methods that are automatically exported into the calling package.

SEE ALSO

Valiant, Valiant::Filterable

AUTHOR

See Valiant

COPYRIGHT & LICENSE

See Valiant