Valiant::Filters - Adds a filter DSL and API to your Moo/se classes
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:
has
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.
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.
filter
You can override several class methods of this package if you need to create your own custom subclass.
The following subroutines are imported from Valiant::Filters
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_with
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.
filters
The following class methods are available for subclasses
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.
Methods that are automatically exported into the calling package.
Valiant, Valiant::Filterable
See Valiant
To install Valiant, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Valiant
CPAN shell
perl -MCPAN -e shell install Valiant
For more information on module installation, please visit the detailed CPAN module installation guide.