NAME

Valiant::Validator::With - Filter using a coderef and options opts

SYNOPSIS

package Local::Test::User;

use Moo;
use Valiant::Filters;

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

filters name => (
  with => {
    cb => sub {
      my ($class, $attrs, $name, $opts) = @_;
      return $attrs->{$name}.$opts->{a};
    },
    opts => +{ a=>'foo' },
  },
  with => sub {
      my ($class, $attrs, $name) = @_;
      return $attrs->{$name}.'bar';
  },
  with => [sub {
      my ($class, $attrs, $name, $opts) = @_;
      return $attrs->{$name}.$opts;
  }, 'baz'],
);

my $user = Local::Test::User->new(name=>'john');

print $user->name; # 'johnfoobarbaz'

DESCRIPTIONi

this filter allows you to make a custom subroutine reference into a filter, with options options for parameterization. You can use this when you have a very special filter need but don't feel like writing a custom filter by subclassing Valiant::Filter::Each.

Passing parameters to $opts

You can pass parameters to the $opts hashref using the opts argument:

my $filter = sub {
  my ($self, $class, $attrs, $attribute_name, $opts) = @_;
  my $old_value = $attrs->{$attribute_name};
  # ...
  return $new_value

};

filters my_attribute => (
  with => {
    cb => $filter,
    opts => {arg => 2000},
  },
);

You might find this useful in creating more parametered callbacks. However at this point you might wish to consider just writing a custom filter class.

SHORTCUT FORM

This filter supports the follow shortcut forms:

validates attribute => ( with => sub { my $self = shift; ... }, ... );
validates attribute => ( with => [\&method, [1,2,3]], ... );

Which is the same as:

validates attribute => (
  with => {
    cb => sub { ... },
  },
  ...
);

validates attribute => (
  with => {
    cb => \&method,
    opts => [1,2,3],
  },
  ...
);

SEE ALSO

Valiant, Valiant::Filter, Valiant::Filter::Each.

AUTHOR

See Valiant

COPYRIGHT & LICENSE

See Valiant