Valiant/Validator/Array.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 60.0% 6 / 10 Conditioncond 54.5% 6 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 78.7% 74 / 94 CC 10 SCAR 14.7
CC
10
Cov
85%
validate_each
8.9
normalize_shortcut
3.2
BEGIN
1.0
CRAP 4.3
« Valiant/Filter/Collection.pm ↑ Summary Valiant/Validator/OnlyOf.pm »
1 package Valiant::Validator::Array;
2  
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 with 'Valiant::Validator::Each';
8  
9 2 has max_length => (is=>'ro', predicate=>'has_max_length');
10 2 has min_length => (is=>'ro', predicate=>'has_min_length');
11 2 has validations => (is=>'ro', required=>1);
12  
13 2 has validator_class => (is=>'ro', required=>1, default=>'Valiant::Proxy::Array');
14 4 ▶has validator_class_args => (is=>'ro', required=>1, default=>sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
15 4 ▶has invalid_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid'});
Subroutine __ANON__: called CC=1 SCAR=0.0
16 4 ▶has max_length_err => (is=>'ro', required=>1, default=>sub {_t 'max_length_err'});
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 ▶has min_length_err => (is=>'ro', required=>1, default=>sub {_t 'min_length_err'});
Subroutine __ANON__: called CC=1 SCAR=0.0
18 4 ▶has not_array_err => (is=>'ro', required=>1, default=>sub {_t 'not_array_err'});
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 4 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 4 my ( $orig, $class, @args ) = @_;
22 4 my $args = $class->$orig(@args);
23 4 return $args;
24 2 };
25  
26 2 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=3 SCAR=11.7
Pod undocumented
27 2 my ($class, $arg) = @_;
28 2 ▶ if( (ref($arg)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $arg || "") eq "ARRAY") 2 0 2
Condition50%1/2
ref $arg || ''
l!l
20
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
Aresult
11
00
29 2 return { validations => $arg };
30 }
31 }
32  
33 4 ▶sub validate_each {
Subroutine validate_each: called CC=8 SCAR=21.9
Pod undocumented
34 4 my ($self, $record, $attribute, $value, $options) = @_;
35  
36 4 ▶ unless(ref($value) and (ref($value) eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value and ref $value eq "ARRAY") 0 4 4
Condition33%1/3
ref $value and ref $value eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $value and ref $value eq "ARRAY"
ref $value ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= ref $valueB= ref $value eq "ARRAY"
ABresult
0X0
100
111
37 0 $record->errors->add($attribute, $self->not_array_err, $options);
38 0 return;
39 }
40  
41 4 my $size = scalar(@$value);
42 4 ▶ $record->errors->add($attribute, $self->min_length_err, +{%$options, size=>$size, min=>$self->min_length})
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_min_length and $size < $self->min_length 0 4 4
Condition66%2/3
$self->has_min_length and $size < $self->min_length
!ll&&!rl&&r
310
MC/DC0%0/2
$self->has_min_length and $size < $self->min_length
$self->has_min_length $size < $self->min_length
Truth table66%2/3
A and B
A= $self->has_min_lengthB= $size < $self->min_length
ABresult
0X0
100
111
43 if $self->has_min_length and ($size < $self->min_length);
44  
45 4 ▶ $record->errors->add($attribute, $self->max_length_err, +{%$options, size=>$size, max=>$self->max_length})
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_max_length and $size > $self->max_length 0 4 4
Condition66%2/3
$self->has_max_length and $size > $self->max_length
!ll&&!rl&&r
310
MC/DC0%0/2
$self->has_max_length and $size > $self->max_length
$self->has_max_length $size > $self->max_length
Truth table66%2/3
A and B
A= $self->has_max_lengthB= $size > $self->max_length
ABresult
0X0
100
111
46 if $self->has_max_length and ($size > $self->max_length);
47  
48 4 my @validations = @{$self->validations};
49 my $validator = use_module($self->validator_class)
50 ->new(
51 14 validations => [[ [ map { "${attribute}[${_}]" } 0..$#$value ], @validations ]],
52 4 %{ $self->validator_class_args },
53 );
54  
55 4 my $result = $validator->validate($value, %$options);
56  
57 4 ▶ if($result->invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->invalid) 3 1 4
58 3 my $errors = $result->errors;
59 3 $errors->{__result} = $result; # hack to keep this in scope
60 3 $record->errors->add($attribute, $self->invalid_msg, $options);
61  
62 15 ▶ $errors->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 15 my ($index, $message) = @_;
64 15 $record->errors->add("${index}", $message);
65 3 });
66 }
67 }
68  
69 2 1;
70  
71 =head1 NAME
72  
73 Valiant::Validator::Array - Verify items in an arrayref.
74  
75 =head1 SYNOPSIS
76  
77 package Local::Test::Car;
78  
79 use Moo;
80 use Valiant::Validations;
81  
82 has ['make', 'model', 'year'] => (is=>'ro');
83  
84 validates make => ( inclusion => [qw/Toyota Tesla Ford/] );
85 validates model => ( length => [2, 20] );
86 validates year => ( numericality => { greater_than_or_equal_to => 1960 });
87  
88 package Local::Test::Array;
89  
90 use Moo;
91 use Valiant::Validations;
92  
93 has status => (is=>'ro');
94 has name => (is=>'ro');
95 has car => (is=>'ro');
96  
97 validates name => (length=>[2,5]);
98 validates car => ( array => { validations => [object=>1] } );
99 validates status => (
100 array => {
101 max_length => 3,
102 min_length => 1,
103 validations => [
104 inclusion => +{
105 in => [qw/active retired/],
106 },
107 ]
108 },
109 );
110  
111 my $car = Local::Test::Car->new(
112 make => 'Chevy',
113 model => '1',
114 year => 1900
115 );
116  
117 my $object = Local::Test::Array->new(
118 name => 'napiorkowski',
119 status => [qw/active running retired retired aaa bbb ccc active/],
120 car => [$car],
121 );
122  
123 $object->validate->invalid; # TRUE
124 $object->car->[0]->invalid; # TRUE
125  
126 # Error Messages
127  
128 my $all_errors = +{ $object->errors->to_hash(full_messages=>1) };
129  
130 # $all_errors = {
131 # car => [
132 # "Car Is Invalid",
133 # ],
134 # "car[0]" => [
135 # "Car Is Invalid",
136 # ],
137 # "car[0].make" => [
138 # "Car Make is not in the list",
139 # ],
140 # "car[0].model" => [
141 # "Car Model is too short (minimum is 2 characters)",
142 # ],
143 # "car[0].year" => [
144 # "Car Year must be greater than or equal to 1960",
145 # ],
146 # name => [
147 # "Name is too long (maximum is 5 characters)",
148 # ],
149 # status => [
150 # "Status Is Invalid",
151 # ],
152 # "status[1]" => [
153 # "Status is not in the list",
154 # ],
155 # "status[4]" => [
156 # "Status is not in the list",
157 # ],
158 # "status[5]" => [
159 # "Status is not in the list",
160 # ],
161 # "status[6]" => [
162 # "Status is not in the list",
163 # ],
164 # };
165
166 # Errors just on the car array item
167  
168 my $car_errors = +{ $object->car->[0]->errors->to_hash(full_messages=>1) };
169  
170 # $car_errors = {
171 # make => [
172 # "Make is not in the list",
173 # ],
174 # model => [
175 # "Model is too short (minimum is 2 characters)",
176 # ],
177 # year => [
178 # "Year must be greater than or equal to 1960",
179 # ],
180 # };
181  
182 =head1 DESCRIPTION
183  
184 Validations for arrays (really arrayrefs since that's how L<Moo> attrivbutes work).
185 Allows you to define validations on the array as a whole (such as set a maximum or
186 minimum array length) as well as define validations on the array individual items.
187 Can be used with the L<Valiant::Validator::Object> validator to deeply nest arrays of
188 objects.
189  
190 =head1 ATTRIBUTES
191  
192 This validator defines the following attributes
193  
194 =head2 max_length
195  
196 The maximum size of the array. For example "@a = (1,2,3)" has size of 3.
197  
198 =head2 min_length
199  
200 The minimum size of the array.
201  
202 =head2 max_length_err
203  
204 =head2 min_length_err
205  
206 The errors associated with the minimum or maximum array size errors. Defaults are translatio
207 tag 'max_length_err' and 'min_length_err'.
208  
209 =head2 invalid_msg
210  
211 The message returned when an array is generically invalid. An array becomes invalid should
212 any validations you define on array items fail to validate. Default is translation tag 'invalid'.
213  
214 =head2 validations
215  
216 An arrayref of validations that are run on each item in the list. Keep in mind the performance
217 inplications of this should the list be very long.
218  
219 =head2 validator_class
220  
221 Defaults to L<Valiant::Proxy::Array>, which value should be a subclass of. You probably
222 only need this if you are doing very custom validations.
223  
224 =head2 validator_class_args
225  
226 A hashref of args that get passed to the C<new> method of C<validator_class>.
227 Defaults to an empty hashref. You might need this if you build a custom validator
228 class and have special arguments it needs.
229  
230 =head2 not_array_err
231  
232 The error message used when the value under validation is not an arrayref at all.
233 Default is translation tag 'not_array_err'.
234  
235 =head1 SHORTCUT FORM
236  
237 This validator supports the follow shortcut forms:
238  
239 validates attribute => ( array => [ presence=>1, length=>[2,10] ], ... );
240  
241 Which is the same as:
242  
243 validates attribute => (
244 array => {
245 validations => [
246 presence => 1,
247 length => [2,10],
248 ],
249 },
250 );
251  
252 =head1 GLOBAL PARAMETERS
253  
254 This validator supports all the standard shared parameters: C<if>, C<unless>,
255 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
256  
257 =head1 SEE ALSO
258
259 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
260  
261 =head1 AUTHOR
262
263 See L<Valiant>
264
265 =head1 COPYRIGHT & LICENSE
266
267 See L<Valiant>
268  
269 =cut
« Valiant/Filter/Collection.pm ↑ Summary Valiant/Validator/OnlyOf.pm »