blib/lib/Validate/Simple.pm

filter: Statementstmt 95.9% 234 / 244 Branchbran 94.5% 121 / 128 Conditioncond 80.0% 84 / 105 MC/DCmcdc 60.9% 39 / 64 Subroutinesub 97.2% 35 / 36 Podpod 79.1% 19 / 24 totaltotal 88.5% 532 / 601 CC 109 SCAR 32.6
CC
109
Cov
92%
validate_value
54.4
validate_specs
25.2
_validate
10.0
CRAP 25.9
↑ Summary
1 package Validate::Simple;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = 'v0.4.1';
7  
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Scalar::Util qw/blessed looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Data::Types qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 my $VALUES_OF_ENUM = '__ VALUES __ OF __ ENUM __';
16 4 my $VALIDATE_OBJECT = '__ VALIDATE __ OBJECT __';
17  
18 # Constructor
19 #
20 1942 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
21 1942 my ( $class, $specs, $all_errors ) = @_;
22  
23 1942 ▶ if ( defined( $specs ) && ref( $specs ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $specs and ref $specs ne "HASH") 1 1941 1942
Condition100%3/3
defined $specs and ref $specs ne "HASH"
!ll&&!rl&&r
419371
MC/DC100%2/2
defined $specs and ref $specs ne "HASH"
defined $specs ref $specs ne "HASH"
Truth table100%3/3
A and B
A= defined $specsB= ref $specs ne "HASH"
ABresult
0X0
100
111
24 1 croak "Specification must be a hashref";
25 }
26  
27 1941 my $self = bless {
28 specs => $specs,
29 all_errors => !!$all_errors,
30 required => 0,
31 _errors => [],
32 }, $class;
33  
34 1941 ▶ if ( defined( $specs ) && keys( %$specs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $specs and keys %$specs) 1937 4 1941
Condition66%2/3
defined $specs and keys %$specs
!ll&&!rl&&r
401937
MC/DC50%1/2
defined $specs and keys %$specs
defined $specs keys %$specs
Truth table66%2/3
A and B
A= defined $specsB= keys %$specs
ABresult
0X0
100
111
35 $self->validate_specs( $specs, \$self->{required} )
36 1937 ▶ || croak "Specs is not valid: " . join( ";\n", $self->errors() );
Branch: 1 branch
BranchTrueFalseTotal
$self->validate_specs($specs, \$self->{"required"}) or croak('Specs is not valid: ' . join(";\n", $self->errors)) 1936 1 1937
37 }
38  
39 1940 return $self;
40 }
41  
42 # Register validation errors
43 #
44 # Pushes an error to the list of errors
45 #
46 2757 ▶sub _error {
Subroutine _error: called CC=2 SCAR=7.0
47 2757 my ( $self, $error ) = @_;
48  
49 2757 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2757 0 2757
50 2757 push @{ $self->{_errors} }, $error;
51 }
52  
53 2757 return;
54 }
55  
56  
57 # List of validatioon errors
58 #
59 7321 ▶sub errors {
Subroutine errors: called CC=2 SCAR=7.2
Pod documented
60 7321 ▶ return wantarray ? @{ $_[0]->{_errors} } : $_[0]->{_errors};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7321 0 7321
61 }
62  
63 # Deletes all collected errors and returns them
64 #
65 5381 ▶sub delete_errors {
Subroutine delete_errors: called CC=1 SCAR=0.0
Pod undocumented
66 5381 my ( $self ) = @_;
67 5381 my @errors = $self->errors();
68 5381 $self->{_errors} = [];
69 5381 return @errors;
70 }
71  
72  
73  
74 # Specifications of specification
75  
76 4 my @all_types = qw/
77 any
78 number
79 positive
80 non_negative
81 negative
82 non_positive
83 integer
84 positive_int
85 non_negative_int
86 negative_int
87 non_positive_int
88 string
89 array
90 hash
91 enum
92 code
93 spec
94 /;
95  
96 4 my @number_types = qw/
97 number
98 positive
99 non_negative
100 negative
101 non_positive
102 integer
103 positive_int
104 non_negative_int
105 negative_int
106 non_positive_int
107 /;
108  
109 4 my @string_types = ( 'string' );
110  
111 4 my @list_types = qw/
112 array
113 hash
114 /;
115  
116 4 my @enum_types = ( 'enum' );
117  
118 4 my @spec_types = ( 'spec' );
119  
120 # Common for all
121 4 my %any = (
122 required => {
123 type => 'any',
124 },
125 undef => {
126 type => 'any',
127 },
128 callback => {
129 type => 'code',
130 },
131 );
132  
133 # Common for numbers
134 4 my %number = (
135 gt => {
136 type => 'number',
137 },
138 ge => {
139 type => 'number',
140 },
141 lt => {
142 type => 'number',
143 },
144 le => {
145 type => 'number',
146 },
147 );
148  
149 # Common for strings
150 my %string = (
151 min_length => {
152 type => 'positive_int',
153 },
154 max_length => {
155 type => 'positive_int',
156 },
157 re => {
158 type => 'any',
159 120 ▶ callback => sub { ref( $_[0] ) eq 'Regexp' }
Subroutine __ANON__: called CC=1 SCAR=0.0
160 },
161 4 );
162  
163 # Common for lists
164 4 my %list = (
165 empty => {
166 type => 'any',
167 },
168 of => {
169 type => 'hash',
170 of => {
171 type => 'any',
172 },
173 required => 1,
174 },
175 );
176  
177 # Common for enums
178 4 my %enum = (
179 values => {
180 type => 'array',
181 of => {
182 type => 'string',
183 },
184 required => 1,
185 },
186 $VALUES_OF_ENUM => {
187 type => 'hash',
188 of => {
189 type => 'string',
190 undef => 1,
191 },
192 empty => 1,
193 },
194 );
195  
196 # Common for spec
197 my %spec = (
198 of => {
199 type => 'hash',
200 of => {
201 type => 'any',
202 },
203 required => 1,
204 empty => 0,
205 },
206 $VALIDATE_OBJECT => {
207 type => 'any',
208 255 ▶ callback => sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
209 # Must be an object that implements method 'validate'
210 255 ▶ return blessed( $_[0] ) && $_[0]->isa(__PACKAGE__);
Condition33%1/3
builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
00255
MC/DC0%0/2
builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table33%1/3
A && B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
211 },
212 },
213 4 );
214  
215 # Specification of specification format
216 my %specs_of_specs = (
217 8 ( map { $_ => { specs => { %any } } } qw/any code/ ),
218 40 ( map { $_ => { specs => { %any, %number } } } @number_types ),
219 4 ( map { $_ => { specs => { %any, %string } } } @string_types ),
220 8 ( map { $_ => { specs => { %any, %list } } } @list_types ),
221 4 ( map { $_ => { specs => { %any, %enum } } } @enum_types ),
222 4 ( map { $_ => { specs => { %any, %spec } } } @spec_types ),
223 );
224  
225 4 for my $key ( keys %specs_of_specs ) {
226 $specs_of_specs{ $key }{specs}{type} = {
227 68 type => 'enum',
228 # Here we need both 'values' and "$VALUES_OF_ENUM"
229 # to pass 'validate_specs'
230 values => [ $key ],
231 $VALUES_OF_ENUM => { $key => undef },
232 required => 1,
233 };
234 68 my $required = 0;
235 68 for my $k ( keys %{ $specs_of_specs{ $key }{specs} } ) {
236 $required++
237 476 ▶ if exists $specs_of_specs{ $key }{specs}{ $k }{required};
Branch: 1 branch
BranchTrueFalseTotal
if exists $specs_of_specs{$key}{"specs"}{$k}{"required"} 84 392 476
238 }
239 68 $specs_of_specs{ $key }{required} = $required;
240 }
241  
242 # Validate functions per type
243 4522 ▶$specs_of_specs{any}{validate} = sub { 1; };
Subroutine __ANON__: called CC=1 SCAR=0.0
244 4 for my $type ( @all_types ) {
245 68 ▶ next if $type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "any" 4 64 68
246 32735 ▶ $specs_of_specs{ $type }{validate} = sub { __PACKAGE__->$type( @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
247 }
248  
249 # Returns specification of specification
250 #
251 0 ▶sub spec_of_specs {
Subroutine spec_of_specs: not called CC=1 SCAR=6.9
Pod undocumented
252 0 return $specs_of_specs{ $_[1] };
253 }
254  
255 # Validates parameters according to specs
256 #
257 3600 ▶sub validate {
Subroutine validate: called CC=7 SCAR=19.5
Pod documented
258 3600 my ( $self, $params, $specs, $all_errors ) = @_;
259  
260 # Clear list of errors
261 3600 $self->delete_errors();
262  
263 3600 ▶ $all_errors //= $self->{all_errors};
Condition66%2/3
$all_errors //= $self->{"all_errors"}
l!l&&r!l&&!r
114824520
MC/DC0%0/2
$all_errors //= $self->{"all_errors"}
$all_errors $self->{"all_errors"}
Truth table66%2/3
A //= B
A= $all_errorsB= $self->{"all_errors"}
ABresult
1X1
011
000
264 3600 $all_errors = !!$all_errors;
265 3600 my $req = 0;
266 # If $specs is not passed, use the one created in constructor,
267 # and skip specs validation
268 3600 ▶ if ( !defined( $specs ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $specs) { } 1907 1693 3600
elsif (not $self->validate_specs($specs, \$req, '', $all_errors)) { } 6 1687 1693
269 1907 ▶ $specs //= $self->{specs};
Condition66%2/3
$specs //= $self->{"specs"}
l!l&&r!l&&!r
019052
MC/DC50%1/2
$specs //= $self->{"specs"}
$specs $self->{"specs"}
Truth table66%2/3
A //= B
A= $specsB= $self->{"specs"}
ABresult
1X1
011
000
270 # If $specs is still not known
271 # do not pass
272 1907 ▶ if ( !defined( $specs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $specs) 2 1905 1907
273 2 croak 'No specs passed';
274 }
275 1905 $req = $self->{required};
276 }
277 # Otherwise, validate specs first
278 elsif ( !$self->validate_specs( $specs, \$req, '', $all_errors ) ) {
279 6 croak 'Specs is not valid: ' . join( '; ', $self->errors() );
280 }
281  
282 # If params are not HASHREF or undef
283 # do not pass
284 3592 ▶ unless ( $self->hash( $params ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hash($params)) 129 3463 3592
285 129 $self->_error( "Expected a hashref for params" );
286 129 return;
287 }
288  
289 # Check parameters
290 3463 my $ret = $self->_validate( $params, $specs, \$req, $all_errors );
291  
292 3463 return $ret;
293 }
294  
295  
296 # Validate specs against predefined specs
297 #
298 # Here we consider specs as params and validate
299 # them against rules, which are stored in %specs_of_specs
300 #
301 5539 ▶sub validate_specs {
Subroutine validate_specs: called CC=23 SCAR=32.3
Pod undocumented
302 5539 my ( $self, $specs, $req, $path_to_var, $all_errors ) = @_;
303  
304 # This variable contains path to the variable name
305 5539 ▶ $path_to_var //= '';
Condition100%2/2
$path_to_var //= ""
l!l
35891950
MC/DC100%1/1
$path_to_var //= ""
$path_to_var
Truth table100%2/2
A
A= $path_to_var
Aresult
11
00
306 5539 ▶ $all_errors //= $self->{all_errors};
Condition66%2/3
$all_errors //= $self->{"all_errors"}
l!l&&r!l&&!r
358919500
MC/DC0%0/2
$all_errors //= $self->{"all_errors"}
$all_errors $self->{"all_errors"}
Truth table66%2/3
A //= B
A= $all_errorsB= $self->{"all_errors"}
ABresult
1X1
011
000
307 5539 $all_errors = !!$all_errors;
308  
309 5539 ▶ unless ( $self->hash( $specs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hash($specs)) 4 5535 5539
310 4 $self->_error( "Specs MUST be a hashref" );
311 4 return;
312 }
313  
314 5535 ▶ if ( !$req ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req) 0 5535 5535
315 0 $self->_error( "No variable to remember amount of required params" );
316 0 return;
317 }
318  
319 5535 ▶ if ( ref( $req ) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $req ne "SCALAR") 0 5535 5535
320 0 $self->_error( "Scalar reference is expected" );
321 }
322  
323 5535 for my $variable ( keys %$specs ) {
324 16390 my $spec = $specs->{ $variable };
325 16390 my $p2v = "$path_to_var/$variable";
326 16390 ▶ unless ( $self->hash( $spec ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hash($spec)) 3 16387 16390
327 3 $self->_error( "Each spec MUST be a hashref: $p2v" );
328 3 return;
329 }
330 my $type = exists( $spec->{type} )
331 ? $spec->{type}
332 16387 ▶ : ( $spec->{type} = 'any' );
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'type'} ? : 16381 6 16387
333  
334 # Known type?
335 16387 ▶ if ( !exists $specs_of_specs{ $type }{specs} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $specs_of_specs{$type}{"specs"}) 3 16384 16387
336 3 $self->_error( "Unknown type '$type' in specs of $p2v" );
337 3 return;
338 }
339  
340  
341 # Validate spec
342 16384 my $spec_of_spec = $specs_of_specs{ $type }{specs};
343 16384 ▶ if ( !$self->_validate( $spec, $spec_of_spec, \$specs_of_specs{ $type }{required} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_validate($spec, $spec_of_spec, \$specs_of_specs{$type}{"required"})) 5 16379 16384
344 5 $self->_error( "Bad spec for variable $p2v, should be " . Dumper( $spec_of_spec ) );
345 5 return;
346 }
347  
348 # Transform enum values into a hash
349 16379 ▶ if ( $type eq 'enum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum") 126 16253 16379
350 $spec->{ $VALUES_OF_ENUM }{$_} = undef
351 126 for @{ $spec->{values} };
352 }
353  
354 # Subspec
355 16379 ▶ if ( $type eq 'spec' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'spec') { } 263 16116 16379
elsif (exists $spec->{'of'} and not $self->validate_specs({'of', $spec->{'of'}}, \(my $r = 0), $p2v, $all_errors)) { } 0 30336 30336
356 263 my $create_error;
357 my $vs = eval {
358 263 blessed( $self )->new( $spec->{of}, $all_errors );
359 263 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $vs = eval { do { builtin::blessed($self)->new($spec->{"of"}, $all_errors) } } or do { $create_error = $@ || "Zombie error" } 262 1 263
360 1 ▶ $create_error = $@ || 'Zombie error';
Condition50%1/2
$@ || 'Zombie error'
l!l
10
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
361 };
362 263 ▶ if ( $create_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create_error) 1 262 263
363 1 $self->_error( $create_error );
364 1 return;
365 }
366 262 my @errors = $vs->delete_errors();
367 262 ▶ if ( @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 262 262
368 $self->_error( "Subspec '$p2v' is invalid: $_" )
369 0 for @errors;
370 0 return;
371 }
372 262 $spec->{ $VALIDATE_OBJECT } = $vs;
373 }
374 ▶ elsif ( exists( $spec->{of} ) && !$self->validate_specs( { of => $spec->{of} }, \( my $r = 0), $p2v, $all_errors ) ) {
Condition66%2/3
exists $spec->{"of"} and not $self->validate_specs({"of", $spec->{"of"}}, \(my $r = 0), $p2v, $all_errors)
!ll&&!rl&&r
1422018960
MC/DC0%0/2
exists $spec->{"of"} and not $self->validate_specs({"of", $spec->{"of"}}, \(my $r = 0), $p2v, $all_errors)
exists $spec->{"of"} not $self->validate_specs({"of", $spec->{"of"}}, \(my $r = 0), $p2v, $all_errors)
Truth table66%2/3
A and not(B)
A= exists $spec->{"of"}B= not $self->validate_specs({"of", $spec->{"of"}}, \(my $r = 0), $p2v, $all_errors)
ABresult
0X0
100
111
375 0 $self->_error( "Bad 'of' spec for variable $p2v" );
376 0 return;
377 }
378  
379 # Calculate amount of rerquired params
380 $$req++
381 16378 ▶ if exists( $spec->{required} ) && $spec->{required};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"required"} and $spec->{"required"} 34 16344 16378
Condition100%3/3
exists $spec->{"required"} and $spec->{"required"}
!ll&&!rl&&r
16343134
MC/DC100%2/2
exists $spec->{"required"} and $spec->{"required"}
exists $spec->{"required"} $spec->{"required"}
Truth table100%3/3
A and B
A= exists $spec->{"required"}B= $spec->{"required"}
ABresult
0X0
100
111
382 }
383  
384 5523 return 1;
385 }
386  
387 # Actual validation of parameters
388 #
389 19847 ▶sub _validate {
Subroutine _validate: called CC=10 SCAR=23.0
390 19847 my ( $self, $params, $specs, $required, $all_errors ) = @_;
391  
392 19847 my $req = $$required;
393 19847 for my $name ( keys %$params ) {
394 33228 ▶ if ( !exists $specs->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $specs->{$name}) 20 33208 33228
395 20 $self->_error( "Unknown param '$name':\n"
396 . Dumper($params) . "\n"
397 . Dumper($specs)
398 );
399 20 ▶ if ( $all_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_errors) { } 6 14 20
400 6 next;
401 }
402 else {
403 14 last;
404 }
405 }
406  
407 33208 my $value = $params->{ $name };
408 33208 my $spec = $specs->{ $name };
409 $req--
410 33208 ▶ if exists( $spec->{required} ) && $spec->{required};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"required"} and $spec->{"required"} 18695 14513 33208
Condition66%2/3
exists $spec->{"required"} and $spec->{"required"}
!ll&&!rl&&r
14513018695
MC/DC50%1/2
exists $spec->{"required"} and $spec->{"required"}
exists $spec->{"required"} $spec->{"required"}
Truth table66%2/3
A and B
A= exists $spec->{"required"}B= $spec->{"required"}
ABresult
0X0
100
111
411  
412 33208 my $valid = $self->validate_value( "/$name", $value, $spec, $all_errors );
413 33208 ▶ last if !$valid && !$all_errors;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid or $all_errors 1662 31546 33208
Condition100%3/3
$valid or $all_errors
l!l&&r!l&&!r
307318151662
MC/DC100%2/2
$valid or $all_errors
$valid $all_errors
Truth table100%3/3
A or B
A= $validB= $all_errors
ABresult
1X1
011
000
414 }
415  
416 # Not all required params found
417 # Check, what is missing
418 19847 ▶ if ( $req ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req) 7 19840 19847
419 7 $self->required_params( $params, $specs );
420 }
421  
422 19847 ▶ return @{ $self->{_errors} } ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'_errors'};} ? : 2547 17300 19847
423 }
424  
425 # Checks whether all required params exist
426 #
427 7 ▶sub required_params {
Subroutine required_params: called CC=5 SCAR=16.1
Pod undocumented
428 7 my ( $self, $params, $specs ) = @_;
429  
430 7 for my $par ( keys %$specs ) {
431 12 my $spec = $specs->{ $par };
432 12 ▶ if ( exists( $spec->{required} ) && $spec->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"required"} and $spec->{"required"}) 8 4 12
Condition66%2/3
exists $spec->{"required"} and $spec->{"required"}
!ll&&!rl&&r
408
MC/DC50%1/2
exists $spec->{"required"} and $spec->{"required"}
exists $spec->{"required"} $spec->{"required"}
Truth table66%2/3
A and B
A= exists $spec->{"required"}B= $spec->{"required"}
ABresult
0X0
100
111
433 8 ▶ if ( !exists $params->{ $par } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{$par}) 7 1 8
434 7 $self->_error( "Required param '$par' does not exist" );
435 }
436 }
437 }
438  
439 7 return;
440 }
441  
442 # Valdates value against spec
443 #
444 37604 ▶sub validate_value {
Subroutine validate_value: called CC=53 SCAR=40.0
Pod undocumented
445 37604 my ( $self, $name, $value, $spec, $all_errors ) = @_;
446  
447 37604 ▶ my $type = $spec->{type} || 'any';
Condition50%1/2
$spec->{'type'} || 'any'
l!l
376040
MC/DC0%0/1
$spec->{'type'} || 'any'
$spec->{'type'}
Truth table50%1/2
A
A= $spec->{'type'}
Aresult
11
00
448 37604 ▶ my $undef = exists( $spec->{undef} ) && $spec->{undef};
Condition66%2/3
exists $spec->{'undef'} && $spec->{'undef'}
!ll&&!rl&&r
370630541
MC/DC50%1/2
exists $spec->{'undef'} && $spec->{'undef'}
exists $spec->{'undef'} $spec->{'undef'}
Truth table66%2/3
A && B
A= exists $spec->{'undef'}B= $spec->{'undef'}
ABresult
0X0
100
111
449  
450 # If undef value is allowed and the value is undefined
451 # do not perform any furrther validation
452 37604 ▶ if ( $undef && !defined( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undef and not defined $value) 347 37257 37604
Condition100%3/3
$undef and not defined $value
!ll&&!rl&&r
37063194347
MC/DC100%2/2
$undef and not defined $value
$undef not defined $value
Truth table100%3/3
A and not(B)
A= $undefB= not defined $value
ABresult
0X0
100
111
453 347 return 1;
454 }
455  
456 37257 my @other = ();
457 # Enum
458 37257 ▶ if ( $type eq 'enum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum") 16440 20817 37257
459 16440 push @other, $spec->{ $VALUES_OF_ENUM };
460 }
461  
462 # Spec
463 37257 ▶ if ( $type eq 'spec' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "spec") 163 37094 37257
464 163 push @other, $spec->{ $VALIDATE_OBJECT };
465 }
466  
467 # Check type
468 37257 ▶ unless ( $specs_of_specs{ $type }{validate}->( $value, @other ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($specs_of_specs{$type}{"validate"}->($value, @other)) 2108 35149 37257
469 2108 ▶ if ( $type eq 'spec' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'spec') { } 139 1969 2108
470 $self->_error( $name . ( /^\// ? '' : ': ' ) . $_ )
471 139 ▶ for $other[0]->errors();
Branch: 1 branch
BranchTrueFalseTotal
m[^/] ? : 106 45 151
472 } else {
473 my $expl = $type eq 'enum'
474 1969 ▶ ? ( ": " . Dumper( $spec->{values} ) )
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'enum' ? : 36 1933 1969
475 : '';
476 1969 ▶ $self->_error( "$name: >> " . ( $value // '[undef]') . " << is not of type '$type'$expl" );
Condition100%2/2
$value // "[undef]"
l!l
1645324
MC/DC100%1/1
$value // "[undef]"
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
477 }
478 2108 return;
479 }
480  
481 # Check greater than
482 35149 ▶ if ( exists $spec->{gt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"gt"}) 176 34973 35149
483 176 ▶ if ( $spec->{gt} >= $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"gt"} >= $value) 101 75 176
484 101 ▶ $self->_error( "$name: " . ( $value // '[undef]') . " > $spec->{gt} returns false" );
Condition50%1/2
$value // "[undef]"
l!l
1010
MC/DC0%0/1
$value // "[undef]"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
485 101 return;
486 }
487 }
488  
489 # Check greater or equal
490 35048 ▶ if ( exists $spec->{ge} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"ge"}) 162 34886 35048
491 162 ▶ if ( $spec->{ge} > $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"ge"} > $value) 30 132 162
492 30 ▶ $self->_error( "$name: " . ( $value // '[undef]') . " >= $spec->{ge} returns false" );
Condition50%1/2
$value // "[undef]"
l!l
300
MC/DC0%0/1
$value // "[undef]"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
493 30 return;
494 }
495 }
496  
497 # Check less than
498 35018 ▶ if ( exists $spec->{lt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"lt"}) 204 34814 35018
499 204 ▶ if ( $spec->{lt} <= $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"lt"} <= $value) 132 72 204
500 132 ▶ $self->_error( "$name: " . ( $value // '[undef]') . " < $spec->{lt} returns false" );
Condition50%1/2
$value // "[undef]"
l!l
1320
MC/DC0%0/1
$value // "[undef]"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
501 132 return;
502 }
503 }
504  
505 # Check less or equal
506 34886 ▶ if ( exists $spec->{le} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"le"}) 186 34700 34886
507 186 ▶ if ( $spec->{le} < $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"le"} < $value) 72 114 186
508 72 ▶ $self->_error( "$name: " . ( $value // '[undef]') . " <= $spec->{le} returns false" );
Condition50%1/2
$value // "[undef]"
l!l
720
MC/DC0%0/1
$value // "[undef]"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
509 72 return;
510 }
511 }
512  
513 # Check min length
514 34814 ▶ if ( exists $spec->{min_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"min_length"}) 48 34766 34814
515 48 ▶ if ( $spec->{min_length} > length( $value // '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"min_length"} > length($value // "")) 24 24 48
Condition50%1/2
$value // ""
l!l
480
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
516 24 ▶ $self->_error( "$name: length('" . ( $value // '[undef]') . "') > $spec->{min_length} returns false" );
Condition50%1/2
$value // "[undef]"
l!l
240
MC/DC0%0/1
$value // "[undef]"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
517 24 return;
518 }
519 }
520  
521 # Check max length
522 34790 ▶ if ( exists $spec->{max_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"max_length"}) 48 34742 34790
523 48 ▶ if ( $spec->{max_length} < length( $value // '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"max_length"} < length($value // "")) 24 24 48
Condition50%1/2
$value // ""
l!l
480
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
524 24 ▶ $self->_error( "$name: length('" . ( $value // '[undef]') . "') < $spec->{max_length} returns false" );
Condition50%1/2
$value // "[undef]"
l!l
240
MC/DC0%0/1
$value // "[undef]"
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
525 24 return;
526 }
527 }
528  
529 # Check re
530 34766 ▶ if ( exists $spec->{re} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"re"}) 18 34748 34766
531 18 ▶ if ( $value !~ $spec->{re} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$spec->{"re"}/) 12 6 18
532 12 $self->_error( "$name: '$value' does not match the Regexp $spec->{re}" );
533 12 return;
534 }
535 }
536  
537 # Check of
538 34754 $all_errors = !!$all_errors;
539 34754 ▶ if ( exists $spec->{of} && $type ne 'spec' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"of"} and $type ne "spec") 2848 31906 34754
Condition100%3/3
exists $spec->{"of"} and $type ne "spec"
!ll&&!rl&&r
31882242848
MC/DC100%2/2
exists $spec->{"of"} and $type ne "spec"
exists $spec->{"of"} $type ne "spec"
Truth table100%3/3
A and B
A= exists $spec->{"of"}B= $type ne "spec"
ABresult
0X0
100
111
540 2848 ▶ if ( $type eq 'array' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'array') { } 403 2445 2848
elsif ($type eq 'hash') { } 2445 0 2445
541 403 my $arr_size = $#$value;
542 403 ▶ for ( my $i = 0; $i <= $arr_size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $arr_size) 927 322 1249
543 my $is_valid = $self->validate_value(
544 "$name/$i",
545 $value->[$i],
546 $spec->{of},
547 927 $all_errors,
548 );
549 927 ▶ if ( !$is_valid && !$all_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid or $all_errors) 81 846 927
Condition100%3/3
$is_valid or $all_errors
l!l&&r!l&&!r
7786881
MC/DC100%2/2
$is_valid or $all_errors
$is_valid $all_errors
Truth table100%3/3
A or B
A= $is_validB= $all_errors
ABresult
1X1
011
000
550 81 return;
551 }
552 }
553 }
554 elsif ( $type eq 'hash' ) {
555 2445 for my $key ( keys %$value ) {
556 my $is_valid = $self->validate_value(
557 "$name/$key",
558 $value->{$key},
559 $spec->{of},
560 3469 $all_errors,
561 );
562 3469 ▶ if ( !$is_valid && !$all_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid or $all_errors) 48 3421 3469
Condition100%3/3
$is_valid or $all_errors
l!l&&r!l&&!r
33932848
MC/DC100%2/2
$is_valid or $all_errors
$is_valid $all_errors
Truth table100%3/3
A or B
A= $is_validB= $all_errors
ABresult
1X1
011
000
563 48 return;
564 }
565 }
566 }
567 else {
568 0 $self->_error( "$name: " . "Cannot set elements types for $type" );
569 0 return;
570 }
571  
572 2719 ▶ if ( ( $type eq 'array' && !@$value )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array" and not @$value or $type eq "hash" and not %$value) 78 2641 2719
Condition100%9/9
$type eq "array" and not @$value or $type eq "hash" and not %$value
l!l&&r!l&&!r
48302641
$type eq "array" and not @$value
!ll&&!rl&&r
239727448
$type eq "hash" and not %$value
!ll&&!rl&&r
274236730
MC/DC75%3/4
$type eq "array" and not @$value or $type eq "hash" and not %$value
$type eq "array" not @$value $type eq "hash" not %$value
Truth table57%4/7
A and not(B) or C and not(D)
A= $type eq "array"B= not @$valueC= $type eq "hash"D= not %$value
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
573 || ( $type eq 'hash' && !%$value ) ) {
574 78 ▶ if ( !exists( $spec->{empty} ) || !$spec->{empty} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $spec->{"empty"} and $spec->{"empty"}) 58 20 78
Condition66%2/3
exists $spec->{"empty"} and $spec->{"empty"}
!ll&&!rl&&r
58020
MC/DC50%1/2
exists $spec->{"empty"} and $spec->{"empty"}
exists $spec->{"empty"} $spec->{"empty"}
Truth table66%2/3
A and B
A= exists $spec->{"empty"}B= $spec->{"empty"}
ABresult
0X0
100
111
575 58 $self->_error( "$name: " . ucfirst( $type ) . " cannot be empty" );
576 58 return;
577 }
578 }
579 }
580  
581 # Check code
582 34567 ▶ if ( exists $spec->{callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"callback"}) 399 34168 34567
583 399 ▶ if ( !$spec->{callback}->( $value, @other ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{"callback"}->($value, @other)) 12 387 399
584 12 $self->_error("Callback returned false");
585 12 return;
586 }
587 }
588 34555 return 1;
589 }
590  
591  
592  
593 # Primitives
594 # ==========
595 10493 ▶sub number {
Subroutine number: called CC=1 SCAR=0.0
Pod documented
596 10493 return looks_like_number( $_[1] );
597 }
598  
599 210 ▶sub positive {
Subroutine positive: called CC=2 SCAR=6.9
Pod documented
600 210 ▶ return $_[0]->number( $_[1] ) && $_[1] > 0;
Condition100%3/3
$_[0]->number($_[1]) && $_[1] > 0
!ll&&!rl&&r
924474
MC/DC100%2/2
$_[0]->number($_[1]) && $_[1] > 0
$_[0]->number($_[1]) $_[1] > 0
Truth table100%3/3
A && B
A= $_[0]->number($_[1])B= $_[1] > 0
ABresult
0X0
100
111
601 }
602  
603 229 ▶sub non_negative {
Subroutine non_negative: called CC=2 SCAR=6.9
Pod documented
604 229 ▶ return $_[0]->number( $_[1] ) && $_[1] >= 0;
Condition100%3/3
$_[0]->number($_[1]) && $_[1] >= 0
!ll&&!rl&&r
9231106
MC/DC100%2/2
$_[0]->number($_[1]) && $_[1] >= 0
$_[0]->number($_[1]) $_[1] >= 0
Truth table100%3/3
A && B
A= $_[0]->number($_[1])B= $_[1] >= 0
ABresult
0X0
100
111
605 }
606  
607 228 ▶sub negative {
Subroutine negative: called CC=2 SCAR=6.9
Pod documented
608 228 ▶ return $_[0]->number( $_[1] ) && $_[1] < 0;
Condition100%3/3
$_[0]->number($_[1]) && $_[1] < 0
!ll&&!rl&&r
926274
MC/DC100%2/2
$_[0]->number($_[1]) && $_[1] < 0
$_[0]->number($_[1]) $_[1] < 0
Truth table100%3/3
A && B
A= $_[0]->number($_[1])B= $_[1] < 0
ABresult
0X0
100
111
609 }
610  
611 229 ▶sub non_positive {
Subroutine non_positive: called CC=2 SCAR=6.9
Pod documented
612 229 ▶ return $_[0]->number( $_[1] ) && $_[1] <= 0;
Condition100%3/3
$_[0]->number($_[1]) && $_[1] <= 0
!ll&&!rl&&r
9231106
MC/DC100%2/2
$_[0]->number($_[1]) && $_[1] <= 0
$_[0]->number($_[1]) $_[1] <= 0
Truth table100%3/3
A && B
A= $_[0]->number($_[1])B= $_[1] <= 0
ABresult
0X0
100
111
613 }
614  
615 861 ▶sub integer {
Subroutine integer: called CC=1 SCAR=0.0
Pod documented
616 861 return is_int( $_[1] );
617 }
618  
619 677 ▶sub positive_int {
Subroutine positive_int: called CC=1 SCAR=0.0
Pod documented
620 677 return is_count( $_[1] );
621 }
622  
623 240 ▶sub non_negative_int {
Subroutine non_negative_int: called CC=1 SCAR=0.0
Pod documented
624 240 return is_whole( $_[1] );
625 }
626  
627 275 ▶sub negative_int {
Subroutine negative_int: called CC=2 SCAR=6.9
Pod documented
628 275 ▶ return $_[0]->integer( $_[1] ) && $_[1] < 0;
Condition100%3/3
$_[0]->integer($_[1]) && $_[1] < 0
!ll&&!rl&&r
1246388
MC/DC100%2/2
$_[0]->integer($_[1]) && $_[1] < 0
$_[0]->integer($_[1]) $_[1] < 0
Truth table100%3/3
A && B
A= $_[0]->integer($_[1])B= $_[1] < 0
ABresult
0X0
100
111
629 }
630  
631 276 ▶sub non_positive_int {
Subroutine non_positive_int: called CC=2 SCAR=6.9
Pod documented
632 276 ▶ return $_[0]->integer( $_[1] ) && $_[1] <= 0;
Condition100%3/3
$_[0]->integer($_[1]) && $_[1] <= 0
!ll&&!rl&&r
12432120
MC/DC100%2/2
$_[0]->integer($_[1]) && $_[1] <= 0
$_[0]->integer($_[1]) $_[1] <= 0
Truth table100%3/3
A && B
A= $_[0]->integer($_[1])B= $_[1] <= 0
ABresult
0X0
100
111
633 }
634  
635 17047 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
636 17047 return is_string( $_[1] );
637 }
638  
639 552 ▶sub array {
Subroutine array: called CC=1 SCAR=0.0
Pod documented
640 552 return ref( $_[1] ) eq 'ARRAY';
641 }
642  
643 28132 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
644 28132 return ref( $_[1] ) eq 'HASH';
645 }
646  
647 16446 ▶sub enum {
Subroutine enum: called CC=2 SCAR=6.9
Pod documented
648 16446 ▶ return $_[0]->string( $_[1] ) && exists $_[2]->{$_[1]};
Condition100%3/3
$_[0]->string($_[1]) && exists $_[2]{$_[1]}
!ll&&!rl&&r
33716406
MC/DC100%2/2
$_[0]->string($_[1]) && exists $_[2]{$_[1]}
$_[0]->string($_[1]) exists $_[2]{$_[1]}
Truth table100%3/3
A && B
A= $_[0]->string($_[1])B= exists $_[2]{$_[1]}
ABresult
0X0
100
111
649 }
650  
651 218 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod documented
652 218 return ref( $_[1] ) eq 'CODE';
653 }
654  
655 290 ▶sub spec {
Subroutine spec: called CC=1 SCAR=0.0
Pod documented
656 290 return $_[2]->validate( $_[1] );
657 }
658  
659 4 1;
660  
661  
662 __END__
↑ Summary