Verifier.pm

filter: Statementstmt 98.3% 179 / 182 Branchbran 87.8% 72 / 82 Conditioncond 84.6% 33 / 39 MC/DCmcdc 73.9% 17 / 23 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 92.6% 316 / 341 CC 62 SCAR 36.8
CC
62
Cov
94%
verify
53.9
_filter_value
8.0
__ANON__
3.0
CRAP 39.7
↑ Summary Verifier/Nested.pm »
1 package Data::Verifier;
2 17 $Data::Verifier::VERSION = '0.66';
3 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Profile based data verification with Moose type constraints.
6  
7 17 ▶use Data::Verifier::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Data::Verifier::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Data::Verifier::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 17 has 'derived' => (
17 is => 'ro',
18 isa => 'HashRef[HashRef]',
19 predicate => 'has_derived'
20 );
21  
22  
23 has 'filters' => (
24 is => 'ro',
25 isa => 'ArrayRef[Str|CodeRef]',
26 34 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
27 17 );
28  
29  
30  
31 17 has 'profile' => (
32 is => 'ro',
33 isa => 'HashRef[HashRef]',
34 required => 1
35 );
36  
37  
38 2 ▶sub coercion {
Subroutine coercion: called CC=1 SCAR=0.0
Pod documented
39 2 my %params = @_;
40 Moose::Meta::TypeCoercion->new(
41 type_coercion_map => [
42 2 $params{'from'} => $params{'via'}
43 ]
44 );
45 }
46  
47  
48 112 ▶sub verify {
Subroutine verify: called CC=53 SCAR=39.9
Pod documented
49 112 my ($self, $params, $members) = @_;
50  
51 112 my $results = Data::Verifier::Results->new;
52 112 my $profile = $self->profile;
53  
54 112 my $blessed_params = blessed($params);
55  
56 112 my @post_checks = ();
57 112 foreach my $key (keys(%{ $profile })) {
58 157 my $skip_string_checks = 0;
59  
60 # Get the profile part that is pertinent to this field
61 157 my $fprof = $profile->{$key};
62  
63 # Deal with the fact that what we're given may be an object.
64 157 my $val = do {
65 157 ▶ if($blessed_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed_params) { } 4 153 157
66 4 ▶ $params->can($key) ? $params->$key() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$params->can($key) ? : 3 1 4
67 } else {
68 153 $params->{$key};
69 }
70 };
71  
72 # Creat the "field" that we'll put into the result.
73 157 my $field = Data::Verifier::Field->new;
74  
75 # Save the original value.
76 157 ▶ if(ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 17 140 157
77 17 my @values = @{ $val }; # Make a copy of the array
78 17 $field->original_value(\@values);
79 } else {
80 140 $field->original_value($val);
81 }
82  
83 # Early type check to catch parameterized ArrayRefs.
84 157 ▶ if($fprof->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fprof->{"type"}) 105 52 157
85 105 my $cons = Moose::Util::TypeConstraints::find_or_parse_type_constraint($fprof->{type});
86  
87 105 ▶ die "Unknown type constraint '$fprof->{type}'" unless defined($cons);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cons 0 105 105
88  
89 # If this type is a paramterized arrayref, then we'll handle each
90 # param as if it was an independet value and run it through the
91 # whole profile.
92 105 ▶ if($cons->is_a_type_of('ArrayRef') && $cons->can('type_parameter')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cons->is_a_type_of("ArrayRef") and $cons->can("type_parameter")) 18 87 105
Condition66%2/3
$cons->is_a_type_of("ArrayRef") and $cons->can("type_parameter")
!ll&&!rl&&r
87018
MC/DC50%1/2
$cons->is_a_type_of("ArrayRef") and $cons->can("type_parameter")
$cons->is_a_type_of("ArrayRef") $cons->can("type_parameter")
Truth table66%2/3
A and B
A= $cons->is_a_type_of("ArrayRef")B= $cons->can("type_parameter")
ABresult
0X0
100
111
93  
94 # Get the type parameter for this arrayref
95 18 my $tc = $cons->type_parameter;
96 # Copy the profile.
97 18 my %prof_copy = %{ $fprof };
98 18 $prof_copy{type} = $tc->name;
99 18 my $dv = Data::Verifier->new(
100 # Use the global filters
101 filters => $self->filters,
102 # And JUST this field
103 profile => { $key => \%prof_copy }
104 );
105  
106 # Make sure we are dealing with an array
107 18 my @possibles;
108 18 ▶ if(ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 17 1 18
109 17 @possibles = @{ $val };
110 } else {
111 1 @possibles = ( $val );
112 }
113  
114 # So we can keep up with passed values.
115 18 my @passed;
116 my @pass_post_filter;
117 # Verify each one
118 18 foreach my $poss (@possibles) {
119 50 my $res = $dv->verify({ $key => $poss }, 1);
120 50 ▶ if($res->success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->success) { } 44 6 50
121 # We need to keep up with passed values as well as
122 # post filter values, copying them out of the result
123 # for use in our field.
124 44 push(@passed, $res->get_value($key));
125 44 push(@pass_post_filter, $res->get_post_filter_value($key));
126 } else {
127 # Mark the whole field as failed. We'll use this
128 # later.
129 6 $field->valid(0);
130 }
131 }
132  
133 # Set the value and post_filter_value for the field, then
134 # set the field in the result. We're done, since we sorta
135 # recursed to check all the params for this field.
136 18 $val = \@passed;
137 18 $field->value(\@passed);
138 18 $field->post_filter_value(\@pass_post_filter);
139 18 $results->set_field($key, $field);
140  
141 # Skip all the "string" checks, since we've already done all the
142 # real work.
143 18 $skip_string_checks = 1;
144 }
145 105 ▶ next unless $field->valid; # stop processing if invalid
Branch: 1 branch
BranchTrueFalseTotal
unless $field->valid 4 101 105
146 }
147  
148 153 ▶ unless($skip_string_checks) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_string_checks) 139 14 153
149 # Pass through global filters
150 139 ▶ if($self->filters ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filters) 139 0 139
151 139 $val = $self->_filter_value($self->filters, $val);
152 }
153  
154 # And now per-field ones
155 138 ▶ if($fprof->{filters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fprof->{"filters"}) 11 127 138
156 11 $val = $self->_filter_value($fprof->{filters}, $val);
157 }
158  
159 # Empty strings are undefined
160 138 ▶ if(defined($val) && $val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val eq "") 1 137 138
Condition100%3/3
defined $val and $val eq ""
!ll&&!rl&&r
101271
MC/DC100%2/2
defined $val and $val eq ""
defined $val $val eq ""
Truth table100%3/3
A and B
A= defined $valB= $val eq ""
ABresult
0X0
100
111
161 1 $val = undef;
162 }
163  
164 138 ▶ if(ref($val) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 138 138
165 0 my @values = @{ $val };
166 0 $field->post_filter_value(\@values);
167 } else {
168 138 $field->post_filter_value($val);
169 }
170  
171 138 ▶ if($fprof->{required} && !defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fprof->{'required'} and not defined $val) { } 8 130 138
Condition100%3/3
$fprof->{"required"} and not defined $val
!ll&&!rl&&r
62688
MC/DC100%2/2
$fprof->{"required"} and not defined $val
$fprof->{"required"} not defined $val
Truth table100%3/3
A and not(B)
A= $fprof->{"required"}B= not defined $val
ABresult
0X0
100
111
172 # Set required fields to undef, as they are missing
173 8 $results->set_field($key, undef);
174 } else {
175 130 $results->set_field($key, $field);
176 }
177  
178 # No sense in continuing if the value isn't defined.
179 138 ▶ next unless defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 11 127 138
180  
181 # Check min length
182 127 ▶ if($fprof->{min_length} && length($val) < $fprof->{min_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fprof->{"min_length"} and length $val < $fprof->{"min_length"}) 2 125 127
Condition100%3/3
$fprof->{"min_length"} and length $val < $fprof->{"min_length"}
!ll&&!rl&&r
12052
MC/DC100%2/2
$fprof->{"min_length"} and length $val < $fprof->{"min_length"}
$fprof->{"min_length"} length $val < $fprof->{"min_length"}
Truth table100%3/3
A and B
A= $fprof->{"min_length"}B= length $val < $fprof->{"min_length"}
ABresult
0X0
100
111
183 2 $field->reason('min_length');
184 2 $field->valid(0);
185 2 next; # stop processing!
186 }
187  
188 # Check max length
189 125 ▶ if($fprof->{max_length} && length($val) > $fprof->{max_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fprof->{"max_length"} and length $val > $fprof->{"max_length"}) 2 123 125
Condition100%3/3
$fprof->{"max_length"} and length $val > $fprof->{"max_length"}
!ll&&!rl&&r
11852
MC/DC100%2/2
$fprof->{"max_length"} and length $val > $fprof->{"max_length"}
$fprof->{"max_length"} length $val > $fprof->{"max_length"}
Truth table100%3/3
A and B
A= $fprof->{"max_length"}B= length $val > $fprof->{"max_length"}
ABresult
0X0
100
111
190 2 $field->reason('max_length');
191 2 $field->valid(0);
192 2 next; # stop processing!
193 }
194 }
195  
196 # Validate it
197 137 ▶ if($fprof->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fprof->{"type"}) 94 43 137
198 94 my $cons = Moose::Util::TypeConstraints::find_or_parse_type_constraint($fprof->{type});
199  
200 94 ▶ die "Unknown type constraint '$fprof->{type}'" unless defined($cons);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cons 0 94 94
201  
202 # Look for a global coercion
203 94 ▶ if($fprof->{coerce}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fprof->{'coerce'}) { } 2 92 94
elsif (my $coercion = $fprof->{'coercion'}) { } 2 90 92
204 2 $val = $cons->coerce($val);
205 }
206 # Try a one-off coercion.
207 elsif(my $coercion = $fprof->{coercion}) {
208 2 $val = $coercion->coerce($val);
209 }
210  
211 94 ▶ unless($cons->check($val)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cons->check($val)) 8 86 94
212 8 $field->reason('type_constraint');
213 8 $field->valid(0);
214 8 $field->clear_value;
215 8 next; # stop processing!
216 }
217 }
218  
219 # check for dependents
220 129 my $dependent = $fprof->{dependent};
221 129 my $dep_results;
222 129 ▶ if($dependent and !$members) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dependent and not $members) 8 121 129
Condition100%3/3
$dependent and not $members
!ll&&!rl&&r
108138
MC/DC100%2/2
$dependent and not $members
$dependent not $members
Truth table100%3/3
A and not(B)
A= $dependentB= not $members
ABresult
0X0
100
111
223 # Create a new verifier for use with the dependents
224 8 my $dep_verifier = Data::Verifier->new(
225 filters => $self->filters,
226 profile => $dependent
227 );
228 8 $dep_results = $dep_verifier->verify($params);
229  
230 # Merge the dependent's results with the parent one
231 8 $results->merge($dep_results);
232  
233 # If the dependent isn't valid, then this field isn't either
234 8 ▶ unless($dep_results->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dep_results->success) 1 7 8
235 1 $field->reason('dependent');
236 1 $field->valid(0);
237 1 $field->clear_value;
238 1 next; # stop processing!
239 }
240 }
241  
242 # Add this key the post check so we know to run through them
243 128 ▶ if(!$members && defined($fprof->{post_check}) && $fprof->{post_check}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $members and defined $fprof->{"post_check"} and $fprof->{"post_check"}) 12 116 128
Condition83%5/6
not $members and defined $fprof->{"post_check"} and $fprof->{"post_check"}
!ll&&!rl&&r
116012
not $members and defined $fprof->{"post_check"}
!ll&&!rl&&r
467012
MC/DC66%2/3
not $members and defined $fprof->{"post_check"} and $fprof->{"post_check"}
not $members defined $fprof->{"post_check"} $fprof->{"post_check"}
Truth table75%3/4
not(A) and B and C
A= not $membersB= defined $fprof->{"post_check"}C= $fprof->{"post_check"}
ABCresult
0XX0
10X0
1100
1111
244 12 push(@post_checks, $key);
245 }
246 # Add this key to the post check if we're on "member" mode and there
247 # is a member_post_check specified
248 128 ▶ if($members && defined($fprof->{member_post_check}) && $fprof->{member_post_check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($members and defined $fprof->{"member_post_check"} and $fprof->{"member_post_check"}) 10 118 128
Condition83%5/6
$members and defined $fprof->{"member_post_check"} and $fprof->{"member_post_check"}
!ll&&!rl&&r
118010
$members and defined $fprof->{"member_post_check"}
!ll&&!rl&&r
823610
MC/DC66%2/3
$members and defined $fprof->{"member_post_check"} and $fprof->{"member_post_check"}
$members defined $fprof->{"member_post_check"} $fprof->{"member_post_check"}
Truth table75%3/4
A and B and C
A= $membersB= defined $fprof->{"member_post_check"}C= $fprof->{"member_post_check"}
ABCresult
0XX0
10X0
1100
1111
249 10 push(@post_checks, $key);
250 }
251  
252 # Set the value
253 128 $field->value($val);
254 128 $field->valid(1);
255 }
256  
257 # If we have any post checks, do them.
258 111 ▶ if(scalar(@post_checks)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @post_checks) 20 91 111
259 20 foreach my $key (@post_checks) {
260 22 my $fprof = $profile->{$key};
261 22 my $field = $results->get_field($key);
262  
263 # Execute the post_check...
264  
265 # If we are in member mode, use the member post check, else use
266 # plain ol' post check.
267 22 ▶ my $pc = $members ? $fprof->{member_post_check} : $fprof->{post_check};
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 10 12 22
268 22 ▶ if(defined($pc) && $pc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pc and $pc) 22 0 22
Condition33%1/3
defined $pc and $pc
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $pc and $pc
defined $pc $pc
Truth table33%1/3
A and B
A= defined $pcB= $pc
ABresult
0X0
100
111
269 22 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
270 22 ▶ unless($results->$pc()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($results->$pc) 7 14 21
271 # If that returned false, then this field is invalid!
272 7 $field->clear_value;
273 7 ▶ $field->reason('post_check') unless $field->has_reason;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->has_reason 7 0 7
274 7 $field->valid(0);
275 }
276 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
277 1 $field->reason($_);
278 1 $field->clear_value;
279 1 $field->valid(0);
280 }
281 22 }
282 }
283 }
284  
285 111 ▶ if($self->has_derived) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_derived) 3 108 111
286 3 foreach my $key (keys(%{ $self->derived })) {
287 3 my $prof = $self->derived->{$key};
288 3 my $der = $prof->{deriver};
289 3 ▶ die "Derived fields must have a deriver!" unless defined($der);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $der 0 3 3
290 3 my $rv = $results->$der();
291  
292 3 my $req = $prof->{required};
293  
294 3 my $field = Data::Verifier::Field->new;
295 # If the field is required and we got back undef then this
296 # is a bad value!
297 3 ▶ if(defined($req) && $req && !defined($rv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req and $req and not defined $rv) { } 1 2 3
Condition83%5/6
defined $req and $req and not defined $rv
!ll&&!rl&&r
111
defined $req and $req
!ll&&!rl&&r
012
MC/DC66%2/3
defined $req and $req and not defined $rv
defined $req $req not defined $rv
Truth table75%3/4
A and B and not(C)
A= defined $reqB= $reqC= not defined $rv
ABCresult
0XX0
10X0
1100
1111
298 1 $field->valid(0);
299  
300 1 my $dfields = $prof->{fields};
301 1 foreach my $df (@{ $dfields }) {
302 2 my $f = $results->get_field($df);
303 2 ▶ die "Unknown field '$df' in derived field '$key'!" unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 2 2
304 2 $f->valid(0);
305 2 $f->value(undef);
306 2 $f->reason('derived');
307 }
308 } else {
309 # It's valid, set it to true and put the return value
310 # in.
311 2 $field->valid(1);
312 2 $field->value($rv);
313 2 $field->reason('derived');
314 }
315 3 $results->set_field($key, $field);
316 }
317 }
318  
319 111 return $results;
320 }
321  
322 150 ▶sub _filter_value {
Subroutine _filter_value: called CC=8 SCAR=20.8
323 150 my ($self, $filters, $values) = @_;
324  
325 150 my $created_ref = 0;
326 150 ▶ if(ref($filters) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filters ne "ARRAY") 4 146 150
327 4 $filters = [ $filters ];
328 }
329 # If we already have an array, just let it be. Otherwise transform the
330 # value into an array. ($values may also be a HashRef[Str] here)
331 150 ▶ unless ( ref $values eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $values eq "ARRAY") 150 0 150
332 150 $created_ref = 1;
333 150 $values = [ $values ];
334 }
335  
336 150 foreach my $f (@{ $filters }) {
337  
338 26 foreach my $value (@{ $values }) {
339 26 ▶ if(ref($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f) { } 7 19 26
340 7 $value = $value->$f($value);
341 } else {
342 19 ▶ die "Unknown filter: $f" unless Data::Verifier::Filters->can($f);
Branch: 1 branch
BranchTrueFalseTotal
unless "Data::Verifier::Filters"->can($f) 1 18 19
343 18 $value = Data::Verifier::Filters->$f($value);
344 }
345 }
346 }
347  
348 # Return an arrayref if we have multiple values or a scalar if we have one
349 149 ▶ return $created_ref ? $values->[0] : $values;
Branch: 1 branch
BranchTrueFalseTotal
$created_ref ? : 149 0 149
350 }
351  
352 17 __PACKAGE__->meta->make_immutable;
353  
354 17 1;
355  
356 __END__
↑ Summary Verifier/Nested.pm »