| 1 | package Valiant::Validator::Exclusion; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Valiant::I18N; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | with 'Valiant::Validator::Each'; | |||||||||
| 7 | |||||||||||
| 8 | 1 | has in => (is=>'ro', required=>1); | |||||||||
| 9 | 2 | ▶ | has exclusion => (is=>'ro', required=>1, default=>sub {_t 'exclusion'}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub normalize_shortcut { | ||||||||
|
Subroutine
normalize_shortcut: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 12 | 0 | my ($class, $arg) = @_; | |||||||||
| 13 | 0 | ▶ | $arg = [$arg] unless ref $arg; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 0 | return +{ in => $arg }; | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | 2 | ▶ | sub validate_each { | ||||||||
|
Subroutine
validate_each: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 18 | 2 | my ($self, $record, $attribute, $value, $opts) = @_; | |||||||||
| 19 | |||||||||||
| 20 | 2 | my $in = $self->in; | |||||||||
| 21 | 2 | my @in = (); | |||||||||
| 22 | 2 | ▶ | if(ref($in) eq 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 1 | @in = $in->($record); | |||||||||
| 24 | } else { | ||||||||||
| 25 | 1 | @in = @$in; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | if(grep { $_ eq $value } @in) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 2 | $record->errors->add($attribute, $self->exclusion, +{%$opts, list=>\@in}) | |||||||||
| 31 | } | ||||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 1 | 1; | |||||||||
| 35 | |||||||||||
| 36 | =head1 NAME | ||||||||||
| 37 | |||||||||||
| 38 | Valiant::Validator::Exclusion - Value cannot be in a list | ||||||||||
| 39 | |||||||||||
| 40 | =head1 SYNOPSIS | ||||||||||
| 41 | |||||||||||
| 42 | package Local::Test::Exclusion; | ||||||||||
| 43 | |||||||||||
| 44 | use Moo; | ||||||||||
| 45 | use Valiant::Validations; | ||||||||||
| 46 | |||||||||||
| 47 | has domain => (is=>'ro'); | ||||||||||
| 48 | has country => (is=>'ro'); | ||||||||||
| 49 | |||||||||||
| 50 | validates domain => ( | ||||||||||
| 51 | exclusion => +{ | ||||||||||
| 52 | in => [qw/org co/], | ||||||||||
| 53 | }, | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | validates country => ( | ||||||||||
| 57 | exclusion => +{ | ||||||||||
| 58 | in => \&restricted, | ||||||||||
| 59 | }, | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | sub restricted { | ||||||||||
| 63 | my $self = shift; | ||||||||||
| 64 | return (qw(usa uk)); | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | my $object = Local::Test::Exclusion->new( | ||||||||||
| 68 | domain => 'org', | ||||||||||
| 69 | country => 'usa', | ||||||||||
| 70 | ); | ||||||||||
| 71 | |||||||||||
| 72 | $object->validate; | ||||||||||
| 73 | |||||||||||
| 74 | warn $object->errors->_dump; | ||||||||||
| 75 | |||||||||||
| 76 | $VAR1 = { | ||||||||||
| 77 | 'country' => [ | ||||||||||
| 78 | 'Country is reserved' | ||||||||||
| 79 | ], | ||||||||||
| 80 | 'domain' => [ | ||||||||||
| 81 | 'Domain is reserved' | ||||||||||
| 82 | ] | ||||||||||
| 83 | }; | ||||||||||
| 84 | |||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||
| 86 | |||||||||||
| 87 | Value cannot be from a list of reserved values. Value can be given | ||||||||||
| 88 | as either an arrayref or a coderef (which recieves the validating | ||||||||||
| 89 | object as the first argument, so you can call methods for example). | ||||||||||
| 90 | |||||||||||
| 91 | If value is invalid uses the C<exclusion> translation tag (which you can | ||||||||||
| 92 | override as an argument). | ||||||||||
| 93 | |||||||||||
| 94 | =head1 SHORTCUT FORM | ||||||||||
| 95 | |||||||||||
| 96 | This validator supports the follow shortcut forms: | ||||||||||
| 97 | |||||||||||
| 98 | validates attribute => ( exclusion => [qw/a b c/], ... ); | ||||||||||
| 99 | |||||||||||
| 100 | Which is the same as: | ||||||||||
| 101 | |||||||||||
| 102 | validates attribute => ( | ||||||||||
| 103 | exclusion => +{ | ||||||||||
| 104 | in => [qw/a b c/], | ||||||||||
| 105 | }, | ||||||||||
| 106 | ); | ||||||||||
| 107 | |||||||||||
| 108 | This also works for the coderef form: | ||||||||||
| 109 | |||||||||||
| 110 | validates attribute => ( exclusion => \&coderef, ... ); | ||||||||||
| 111 | |||||||||||
| 112 | validates attribute => ( | ||||||||||
| 113 | exclusion => +{ | ||||||||||
| 114 | in => \&coderef, | ||||||||||
| 115 | }, | ||||||||||
| 116 | ); | ||||||||||
| 117 | |||||||||||
| 118 | =head1 GLOBAL PARAMETERS | ||||||||||
| 119 | |||||||||||
| 120 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||
| 121 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 SEE ALSO | ||||||||||
| 124 | |||||||||||
| 125 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||
| 126 | |||||||||||
| 127 | =head1 AUTHOR | ||||||||||
| 128 | |||||||||||
| 129 | See L<Valiant> | ||||||||||
| 130 | |||||||||||
| 131 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 132 | |||||||||||
| 133 | See L<Valiant> | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||