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