blib/lib/Params/Validate/Strict.pm

filter: Statementstmt 90.7% 924 / 1018 Branchbran 93.3% 905 / 970 Conditioncond 84.8% 453 / 534 MC/DCmcdc 73.7% 225 / 305 Subroutinesub 100% 33 / 33 Podpod 100% 2 / 2 totaltotal 88.8% 2542 / 2862 CC 692 SCAR 65.7
CC
692
Cov
90%
validate_strict
932.6
_compile_field
43.0
_validate_value_constraint
16.2
CRAP 711.3
↑ Summary blib/lib/Params/Validate/Strict/BNF.pm »
1 package Params::Validate::Strict;
2  
3 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 36 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶use Exporter qw(import); # Required for @EXPORT_OK
Subroutine BEGIN: called CC=1 SCAR=0.0
8 36 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 36 ▶use List::Util 1.33 qw(all any); # Required for memberof/matches validation
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use Readonly::Values::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 36 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 36 our @ISA = qw(Exporter);
14 36 our @EXPORT_OK = qw(validate_strict compile_schema);
15  
16 =head1 NAME
17  
18 Params::Validate::Strict - Validates a set of parameters against a schema
19  
20 =head1 VERSION
21  
22 Version 0.43
23  
24 =cut
25  
26 36 our $VERSION = '0.43';
27  
28 # Recursion depth counter - localised on every entry so it unwinds automatically.
29 # Protects against mutations (or bugs) that turn the pipe-normalisation guard into
30 # an infinite loop through the union-type ARRAY handler.
31 36 our $_depth = 0;
32  
33 # Type codes used by compiled schemas (see _compile_schema). Numeric codes
34 # keep the per-call dispatch to integer comparisons.
35 36 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 36 _T_STRING => 1,
37 _T_INT => 2,
38 _T_NUM => 3,
39 _T_BOOL => 4,
40 _T_ARRAY => 5,
41 _T_HASH => 6,
42 _T_SCALAR => 7,
43 _T_CODE => 8,
44 _T_OBJECT => 9,
45 36 };
46  
47 =head1 SYNOPSIS
48  
49 my $schema = {
50 username => { type => 'string', min => 3, max => 50 },
51 age => { type => 'integer', min => 0, max => 150 },
52 };
53  
54 my $input = {
55 username => 'john_doe',
56 age => '30', # Will be coerced to integer
57 };
58  
59 my $validated_input = validate_strict(schema => $schema, input => $input);
60  
61 if(defined($validated_input)) {
62 print "Example 1: Validation successful!\n";
63 print 'Username: ', $validated_input->{username}, "\n";
64 print 'Age: ', $validated_input->{age}, "\n"; # It's an integer now
65 } else {
66 print "Example 1: Validation failed: $@\n";
67 }
68  
69 Upon first reading this may seem overly complex and full of scope creep in a sledgehammer to crack a nut sort of way,
70 however two use cases make use of the extensive logic that comes with this code
71 and I have a couple of other reasons for writing it.
72  
73 =over 4
74  
75 =item * Black Box Testing
76  
77 The schema can be plumbed into L<App::Test::Generator> to automatically create a set of black-box test cases.
78  
79 =item * WAF
80  
81 The schema can be plumbed into a WAF,
82 e.g., L<VWF|https://github.com/nigelhorne/VWF/>,
83 to protect from random user input.
84  
85 =item * Improved API Documentation
86  
87 Even if you don't use this module,
88 the specification syntax can help with documentation.
89  
90 =item * I like it
91  
92 I found it fun to write this,
93 even if nobody else finds it useful,
94 though I hope you will.
95  
96 =back
97  
98 =head1 METHODS
99  
100 =head2 validate_strict
101  
102 Validates a set of parameters against a schema.
103  
104 This function takes two mandatory arguments:
105  
106 =over 4
107  
108 =item * C<schema> || C<members>
109  
110 A reference to a hash that defines the validation rules for each parameter.
111 The keys of the hash are the parameter names, and the values are either a string representing the parameter type or a reference to a hash containing more detailed rules.
112  
113 As an alternative the schema may be supplied as an B<arrayref of parameter hashrefs>,
114 where every element describes one parameter and carries a mandatory
115 C<name> key:
116  
117 $schema = [
118 { name => 'username', type => 'string', min => 3, max => 50 },
119 { name => 'age', type => 'integer', min => 0, max => 150 },
120 { name => 'role', type => 'string', optional => 1, default => 'user' },
121 ];
122  
123 The arrayref form is normalised to the standard hashref form before any further
124 processing. It is particularly useful when declaration order matters (e.g.
125 for positional or mixed calling conventions used by some CPAN modules). The
126 C<name> key is consumed during normalisation and does not appear as a
127 validation rule.
128  
129 For some sort of compatibility with L<Data::Processor>,
130 it is possible to wrap the schema within a hash like this:
131  
132 $schema = {
133 description => 'Describe what this schema does',
134 error_msg => 'An error message',
135 schema => {
136 # ... schema goes here
137 }
138 }
139  
140 =item * C<args> || C<input>
141  
142 A reference to a hash containing the parameters to be validated.
143 The keys of the hash are the parameter names, and the values are the parameter values.
144  
145 =back
146  
147 It takes optional arguments:
148  
149 =over 4
150  
151 =item * C<description>
152  
153 What the schema does,
154 used in error messages.
155  
156 =item * C<error_msg>
157  
158 Overrides the default message when something doesn't validate.
159  
160 =item * C<unknown_parameter_handler>
161  
162 This parameter describes what to do when a parameter is given that is not in the schema of valid parameters.
163 It must be one of C<die>, C<warn>, or C<ignore>.
164  
165 It defaults to C<die> unless C<carp_on_warn> is given, in which case it defaults to C<warn>.
166  
167 =item * C<logger>
168  
169 A logging object that understands messages such as C<error> and C<warn>.
170  
171 =item * C<custom_types>
172  
173 A reference to a hash that defines reusable custom types.
174 Custom types allow you to define validation rules once and reuse them throughout your schema,
175 making your validation logic more maintainable and readable.
176  
177 Each custom type is defined as a hash reference containing the same validation rules available for regular parameters
178 (C<type>, C<min>, C<max>, C<matches>, C<memberof>, C<values>, C<enum>, C<notmemberof>, C<callback>, etc.).
179  
180 my $custom_types = {
181 email => {
182 type => 'string',
183 matches => qr/^[^@\s]+\@[^@\s]+\.[^@\s]+$/,
184 error_msg => 'Invalid email address format'
185 }, phone => {
186 type => 'string',
187 matches => qr/^\+?[1-9]\d{1,14}$/,
188 min => 10,
189 max => 15
190 }, percentage => {
191 type => 'number',
192 min => 0,
193 max => 100
194 }, status => {
195 type => 'string',
196 memberof => ['draft', 'published', 'archived']
197 }
198 };
199  
200 my $schema = {
201 user_email => { type => 'email' },
202 contact_number => { type => 'phone', optional => 1 },
203 completion => { type => 'percentage' },
204 post_status => { type => 'status' }
205 };
206  
207 my $validated = validate_strict(
208 schema => $schema,
209 input => $input,
210 custom_types => $custom_types
211 );
212  
213 Custom types can be extended or overridden in the schema by specifying additional constraints:
214  
215 my $schema = {
216 admin_username => {
217 type => 'username', # Uses custom type definition
218 min => 5, # Overrides custom type's min value
219 max => 15 # Overrides custom type's max value
220 }
221 };
222  
223 Custom types work seamlessly with nested schema, optional parameters, and all other validation features.
224  
225 =back
226  
227 The schema can define the following rules for each parameter:
228  
229 =over 4
230  
231 =item * C<type>
232  
233 The data type of the parameter.
234 Valid types are C<string>, C<integer>, C<number>, C<float>, C<boolean>, C<scalar>, C<scalarref>, C<stringref>, C<hashref>, C<arrayref>, C<object>, C<coderef>, C<regex>, C<handle>, C<arraylike>, C<hashlike>, C<codelike>, C<invocant> and C<void>.
235 C<scalar> accepts any plain scalar value (string, number, boolean, etc.) but rejects references (arrayrefs, hashrefs, coderefs, objects).
236 C<scalarref> accepts a reference to a scalar value (e.g. C<\$var>) but rejects plain scalars, arrayrefs, hashrefs, coderefs, and objects.
237 C<stringref> accepts a reference to a scalar that contains a plain string (e.g. C<\$str>) and rejects plain scalars, references-to-references, arrayrefs, hashrefs, coderefs, and objects.
238 C<void> asserts that the parameter value is C<undef> (the parameter represents a void return or absent output).
239 When C<void> is used the schema must contain exactly one parameter.
240 The C<min>/C<max> constraints apply to the B<length> (in characters) of the referenced string.
241 All other string rules (C<matches>, C<nomatch>, C<memberof>, etc.) operate on the dereferenced string value.
242 The validated return value is the dereferenced plain string.
243 C<regex> accepts a compiled regular expression (C<qr//> object); the value is returned unchanged.
244 C<handle> accepts a file handle: a glob reference with a defined C<fileno>, an C<IO::Handle> subclass instance, or any value for which C<fileno> returns a defined value.
245 C<arraylike> accepts an array reference or a blessed object that overloads C<@{}> array dereferencing.
246 C<hashlike> accepts a hash reference or a blessed object that overloads C<%{}> hash dereferencing.
247 C<codelike> accepts a code reference or a blessed object that overloads C<&{}> code dereferencing.
248 C<invocant> accepts either a blessed object instance or a plain string that is a syntactically valid Perl class name (e.g. C<'MyApp::Widget'>).
249  
250 A type can be an arrayref when a parameter could have different types (e.g. a string or an object).
251  
252 $schema = {
253 username => [
254 { type => 'string', min => 3, max => 50 }, # Name
255 { type => 'integer', 'min' => 1 }, # UID that isn't root
256 ]
257 };
258  
259 As a shorthand, C<type> itself may be an arrayref of type name strings (a I<union type>),
260 or a pipe-separated string, when all other constraints are shared between the alternatives:
261  
262 $schema = {
263 data => { type => ['string', 'arrayref'] },
264 id => { type => 'string|integer', optional => 1 },
265 };
266  
267 This is equivalent to the full array-of-rules form but more concise.
268 Whitespace around the C<|> is ignored, so C<'string | arrayref'> is the same as C<'string|arrayref'>.
269 Every other key in the rule hash (C<optional>, C<min>, C<max>, C<matches>, etc.)
270 is inherited by each candidate type and validated independently against it.
271 Type names are tried left-to-right; the first match wins and its coercion
272 (e.g. numeric types) is propagated back to the caller.
273 If the value fails all candidate types, validation croaks with a message
274 listing the union members.
275  
276 =item * C<can>
277  
278 The parameter must be an object, or with C<type =E<gt> 'invocant'> an object or a class name,
279 that understands the method C<can>.
280 C<can> can be a simple scalar string of a method name,
281 or an arrayref of a list of method names, all of which must be supported by the object.
282  
283 $schema = {
284 gedcom => { type => object, can => 'get_individual' }
285 }
286  
287 =item * C<isa>
288  
289 The parameter must be an object of type C<isa>.
290 Requires C<type =E<gt> 'object'>.
291  
292 =item * C<does>
293  
294 The parameter must be a blessed object that satisfies the role via C<-E<gt>DOES>.
295 Requires C<type =E<gt> 'object'>.
296  
297 handler => { type => 'object', does => 'My::Role::Printable' }
298  
299 =item * C<classcan>
300  
301 The parameter must be a string holding a syntactically valid Perl class name
302 whose class understands the given method (or every method in an arrayref).
303 The check is always a C<-E<gt>can> method call, so an overridden C<can> is respected.
304 The class must already be loaded.
305 Symmetric with C<classisa> and C<classdoes>.
306  
307 backend => { type => 'string', classcan => ['connect', 'disconnect'] }
308  
309 =item * C<element_isa>
310  
311 Requires C<type =E<gt> 'arrayref'>.
312 Every element must be a blessed object that passes C<-E<gt>isa('Base::Class')>.
313 An empty arrayref is accepted; add C<min =E<gt> 1> to require at least one element.
314 Unlike C<element_type =E<gt> 'object'>, this checks the inheritance chain, not just blessedness.
315  
316 handlers => { type => 'arrayref', min => 1, element_isa => 'My::Handler' }
317  
318 =item * C<needs>
319  
320 A parameter name, or an arrayref of names, that must also be given whenever this parameter is.
321 A shorthand for one C<dependency> relationship per name (see C<relationships>).
322  
323 user => { type => 'string', optional => 1, needs => ['password', 'realm'] }
324  
325 =item * C<classisa>
326  
327 The parameter must be a string holding a syntactically valid Perl class name
328 that passes C<-E<gt>isa('Base::Class')>.
329 The class must already be loaded (its C<@ISA> must be reachable).
330 Does not accept blessed object references; use C<isa> for those.
331  
332 backend => { type => 'string', classisa => 'My::Backend::Base' }
333  
334 =item * C<subclass>
335  
336 Like C<classisa>, but requires a I<strict> subclass: the value must not equal
337 the base class name itself.
338  
339 plugin => { type => 'string', subclass => 'My::Plugin::Base' }
340  
341 =item * C<classdoes>
342  
343 Like C<classisa>, but tests C<-E<gt>DOES> (role consumption) instead of C<-E<gt>isa>.
344  
345 consumer => { type => 'string', classdoes => 'My::Role::Loggable' }
346  
347 =item * C<driver>
348  
349 The parameter must be a valid class name that: (1) can be loaded via C<require>,
350 and (2) passes C<-E<gt>isa('Base::Class')>.
351 The module is actually loaded as a side effect of validation.
352  
353 store => { type => 'string', driver => 'Cache::Store' }
354  
355 =item * C<memberof>
356  
357 The parameter must be a member of the given arrayref.
358  
359 status => {
360 type => 'string',
361 memberof => ['draft', 'published', 'archived']
362 }
363  
364 priority => {
365 type => 'integer',
366 memberof => [1, 2, 3, 4, 5]
367 }
368  
369 For string types, the comparison is case-sensitive by default. Use the C<case_sensitive>
370 flag to control this behavior:
371  
372 # Case-sensitive (default) - must be exact match
373 code => {
374 type => 'string',
375 memberof => ['ABC', 'DEF', 'GHI']
376 # 'abc' will fail
377 }
378  
379 # Case-insensitive - any case accepted
380 code => {
381 type => 'string',
382 memberof => ['ABC', 'DEF', 'GHI'],
383 case_sensitive => 0
384 # 'abc', 'Abc', 'ABC' all pass, original case preserved
385 }
386  
387 For numeric types (C<integer>, C<number>, C<float>), the comparison uses numeric
388 equality (C<==> operator):
389  
390 rating => {
391 type => 'number',
392 memberof => [0.5, 1.0, 1.5, 2.0]
393 }
394  
395 Note that C<memberof> cannot be combined with C<min> or C<max> constraints as they
396 serve conflicting purposes - C<memberof> defines an explicit whitelist while C<min>/C<max>
397 define ranges.
398  
399 =item * C<enum>
400  
401 Same as C<memberof>.
402  
403 =item * C<values>
404  
405 Same as C<memberof>.
406  
407 =item * C<notmemberof>
408  
409 The parameter must not be a member of the given arrayref (blacklist).
410 This is the inverse of C<memberof>.
411  
412 username => {
413 type => 'string',
414 notmemberof => ['admin', 'root', 'system', 'administrator']
415 }
416  
417 port => {
418 type => 'integer',
419 notmemberof => [22, 23, 25, 80, 443] # Reserved ports
420 }
421  
422 Like C<memberof>, string comparisons are case-sensitive by default but can be controlled
423 with the C<case_sensitive> flag:
424  
425 # Case-sensitive (default)
426 username => {
427 type => 'string',
428 notmemberof => ['Admin', 'Root']
429 # 'admin' would pass, 'Admin' would fail
430 }
431  
432 # Case-insensitive
433 username => {
434 type => 'string',
435 notmemberof => ['Admin', 'Root'],
436 case_sensitive => 0
437 # 'admin', 'ADMIN', 'Admin' all fail
438 }
439  
440 The blacklist is checked after any C<transform> rules are applied, allowing you to
441 normalize input before checking:
442  
443 username => {
444 type => 'string',
445 transform => sub { lc($_[0]) }, # Normalize to lowercase
446 notmemberof => ['admin', 'root', 'system']
447 }
448  
449 C<notmemberof> can be combined with other validation rules:
450  
451 username => {
452 type => 'string',
453 notmemberof => ['admin', 'root', 'system'],
454 min => 3,
455 max => 20,
456 matches => qr/^[a-z0-9_]+$/
457 }
458  
459 =item * C<case_sensitive>
460  
461 A boolean value indicating whether string comparisons should be case-sensitive.
462 This flag affects the C<memberof> and C<notmemberof> validation rules.
463 The default value is C<1> (case-sensitive).
464  
465 When set to C<0>, string comparisons are performed case-insensitively, allowing values
466 with different casing to match. The original case of the input value is preserved in
467 the validated output.
468  
469 # Case-sensitive (default)
470 status => {
471 type => 'string',
472 memberof => ['Draft', 'Published', 'Archived'] # Input 'draft' will fail - must match exact case
473 }
474  
475 # Case-insensitive
476 status => {
477 type => 'string',
478 memberof => ['Draft', 'Published', 'Archived'],
479 case_sensitive => 0 # Input 'draft', 'DRAFT', or 'DrAfT' will all pass
480 }
481  
482 country_code => {
483 type => 'string',
484 memberof => ['US', 'UK', 'CA', 'FR'],
485 case_sensitive => 0 # Accept 'us', 'US', 'Us', etc.
486 }
487  
488 This flag has no effect on numeric types (C<integer>, C<number>, C<float>) as numbers
489 do not have case.
490  
491 =item * C<min>/C<minimum>
492  
493 The minimum length (for strings in characters not bytes), value (for numbers) or number of keys (for hashrefs).
494  
495 =item * C<max>
496  
497 The maximum length (for strings in characters not bytes), value (for numbers) or number of keys (for hashrefs).
498  
499 =item * C<matches>
500  
501 A regular expression that the parameter value must match.
502 Checks all members of arrayrefs.
503  
504 =item * C<nomatch>
505  
506 A regular expression that the parameter value must not match.
507 Checks all members of arrayrefs.
508  
509 =item * C<bnf>
510  
511 An arrayref of BNF grammar lines that defines the set of strings the
512 parameter value must belong to.
513 The first rule in the grammar is the start rule; the value must match it
514 exactly (anchored).
515  
516 Each element is either a rule definition (C<< <name> ::= ... >>) or a
517 continuation of the previous rule. Terminals are double-quoted; non-terminals
518 use angle brackets. Alternatives are separated by C<|>.
519  
520 $schema = {
521 na_tel_no => {
522 type => 'string',
523 bnf => [
524 '<telephone-number> ::= <country-code-opt> <area-code> <separator-opt>',
525 '<central-office-code> <separator-opt> <station-code>',
526 '<country-code-opt> ::= "" | "+1" | "1"',
527 '<separator-opt> ::= "" | "-" | " " | "."',
528 '<area-code> ::= <digit2-9> <digit0-9> <digit0-9>',
529 '<central-office-code> ::= <digit2-9> <digit0-9> <digit0-9>',
530 '<station-code> ::= <digit0-9> <digit0-9> <digit0-9> <digit0-9>',
531 '<digit0-9> ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"',
532 '<digit2-9> ::= "2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"',
533 ],
534 },
535 };
536  
537 Implemented by L<Params::Validate::Strict::BNF>. Recursive grammars are not
538 supported.
539  
540 =item * C<position>
541  
542 For routines and methods that take positional args,
543 this integer value defines which position the argument will be in.
544 If this is set for all arguments,
545 C<validate_strict> will return a reference to an array, rather than a reference to a hash.
546  
547 =item * C<slurp>
548  
549 Valid only in positional-argument schemas (those where every parameter has a
550 C<position> value). When C<slurp =E<gt> 1> is set, this parameter collects
551 I<all> remaining positional arguments starting from C<position> into an
552 arrayref, rather than taking only the single element at that index.
553  
554 # sub log_message($level, @messages)
555 my $schema = {
556 level => { type => 'string', position => 0 },
557 messages => { type => 'arrayref', position => 1, slurp => 1 },
558 };
559  
560 The slurp parameter is implicitly optional: if there are no arguments at or
561 beyond C<position>, the value is an empty arrayref. Combine with C<min =E<gt>
562 1> to require at least one element:
563  
564 messages => { type => 'arrayref', position => 1, slurp => 1, min => 1 }
565  
566 At most one slurp parameter may be defined per schema, and it must have the
567 highest C<position> value. The return value at that position is an arrayref.
568  
569 =item * C<aliases>
570  
571 An arrayref of alternative input-key names that are also accepted for this
572 parameter. When any alias is found in the input the parameter is stored
573 under its canonical schema key; if both the canonical name and an alias are
574 present the canonical name takes precedence. Aliases are not treated as
575 unknown parameters regardless of the C<unknown_parameter_handler> setting.
576  
577 colour => {
578 type => 'string',
579 aliases => ['color'],
580 memberof => ['red', 'green', 'blue'],
581 }
582  
583 Only named (hashref) input supports aliases; positional (arrayref) input
584 ignores them.
585  
586 =item * C<regex>
587  
588 Synonym of matches
589  
590 =item * C<description>
591  
592 The description of the rule
593  
594 =item * C<callback>
595  
596 A code reference to a subroutine that performs custom validation logic.
597 The subroutine is called with four arguments: the parameter value, the argument list, the schema and the name of the parameter being validated
598 (the canonical name, even if the value was supplied under an alias).
599 It should return true if the value is valid, false otherwise.
600 The name lets one callback be shared between several parameters.
601  
602 Use this to test more complex examples:
603  
604 my $schema = {
605 even_number => {
606 type => 'integer',
607 callback => sub { $_[0] % 2 == 0 }
608 };
609  
610 # Specify the arguments for a routine which has a second, optional argument, which, if given, must be less than or equal to the first
611 my $schema = {
612 first => {
613 type => 'integer'
614 }, second => {
615 type => 'integer',
616 optional => 1,
617 callback => sub {
618 my($value, $args) = @_;
619 # The 'defined' is needed in case 'second' is evaluated before 'first'
620 return (defined($args->{first}) && $value <= $args->{first}) ? 1 : 0
621 }
622 }
623 };
624  
625 =item * C<optional>
626  
627 A boolean value indicating whether the parameter is optional.
628 If true, the parameter is not required.
629 If false or omitted, the parameter is required.
630  
631 It can be a reference to a code snippet that will return true or false,
632 to determine if the parameter is optional or not.
633 The code will be called with two arguments: the value of the parameter and hash ref of all parameters:
634  
635 my $schema = {
636 optional_field => {
637 type => 'string',
638 optional => sub {
639 my ($value, $all_params) = @_;
640 return $all_params->{make_optional} ? 1 : 0;
641 }
642 },
643 make_optional => { type => 'boolean' }
644 };
645  
646 my $result = validate_strict(schema => $schema, input => { make_optional => 1 });
647  
648 If the parameter is not optional, it can be passed an undef value, which will not flag an error.
649 This is by design.
650 So this will not say that the required parameter 's' is missing:
651  
652 validate_strict(
653 schema => { s => { type => 'string' } },
654 input => { s => undef },
655 );
656  
657 =item * C<default>
658  
659 Populate missing optional parameters with the specified value.
660 Note that this value is not validated.
661  
662 username => {
663 type => 'string',
664 optional => 1,
665 default => 'guest'
666 }
667  
668 =item * C<element_type>
669  
670 Extends the validation to individual elements of arrays.
671  
672 tags => {
673 type => 'arrayref',
674 element_type => 'number', # Float means the same
675 min => 1, # this is the length of the array, not the min value for each of the numbers. For that, add a C<schema> rule
676 max => 5
677 }
678  
679 =item * C<error_msg>
680  
681 The custom error message to be used in the event of a validation failure.
682  
683 age => {
684 type => 'integer',
685 min => 18,
686 error_msg => 'You must be at least 18 years old'
687 }
688  
689 =item * C<nullable>
690  
691 Like optional,
692 though this cannot be a coderef,
693 only a flag.
694  
695 =item * C<schema>
696  
697 You can validate nested hashrefs and arrayrefs using the C<schema> property:
698  
699 my $schema = {
700 user => { # 'user' is a hashref
701 type => 'hashref',
702 schema => { # Specify what the elements of the hash should be
703 name => { type => 'string' },
704 age => { type => 'integer', min => 0 },
705 hobbies => { # 'hobbies' is an array ref that this user has
706 type => 'arrayref',
707 schema => { type => 'string' }, # Validate each hobby
708 min => 1 # At least one hobby
709 }
710 }
711 }, metadata => {
712 type => 'hashref',
713 schema => {
714 created => { type => 'string' },
715 tags => {
716 type => 'arrayref',
717 schema => {
718 type => 'string',
719 matches => qr/^[a-z]+$/ # Or you can say matches => '^[a-z]+$'
720 }
721 }
722 }
723 }
724 };
725  
726 =item * C<validate>
727  
728 A snippet of code that validates the input.
729 It's passed the input arguments,
730 and return a string containing a reason for rejection,
731 or undef if it's allowed.
732  
733 my $schema = {
734 user => {
735 type => 'string',
736 validate => sub {
737 if($_[0]->{'password'} eq 'bar') {
738 return undef;
739 }
740 return 'Invalid password, try again';
741 }
742 }, password => {
743 type => 'string'
744 }
745 };
746  
747 =item * C<transform>
748  
749 A code reference to a subroutine that transforms/sanitizes the parameter value before validation.
750 The subroutine should accept the parameter value as an argument and return the transformed value.
751 The transformation is applied before any validation rules are checked, allowing you to normalize
752 or clean data before it is validated.
753  
754 Common use cases include trimming whitespace, normalizing case, formatting phone numbers,
755 sanitizing user input, and converting between data formats.
756  
757 # Simple string transformations
758 username => {
759 type => 'string',
760 transform => sub { lc(trim($_[0])) }, # lowercase and trim
761 matches => qr/^[a-z0-9_]+$/
762 }
763  
764 email => {
765 type => 'string',
766 transform => sub { lc(trim($_[0])) }, # normalize email
767 matches => qr/^[^@\s]+\@[^@\s]+\.[^@\s]+$/
768 }
769  
770 # Array transformations
771 tags => {
772 type => 'arrayref',
773 transform => sub { [map { lc($_) } @{$_[0]}] }, # lowercase all elements
774 element_type => 'string'
775 }
776  
777 keywords => {
778 type => 'arrayref',
779 transform => sub {
780 my @arr = map { lc(trim($_)) } @{$_[0]};
781 my %seen;
782 return [grep { !$seen{$_}++ } @arr]; # remove duplicates
783 }
784 }
785  
786 # Numeric transformations
787 quantity => {
788 type => 'integer',
789 transform => sub { int($_[0] + 0.5) }, # round to nearest integer
790 min => 1
791 }
792  
793 # Sanitization
794 slug => {
795 type => 'string',
796 transform => sub {
797 my $str = lc(trim($_[0]));
798 $str =~ s/[^\w\s-]//g; # remove special characters
799 $str =~ s/\s+/-/g; # replace spaces with hyphens
800 return $str;
801 },
802 matches => qr/^[a-z0-9-]+$/
803 }
804  
805 phone => {
806 type => 'string',
807 transform => sub {
808 my $str = $_[0];
809 $str =~ s/\D//g; # remove all non-digits
810 return $str;
811 },
812 matches => qr/^\d{10}$/
813 }
814  
815 The C<transform> function is applied to the value before any validation checks (C<min>/C<minimum>, C<max>,
816 C<matches>, C<callback>, etc.), ensuring that validation rules are checked against the cleaned data.
817  
818 Transformations work with all parameter types including nested structures:
819  
820 user => {
821 type => 'hashref',
822 schema => {
823 name => {
824 type => 'string',
825 transform => sub { trim($_[0]) }
826 }, email => {
827 type => 'string',
828 transform => sub { lc(trim($_[0])) }
829 }
830 }
831 }
832  
833 Transformations can also be defined in custom types for reusability:
834  
835 my $custom_types = {
836 email => {
837 type => 'string',
838 transform => sub { lc(trim($_[0])) },
839 matches => qr/^[^@\s]+\@[^@\s]+\.[^@\s]+$/
840 }
841 };
842  
843 Note that the transformed value is what gets returned in the validated result and is what
844 subsequent validation rules will check against. If a transformation might fail, ensure it
845 handles edge cases appropriately.
846 It is the responsibility of the transformer to ensure that the type of the returned value is correct,
847 since that is what will be validated.
848  
849 Many validators also allow a code ref to be passed so that you can create your own, conditional validation rule, e.g.:
850  
851 $schema = {
852 age => {
853 type => 'integer',
854 min => sub {
855 my ($value, $all_params) = @_;
856 return $all_params->{country} eq 'US' ? 21 : 18;
857 }
858 }
859 }
860  
861 =item * C<validator>
862  
863 A synonym of C<validate>, for compatibility with L<Data::Processor>.
864  
865 =item * C<cross_validation>
866  
867 A reference to a hash that defines validation rules that depend on more than one parameter.
868 Cross-field validations are performed after all individual parameter validations have passed,
869 allowing you to enforce business logic that requires checking relationships between different fields.
870  
871 Each cross-validation rule is a key-value pair where the key is a descriptive name for the validation
872 and the value is a code reference that accepts a hash reference of all validated parameters.
873 The subroutine should return C<undef> if the validation passes, or an error message string if it fails.
874  
875 my $schema = {
876 password => { type => 'string', min => 8 },
877 password_confirm => { type => 'string' }
878 };
879  
880 my $cross_validation = {
881 passwords_match => sub {
882 my $params = shift;
883 return $params->{password} eq $params->{password_confirm}
884 ? undef : "Passwords don't match";
885 }
886 };
887  
888 my $validated = validate_strict(
889 schema => $schema,
890 input => $input,
891 cross_validation => $cross_validation
892 );
893  
894 Common use cases include password confirmation, date range validation, numeric comparisons,
895 and conditional requirements:
896  
897 # Date range validation
898 my $cross_validation = {
899 date_range_valid => sub {
900 my $params = shift;
901 return $params->{start_date} le $params->{end_date}
902 ? undef : "Start date must be before or equal to end date";
903 }
904 };
905  
906 # Price range validation
907 my $cross_validation = {
908 price_range_valid => sub {
909 my $params = shift;
910 return $params->{min_price} <= $params->{max_price}
911 ? undef : "Minimum price must be less than or equal to maximum price";
912 }
913 };
914  
915 # Conditional required field
916 my $cross_validation = {
917 address_required_for_delivery => sub {
918 my $params = shift;
919 if ($params->{shipping_method} eq 'delivery' && !$params->{delivery_address}) {
920 return "Delivery address is required when shipping method is 'delivery'";
921 }
922 return undef;
923 }
924 };
925  
926 Multiple cross-validations can be defined in the same hash, and they are all checked in order.
927 If any cross-validation fails, the function will C<croak> with the error message returned by the validation:
928  
929 my $cross_validation = {
930 passwords_match => sub {
931 my $params = shift;
932 return $params->{password} eq $params->{password_confirm}
933 ? undef : "Passwords don't match";
934 },
935 emails_match => sub {
936 my $params = shift;
937 return $params->{email} eq $params->{email_confirm}
938 ? undef : "Email addresses don't match";
939 },
940 age_matches_birth_year => sub {
941 my $params = shift;
942 my $current_year = (localtime)[5] + 1900;
943 my $calculated_age = $current_year - $params->{birth_year};
944 return abs($calculated_age - $params->{age}) <= 1
945 ? undef : "Age doesn't match birth year";
946 }
947 };
948  
949 Cross-validations receive the parameters after individual validation and transformation have been applied,
950 so you can rely on the data being in the correct format and type:
951  
952 my $schema = {
953 email => {
954 type => 'string',
955 transform => sub { lc($_[0]) } # Lowercased before cross-validation
956 },
957 email_confirm => {
958 type => 'string',
959 transform => sub { lc($_[0]) }
960 }
961 };
962  
963 my $cross_validation = {
964 emails_match => sub {
965 my $params = shift;
966 # Both emails are already lowercased at this point
967 return $params->{email} eq $params->{email_confirm}
968 ? undef : "Email addresses don't match";
969 }
970 };
971  
972 Cross-validations can access nested structures and optional fields:
973  
974 my $cross_validation = {
975 guardian_required_for_minors => sub {
976 my $params = shift;
977 if ($params->{user}{age} < 18 && !$params->{guardian}) {
978 return "Guardian information required for users under 18";
979 }
980 return undef;
981 }
982 };
983  
984 All cross-validations must pass for the overall validation to succeed.
985  
986 =item * metadata
987  
988 Fields starting with <_> are generated by L<App::Test::Generator::SchemaExtractor>,
989 and are currently ignored.
990  
991 =item * C<semantic>
992  
993 A hint about the semantic meaning of the parameter value.
994 Supported values: C<unix_timestamp>, C<identifier>, C<class_name>, C<filename>.
995  
996 ts => { type => 'integer', semantic => 'unix_timestamp' }
997 func => { type => 'string', semantic => 'identifier' }
998 module => { type => 'string', semantic => 'class_name' }
999 file => { type => 'string', semantic => 'filename' }
1000  
1001 When C<semantic> is C<unix_timestamp>, the value must be a non-negative integer no greater than
1002 C<2147483647> (i.e. a valid 32-bit Unix epoch timestamp).
1003 Values outside this range cause the function to C<croak>.
1004  
1005 When C<semantic> is C<identifier>, the value must match C</\A[A-Za-z_]\w*\z/>, a single
1006 valid Perl bareword identifier. Package separators (C<::>) are not permitted; use
1007 C<class_name> for those.
1008  
1009 When C<semantic> is C<class_name>, the value must match
1010 C</\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/>, a syntactically valid Perl class name such
1011 as C<'Foo'> or C<'Foo::Bar::Baz'>. The class does not need to be loaded.
1012  
1013 When C<semantic> is C<filename>, the value must be a single path component:
1014 non-empty, not C<.> or C<..>, containing neither C</> nor a NUL byte, and at most 255
1015 bytes long when encoded as UTF-8. Directory parts are rejected, so it is safe against
1016 path traversal such as C<../../etc/passwd>. It does not check that the file exists, and
1017 it does not reject characters that are only illegal on some platforms (such as C<:> on Windows).
1018  
1019 Unknown semantic values emit a warning but do not cause an error.
1020  
1021 =item * C<relationships>
1022  
1023 A reference to an array that defines validation rules based on relationships between parameters.
1024 Relationship validations are performed after all individual parameter validations have passed,
1025 but before cross-validations.
1026  
1027 Each relationship is a hash reference with a C<type> field and additional fields depending on the type:
1028  
1029 =over 4
1030  
1031 =item * B<mutually_exclusive>
1032  
1033 Parameters that cannot be specified together.
1034  
1035 relationships => [
1036 {
1037 type => 'mutually_exclusive',
1038 params => ['file', 'content'],
1039 description => 'Cannot specify both file and content'
1040 }
1041 ]
1042  
1043 =item * B<required_group>
1044  
1045 At least one parameter from the group must be specified.
1046  
1047 relationships => [
1048 {
1049 type => 'required_group',
1050 params => ['id', 'name'],
1051 logic => 'or',
1052 description => 'Must specify either id or name'
1053 }
1054 ]
1055  
1056 =item * B<conditional_requirement>
1057  
1058 If one parameter is specified, another becomes required.
1059  
1060 relationships => [
1061 {
1062 type => 'conditional_requirement',
1063 if => 'async',
1064 then_required => 'callback',
1065 description => 'When async is specified, callback is required'
1066 }
1067 ]
1068  
1069 =item * B<dependency>
1070  
1071 One parameter requires another to be present.
1072  
1073 relationships => [
1074 {
1075 type => 'dependency',
1076 param => 'port',
1077 requires => 'host',
1078 description => 'port requires host to be specified'
1079 }
1080 ]
1081  
1082 =item * B<value_constraint>
1083  
1084 Specific value requirements between parameters.
1085  
1086 relationships => [
1087 {
1088 type => 'value_constraint',
1089 if => 'ssl',
1090 then => 'port',
1091 operator => '==',
1092 value => 443,
1093 description => 'When ssl is specified, port must equal 443'
1094 }
1095 ]
1096  
1097 =item * B<value_conditional>
1098  
1099 Parameter required when another has a specific value.
1100  
1101 relationships => [
1102 {
1103 type => 'value_conditional',
1104 if => 'mode',
1105 equals => 'secure',
1106 then_required => 'key',
1107 description => "When mode equals 'secure', key is required"
1108 }
1109 ]
1110  
1111 =back
1112  
1113 If a parameter is optional and its value is C<undef>,
1114 validation will be skipped for that parameter.
1115  
1116 If the validation fails, the function will C<croak> with an error message describing the validation failure.
1117  
1118 If the validation is successful, the function will return a reference to a new hash containing the validated and (where applicable) coerced parameters. Integer and number parameters will be coerced to their respective types.
1119  
1120 The C<description> field is optional but recommended for clearer error messages.
1121  
1122 =back
1123  
1124 =head3 API SPECIFICATION
1125  
1126 =head4 Input
1127  
1128 {
1129 schema => { type => ['hashref', 'arrayref'] }, # or members
1130 input => { type => ['hashref', 'arrayref'] }, # or args
1131 description => { type => 'string', optional => 1 },
1132 custom_types => { type => 'hashref', optional => 1 },
1133 unknown_parameter_handler => { type => 'string', optional => 1,
1134 memberof => ['die', 'warn', 'ignore'] },
1135 logger => { type => 'object', optional => 1, can => ['error', 'warn'] },
1136 relationships => { type => 'arrayref', optional => 1 },
1137 cross_validation => { type => 'hashref', optional => 1 },
1138 }
1139  
1140 The arguments may be passed as a list of key/value pairs or as a single hashref.
1141 A hashref C<input> is validated as named parameters; an arrayref C<input> is validated as
1142 positional parameters, in which case every parameter in the schema needs a C<position>.
1143  
1144 =head4 Output
1145  
1146 { type => 'hashref' }
1147  
1148 A new hashref of the validated and coerced parameters, keyed by canonical parameter name.
1149 It croaks (or calls C<< $logger->error >> and then croaks) on the first validation failure.
1150  
1151 =head2 Example Usage
1152  
1153 my $schema = {
1154 host => { type => 'string' },
1155 port => { type => 'integer' },
1156 ssl => { type => 'boolean' },
1157 file => { type => 'string', optional => 1 },
1158 content => { type => 'string', optional => 1 }
1159 };
1160  
1161 my $relationships = [
1162 {
1163 type => 'mutually_exclusive',
1164 params => ['file', 'content']
1165 }, {
1166 type => 'required_group',
1167 params => ['host', 'file']
1168 },
1169 {
1170 type => 'dependency',
1171 param => 'port',
1172 requires => 'host'
1173 },
1174 {
1175 type => 'value_constraint',
1176 if => 'ssl',
1177 then => 'port',
1178 operator => '==',
1179 value => 443
1180 }
1181 ];
1182  
1183 my $validated = validate_strict(
1184 schema => $schema,
1185 input => $input,
1186 relationships => $relationships
1187 );
1188  
1189 =head1 MIGRATION FROM LEGACY VALIDATORS
1190  
1191 =head2 From L<Params::Validate>
1192  
1193 # Old style
1194 validate(@_, {
1195 name => { type => SCALAR },
1196 age => { type => SCALAR, regex => qr/^\d+$/ }
1197 });
1198  
1199 # New style
1200 validate_strict(
1201 schema => { # or "members"
1202 name => 'string',
1203 age => { type => 'integer', min => 0 }
1204 },
1205 args => { @_ }
1206 );
1207  
1208 =head2 From L<Type::Params>
1209  
1210 # Old style
1211 my ($name, $age) = validate_positional \@_, Str, Int;
1212  
1213 # New style - requires converting to named parameters first
1214 my %args = (name => $_[0], age => $_[1]);
1215 my $validated = validate_strict(
1216 schema => { name => 'string', age => 'integer' },
1217 args => \%args
1218 );
1219  
1220 =cut
1221  
1222 sub validate_strict
1223 48485 ▶{
Subroutine validate_strict: called CC=549 SCAR=68.4
Pod documented
1224 48485 local $_depth = $_depth + 1;
1225 48485 ▶ Carp::croak('validate_strict: maximum call depth exceeded - possible infinite recursion in union-type schema')
Branch: 1 branch
BranchTrueFalseTotal
if $Params::Validate::Strict::_depth > 20 1 48484 48485
1226 if $_depth > 20;
1227  
1228 # $params is only ever read, so a hashref argument need not be copied
1229 48484 ▶ my $params = (ref($_[0]) eq 'HASH') ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 23726 24758 48484
1230  
1231 48484 ▶ my $schema = $params->{'schema'} || $params->{'members'};
Condition100%3/3
$params->{'schema'} || $params->{'members'}
l!l&&r!l&&!r
4847644
MC/DC100%2/2
$params->{'schema'} || $params->{'members'}
$params->{'schema'} $params->{'members'}
Truth table100%3/3
A || B
A= $params->{'schema'}B= $params->{'members'}
ABresult
1X1
011
000
1232 48484 ▶ my $args = $params->{'args'} || $params->{'input'};
Condition100%3/3
$params->{'args'} || $params->{'input'}
l!l&&r!l&&!r
157483234
MC/DC100%2/2
$params->{'args'} || $params->{'input'}
$params->{'args'} $params->{'input'}
Truth table100%3/3
A || B
A= $params->{'args'}B= $params->{'input'}
ABresult
1X1
011
000
1233 48484 my $logger = $params->{'logger'};
1234 48484 my $custom_types = $params->{'custom_types'};
1235 48484 my $unknown_parameter_handler = $params->{'unknown_parameter_handler'};
1236 48484 ▶ if(!defined($unknown_parameter_handler)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $unknown_parameter_handler) 48238 246 48484
1237 48238 ▶ if($params->{'carp_on_warn'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'carp_on_warn'}) { } 2 48236 48238
1238 2 $unknown_parameter_handler = 'warn';
1239 } else {
1240 48236 $unknown_parameter_handler = 'die';
1241 }
1242 }
1243  
1244 48484 ▶ return $args if(!defined($schema)); # No schema, allow all arguments
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 4 48480 48484
1245  
1246 # Accept arrayref schema: [{ name=>'param', type=>'...', ... }, ...]
1247 # Normalise to the standard named-parameter hashref form before further processing.
1248 48480 ▶ if(ref($schema) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "ARRAY") 17 48463 48480
1249 17 $schema = _schema_from_arrayref($schema, $logger);
1250 }
1251  
1252 # Check if schema and args are references to hashes
1253 48473 ▶ if(ref($schema) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema ne "HASH") 3 48470 48473
1254 3 _error($logger, 'validate_strict: schema must be a hash reference');
1255 }
1256  
1257 # Inspired by Data::Processor
1258 48470 ▶ my $schema_description = $params->{'description'} || 'validate_strict';
Condition100%2/2
$params->{'description'} || 'validate_strict'
l!l
548465
MC/DC100%1/1
$params->{'description'} || 'validate_strict'
$params->{'description'}
Truth table100%2/2
A
A= $params->{'description'}
Aresult
11
00
1259 48470 my $error_msg = $params->{'error_msg'};
1260  
1261 48470 ▶ if($schema->{'members'} && ($schema->{'description'} || $schema->{'error_msg'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"members"} and $schema->{"description"} || $schema->{"error_msg"}) 8 48462 48470
Condition50%3/6
$schema->{"members"} and $schema->{"description"} || $schema->{"error_msg"}
!ll&&!rl&&r
4846208
$schema->{"description"} or $schema->{"error_msg"}
l!l&&r!l&&!r
800
MC/DC33%1/3
$schema->{"members"} and $schema->{"description"} || $schema->{"error_msg"}
$schema->{"members"} $schema->{"description"} $schema->{"error_msg"}
Truth table50%2/4
A and B or C
A= $schema->{"members"}B= $schema->{"description"}C= $schema->{"error_msg"}
ABCresult
0XX0
1000
11X1
1011
1262 8 $schema_description = $schema->{'description'};
1263 8 $error_msg = $schema->{'error_msg'};
1264 8 $schema = $schema->{'members'};
1265 # The members value may also be in arrayref form
1266 8 ▶ if(ref($schema) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "ARRAY") 1 7 8
1267 1 $schema = _schema_from_arrayref($schema, $logger);
1268 }
1269 }
1270  
1271 48470 ▶ if(exists($params->{'args'}) && (!defined($args))) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'args'} and not defined $args) { } 2 48468 48470
elsif (ref $args ne 'HASH' and ref $args ne 'ARRAY') { } 4 96896 96900
Condition100%3/3
exists $params->{"args"} and not defined $args
!ll&&!rl&&r
483121562
MC/DC100%2/2
exists $params->{"args"} and not defined $args
exists $params->{"args"} not defined $args
Truth table100%3/3
A and not(B)
A= exists $params->{"args"}B= not defined $args
ABresult
0X0
100
111
1272 2 $args = {};
1273 ▶ } elsif((ref($args) ne 'HASH') && (ref($args) ne 'ARRAY')) {
Condition100%3/3
ref $args ne "HASH" and ref $args ne "ARRAY"
!ll&&!rl&&r
48432324
MC/DC100%2/2
ref $args ne "HASH" and ref $args ne "ARRAY"
ref $args ne "HASH" ref $args ne "ARRAY"
Truth table100%3/3
A and B
A= ref $args ne "HASH"B= ref $args ne "ARRAY"
ABresult
0X0
100
111
1274 4 ▶ _error($logger, $error_msg || "$schema_description: args must be a hash or array reference");
Condition50%1/2
$error_msg || "${schema_description}: args must be a hash or array reference"
l!l
04
MC/DC0%0/1
$error_msg || "${schema_description}: args must be a hash or array reference"
$error_msg
Truth table50%1/2
A
A= $error_msg
Aresult
11
00
1275 }
1276  
1277 # A schema prepared by compile_schema carries pre-computed per-field records
1278 # that let simple fields bypass the generic rule interpreter below. It is
1279 # honoured only when it describes exactly the schema now in hand and the
1280 # input is a named-argument hash.
1281 48466 my $compiled = $params->{'_compiled'};
1282 48466 my $fields;
1283 48466 ▶ if($compiled && ref($args) eq 'HASH' && $schema == $compiled->{'schema'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiled and ref $args eq 'HASH' and $schema == $compiled->{'schema'}) { } 23153 25313 48466
Condition66%4/6
$compiled and ref $args eq "HASH" and $schema == $compiled->{"schema"}
!ll&&!rl&&r
25313023153
$compiled and ref $args eq "HASH"
!ll&&!rl&&r
25313023153
MC/DC33%1/3
$compiled and ref $args eq "HASH" and $schema == $compiled->{"schema"}
$compiled ref $args eq "HASH" $schema == $compiled->{"schema"}
Truth table50%2/4
A and B and C
A= $compiledB= ref $args eq "HASH"C= $schema == $compiled->{"schema"}
ABCresult
0XX0
10X0
1100
1111
1284 23153 $fields = $compiled->{'fields'};
1285 } else {
1286 25313 $compiled = undef;
1287 }
1288  
1289 48466 ▶ if(ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 48434 32 48466
1290 # Named args: build alias reverse-map first so aliased keys are not
1291 # treated as unknown parameters. A compiled schema already knows
1292 # whether it has any aliases.
1293 48434 my %_alias_to_canonical;
1294 48434 ▶ if(!$compiled || $compiled->{'has_aliases'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $compiled or $compiled->{"has_aliases"}) 25331 23103 48434
Condition100%3/3
not $compiled or $compiled->{"has_aliases"}
l!l&&r!l&&!r
252815023103
MC/DC100%2/2
not $compiled or $compiled->{"has_aliases"}
not $compiled $compiled->{"has_aliases"}
Truth table100%3/3
not(A) or B
A= not $compiledB= $compiled->{"has_aliases"}
ABresult
1X1
011
000
1295 25331 foreach my $canonical (keys %{$schema}) {
1296 26969 my $r = $schema->{$canonical};
1297 26969 ▶ if(ref($r) eq 'HASH' && ref($r->{'aliases'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "HASH" and ref $r->{"aliases"} eq "ARRAY") 106 26863 26969
Condition100%3/3
ref $r eq "HASH" and ref $r->{"aliases"} eq "ARRAY"
!ll&&!rl&&r
39426469106
MC/DC100%2/2
ref $r eq "HASH" and ref $r->{"aliases"} eq "ARRAY"
ref $r eq "HASH" ref $r->{"aliases"} eq "ARRAY"
Truth table100%3/3
A and B
A= ref $r eq "HASH"B= ref $r->{"aliases"} eq "ARRAY"
ABresult
0X0
100
111
1298 106 $_alias_to_canonical{$_} = $canonical for @{$r->{'aliases'}};
1299 }
1300 }
1301 }
1302  
1303 48434 foreach my $key (keys %{$args}) {
1304 48543 ▶ if(!exists($schema->{$key}) && !exists($_alias_to_canonical{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schema->{$key} or exists $_alias_to_canonical{$key}) 3266 45277 48543
Condition100%3/3
exists $schema->{$key} or exists $_alias_to_canonical{$key}
l!l&&r!l&&!r
45253243266
MC/DC100%2/2
exists $schema->{$key} or exists $_alias_to_canonical{$key}
exists $schema->{$key} exists $_alias_to_canonical{$key}
Truth table100%3/3
A or B
A= exists $schema->{$key}B= exists $_alias_to_canonical{$key}
ABresult
1X1
011
000
1305 3266 ▶ if($unknown_parameter_handler eq 'die') {
Branch: 3 branches
BranchTrueFalseTotal
if ($unknown_parameter_handler eq 'die') { } 3211 55 3266
elsif ($unknown_parameter_handler eq 'warn') { } 24 31 55
elsif ($unknown_parameter_handler eq 'ignore') { } 17 14 31
1306 3211 _error($logger, "$schema_description: Unknown parameter '$key'");
1307 } elsif($unknown_parameter_handler eq 'warn') {
1308 24 _warn($logger, "$schema_description: Unknown parameter '$key'");
1309 24 next;
1310 } elsif($unknown_parameter_handler eq 'ignore') {
1311 17 ▶ if($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 1 16 17
1312 1 $logger->debug(__PACKAGE__ . ": $schema_description: Unknown parameter '$key'");
1313 }
1314 17 next;
1315 } else {
1316 14 _error($logger, "$schema_description: '$unknown_parameter_handler' unknown_parameter_handler must be one of die, warn, ignore");
1317 }
1318 }
1319 }
1320 }
1321  
1322 # Find out if this routine takes positional arguments
1323 45241 my $are_positional_args = -1;
1324 # compile_schema only prepares schemas that are not positional, so a compiled
1325 # schema supplies the answer and the detection loop has nothing to do.
1326 45241 ▶ $are_positional_args = $compiled->{'positional'} if($compiled);
Branch: 1 branch
BranchTrueFalseTotal
if $compiled 21547 23694 45241
1327 45241 ▶ foreach my $key ($compiled ? () : keys %{$schema}) {
Branch: 1 branch
BranchTrueFalseTotal
$compiled ? : 21547 23694 45241
1328 23702 ▶ if(defined(my $rules = $schema->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rules = $schema->{$key})) { } 23700 2 23702
1329 23700 ▶ if(ref($rules) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq 'HASH') { } 23582 118 23700
1330 23582 ▶ if($rules->{'slurp'} && !defined($rules->{'position'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"slurp"} and not defined $rules->{"position"}) 0 23582 23582
Condition66%2/3
$rules->{"slurp"} and not defined $rules->{"position"}
!ll&&!rl&&r
2357840
MC/DC0%0/2
$rules->{"slurp"} and not defined $rules->{"position"}
$rules->{"slurp"} not defined $rules->{"position"}
Truth table66%2/3
A and not(B)
A= $rules->{"slurp"}B= not defined $rules->{"position"}
ABresult
0X0
100
111
1331 0 _error($logger, "::validate_strict: slurp parameter '$key' requires a 'position'");
1332 }
1333 23582 ▶ if(!defined($rules->{'position'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rules->{"position"}) 23532 50 23582
1334 23532 ▶ if($are_positional_args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($are_positional_args == 1) 1 23531 23532
1335 1 _error($logger, "::validate_strict: $key is missing position value");
1336 }
1337 23531 $are_positional_args = 0;
1338 23531 last;
1339 }
1340 50 $are_positional_args = 1;
1341 } else {
1342 118 $are_positional_args = 0;
1343 118 last;
1344 }
1345 } else {
1346 2 $are_positional_args = 0;
1347 2 last;
1348 }
1349 }
1350  
1351 45240 my %validated_args;
1352 my %invalid_args;
1353 45240 my $has_needs = 0; # true if any parameter has a 'needs' rule
1354 45240 foreach my $key (keys %{$schema}) {
1355 # Fast path: a field that compile_schema fully understood is validated
1356 # here without the generic interpreter. It must produce exactly the same
1357 # result and error messages as the generic code below; t/compile_schema.t
1358 # checks the two against each other. Fields it does not understand have no
1359 # record and fall through.
1360 46420 ▶ if($fields && (my $f = $fields->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields and my $f = $fields->{$key}) 11272 35148 46420
Condition100%3/3
$fields and my $f = $fields->{$key}
!ll&&!rl&&r
245251062311272
MC/DC100%2/2
$fields and my $f = $fields->{$key}
$fields my $f = $fields->{$key}
Truth table100%3/3
A and B
A= $fieldsB= my $f = $fields->{$key}
ABresult
0X0
100
111
1361 11272 my $value = $args->{$key};
1362  
1363 11272 ▶ if(!exists($args->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{$key}) 503 10769 11272
1364 503 ▶ if($f->{'opt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{"opt"}) 164 339 503
1365 164 ▶ $validated_args{$key} //= $f->{'default'} if($f->{'has_default'});
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"has_default"} 26 138 164
Condition33%1/3
$validated_args{$key} //= $f->{"default"}
l!l&&r!l&&!r
0260
MC/DC0%0/2
$validated_args{$key} //= $f->{"default"}
$validated_args{$key} $f->{"default"}
Truth table33%1/3
A //= B
A= $validated_args{$key}B= $f->{"default"}
ABresult
1X1
011
000
1366 164 next;
1367 }
1368 339 _error($logger, "$schema_description: Required parameter $f->{label} is missing");
1369 }
1370  
1371 10769 my $label = $f->{'label'};
1372 10769 my $frules = $f->{'rules'};
1373 10769 my $t = $f->{'t'};
1374  
1375 10769 ▶ if($t == _T_STRING) {
Branch: 8 branches
BranchTrueFalseTotal
if ($t == 1) { } 2250 8519 10769
elsif ($t == 2) { } 2035 6484 8519
elsif ($t == 3) { } 2392 4092 6484
elsif ($t == 4) { } 889 3203 4092
elsif ($t == 5) { } 1357 1846 3203
elsif ($t == 6) { } 676 1170 1846
elsif ($t == 7) { } 442 728 1170
elsif ($t == 8) { } 364 364 728
1376 2250 ▶ if(ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 588 1662 2250
1377 588 _rule_error($logger, $frules, "$schema_description: Parameter $label must be a string");
1378 }
1379 } elsif($t == _T_INT) {
1380 2035 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1955 80 2035
1381 1955 ▶ if(!Scalar::Util::looks_like_number($value) || ($value - $value) != 0 || $value != int($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &Scalar::Util::looks_like_number($value) or $value - $value != 0 or $value != int $value) 1364 591 1955
Condition100%6/6
not &Scalar::Util::looks_like_number($value) or $value - $value != 0 or $value != int $value
l!l&&r!l&&!r
128480591
not &Scalar::Util::looks_like_number($value) or $value - $value != 0
l!l&&r!l&&!r
120975671
MC/DC100%3/3
not &Scalar::Util::looks_like_number($value) or $value - $value != 0 or $value != int $value
not &Scalar::Util::looks_like_number($value) $value - $value != 0 $value != int $value
Truth table100%4/4
not(A) or B or C
A= not &Scalar::Util::looks_like_number($value)B= $value - $value != 0C= $value != int $value
ABCresult
1XX1
01X1
0011
0000
1382 1364 _rule_error($logger, $frules, "$schema_description: Parameter $label ($value) must be an integer");
1383 }
1384 591 $value = int($value);
1385 }
1386 } elsif($t == _T_NUM) {
1387 2392 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2300 92 2392
1388 2300 ▶ if(!Scalar::Util::looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Scalar::Util::looks_like_number($value)) 1472 828 2300
1389 1472 _rule_error($logger, $frules, "$schema_description: Parameter $label must be a number");
1390 }
1391 828 $value = 0 + $value;
1392 }
1393 } elsif($t == _T_BOOL) {
1394 889 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 855 34 889
1395 855 ▶ if(defined(my $b = $Readonly::Values::Boolean::booleans{$value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $b = $Readonly::Values::Boolean::booleans{$value})) { } 141 714 855
1396 141 $value = $b;
1397 } else {
1398 714 _rule_error($logger, $frules, "$schema_description: Parameter $label ($value) must be a boolean");
1399 }
1400 }
1401 } elsif($t == _T_ARRAY) {
1402 1357 ▶ if(defined($value) && ref($value) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and ref $value ne "ARRAY") 1196 161 1357
Condition100%3/3
defined $value and ref $value ne "ARRAY"
!ll&&!rl&&r
521091196
MC/DC100%2/2
defined $value and ref $value ne "ARRAY"
defined $value ref $value ne "ARRAY"
Truth table100%3/3
A and B
A= defined $valueB= ref $value ne "ARRAY"
ABresult
0X0
100
111
1403 1196 _rule_error($logger, $frules, "$schema_description: Parameter $label must be an arrayref, not " . ref($value));
1404 }
1405 } elsif($t == _T_HASH) {
1406 676 ▶ if(defined($value) && ref($value) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and ref $value ne "HASH") 598 78 676
Condition100%3/3
defined $value and ref $value ne "HASH"
!ll&&!rl&&r
2652598
MC/DC100%2/2
defined $value and ref $value ne "HASH"
defined $value ref $value ne "HASH"
Truth table100%3/3
A and B
A= defined $valueB= ref $value ne "HASH"
ABresult
0X0
100
111
1407 598 _rule_error($logger, $frules, "$schema_description: Parameter $label must be an hashref");
1408 }
1409 } elsif($t == _T_SCALAR) {
1410 442 ▶ if(defined($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and ref $value) 119 323 442
Condition100%3/3
defined $value and ref $value
!ll&&!rl&&r
17306119
MC/DC100%2/2
defined $value and ref $value
defined $value ref $value
Truth table100%3/3
A and B
A= defined $valueB= ref $value
ABresult
0X0
100
111
1411 119 _rule_error($logger, $frules, "$schema_description: Parameter $label must be a scalar, not a " . ref($value) . ' reference');
1412 }
1413 } elsif($t == _T_CODE) {
1414 364 ▶ if(defined($value) && ref($value) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and ref $value ne "CODE") 336 28 364
Condition100%3/3
defined $value and ref $value ne "CODE"
!ll&&!rl&&r
1414336
MC/DC100%2/2
defined $value and ref $value ne "CODE"
defined $value ref $value ne "CODE"
Truth table100%3/3
A and B
A= defined $valueB= ref $value ne "CODE"
ABresult
0X0
100
111
1415 336 _rule_error($logger, $frules, "$schema_description: Parameter $label must be a coderef, not a ref to " . ref($value));
1416 }
1417 } else { # _T_OBJECT
1418 364 ▶ if(defined($value) && !Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not builtin::blessed($value)) 336 28 364
Condition100%3/3
defined $value and not builtin::blessed($value)
!ll&&!rl&&r
1414336
MC/DC100%2/2
defined $value and not builtin::blessed($value)
defined $value not builtin::blessed($value)
Truth table100%3/3
A and not(B)
A= defined $valueB= not builtin::blessed($value)
ABresult
0X0
100
111
1419 336 _rule_error($logger, $frules, "$schema_description: Parameter $label must be an object");
1420 }
1421 }
1422  
1423 4046 my $chars; # the character count, computed at most once for min and max
1424 4046 ▶ if(defined(my $min = $f->{'min'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $min = $f->{"min"})) 928 3118 4046
1425 928 my $mk = $f->{'mk'};
1426 928 ▶ if($mk eq 's') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mk eq 's') { } 476 452 928
elsif ($mk eq 'a') { } 54 398 452
elsif ($mk eq 'h') { } 27 371 398
1427 476 ▶ if(!defined($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 24 452 476
elsif (defined(my $len = $chars = _number_of_characters($value))) { } 452 0 452
1428 24 ▶ if($min > 0 && !$f->{'opt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > 0 and not $f->{"opt"}) 18 6 24
Condition100%3/3
$min > 0 and not $f->{"opt"}
!ll&&!rl&&r
3318
MC/DC100%2/2
$min > 0 and not $f->{"opt"}
$min > 0 not $f->{"opt"}
Truth table100%3/3
A and not(B)
A= $min > 0B= not $f->{"opt"}
ABresult
0X0
100
111
1429 18 ▶ _rule_error($logger, $frules, "$schema_description: Parameter $label is undefined, but must be at least $min character" . ($min == 1 ? '' : 's'));
Branch: 1 branch
BranchTrueFalseTotal
$min == 1 ? : 12 6 18
1430 }
1431 } elsif(defined(my $len = ($chars = _number_of_characters($value)))) {
1432 452 ▶ if($len < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $min) 42 410 452
1433 42 _rule_error($logger, $frules, "$schema_description: Parameter $label too short, ($len characters), must be at least $min characters");
1434 }
1435 } else {
1436 0 _rule_error($logger, $frules, "$schema_description: Parameter $label can't be decoded");
1437 }
1438 } elsif($mk eq 'a') {
1439 54 ▶ if(!defined($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 18 36 54
elsif (scalar @{$value;} < $min) { } 14 22 36
1440 18 ▶ if($min > 0 && !$f->{'opt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > 0 and not $f->{"opt"}) 12 6 18
Condition100%3/3
$min > 0 and not $f->{"opt"}
!ll&&!rl&&r
4212
MC/DC100%2/2
$min > 0 and not $f->{"opt"}
$min > 0 not $f->{"opt"}
Truth table100%3/3
A and not(B)
A= $min > 0B= not $f->{"opt"}
ABresult
0X0
100
111
1441 12 ▶ _rule_error($logger, $frules, "$schema_description: Parameter $label is undefined, but must have at least $min member" . ($min > 1 ? 's' : ''));
Branch: 1 branch
BranchTrueFalseTotal
$min > 1 ? : 2 10 12
1442 }
1443 36 } elsif(scalar(@{$value}) < $min) {
1444 14 ▶ _rule_error($logger, $frules, "$schema_description: Parameter $label must have at least $min member" . (($min > 1) ? 's' : ''));
Branch: 1 branch
BranchTrueFalseTotal
$min > 1 ? : 2 12 14
1445 }
1446 } elsif($mk eq 'h') {
1447 27 ▶ if(!defined($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 9 18 27
elsif (scalar keys %{$value;} < $min) { } 7 11 18
1448 9 ▶ if($min > 0 && !$f->{'opt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > 0 and not $f->{"opt"}) 6 3 9
Condition100%3/3
$min > 0 and not $f->{"opt"}
!ll&&!rl&&r
216
MC/DC100%2/2
$min > 0 and not $f->{"opt"}
$min > 0 not $f->{"opt"}
Truth table100%3/3
A and not(B)
A= $min > 0B= not $f->{"opt"}
ABresult
0X0
100
111
1449 6 _rule_error($logger, $frules, "$schema_description: Parameter $label is undefined, but must contain at least $min keys");
1450 }
1451 18 } elsif(scalar(keys(%{$value})) < $min) {
1452 7 _rule_error($logger, $frules, "$schema_description: Parameter $label must contain at least $min keys");
1453 }
1454 } else { # 'n': integer, number or float
1455 371 ▶ if(!defined($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 36 335 371
elsif (&Scalar::Util::looks_like_number($value)) { } 335 0 335
1456 36 ▶ if($min > 0 && !$f->{'opt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > 0 and not $f->{"opt"}) 24 12 36
Condition100%3/3
$min > 0 and not $f->{"opt"}
!ll&&!rl&&r
8424
MC/DC100%2/2
$min > 0 and not $f->{"opt"}
$min > 0 not $f->{"opt"}
Truth table100%3/3
A and not(B)
A= $min > 0B= not $f->{"opt"}
ABresult
0X0
100
111
1457 24 _rule_error($logger, $frules, "$schema_description: Parameter $label is undefined, but must be at least $min");
1458 }
1459 } elsif(Scalar::Util::looks_like_number($value)) {
1460 335 ▶ if($value < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $min) 72 263 335
1461 72 ▶ if($frules->{'error_msg'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($frules->{'error_msg'}) { } 8 64 72
elsif ($f->{'tname'} eq 'integer' and $value == 0) { } 13 51 64
elsif ($f->{'tname'} eq 'integer' and $value == 1) { } 2 49 51
1462 8 _error($logger, $frules->{'error_msg'});
1463 ▶ } elsif(($f->{'tname'} eq 'integer') && ($value == 0)) {
Condition100%3/3
$f->{"tname"} eq "integer" and $value == 0
!ll&&!rl&&r
282313
MC/DC100%2/2
$f->{"tname"} eq "integer" and $value == 0
$f->{"tname"} eq "integer" $value == 0
Truth table100%3/3
A and B
A= $f->{"tname"} eq "integer"B= $value == 0
ABresult
0X0
100
111
1464 13 _error($logger, "$schema_description: Parameter $label ($value) must be a positive number");
1465 ▶ } elsif(($f->{'tname'} eq 'integer') && ($value == 1)) {
Condition100%3/3
$f->{"tname"} eq "integer" and $value == 1
!ll&&!rl&&r
28212
MC/DC100%2/2
$f->{"tname"} eq "integer" and $value == 1
$f->{"tname"} eq "integer" $value == 1
Truth table100%3/3
A and B
A= $f->{"tname"} eq "integer"B= $value == 1
ABresult
0X0
100
111
1466 2 _error($logger, "$schema_description: Parameter $label ($value) must be a positive, non-zero number");
1467 } else {
1468 49 _error($logger, "$schema_description: Parameter $label ($value) must be at least $min");
1469 }
1470 }
1471 } else {
1472 0 _rule_error($logger, $frules, "$schema_description: Parameter $label ($value) must be a number");
1473 }
1474 }
1475 }
1476  
1477 3851 ▶ if(defined(my $max = $f->{'max'}) && defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max = $f->{"max"}) and defined $value) 457 3394 3851
Condition100%3/3
defined(my $max = $f->{"max"}) and defined $value
!ll&&!rl&&r
336430457
MC/DC100%2/2
defined(my $max = $f->{"max"}) and defined $value
defined(my $max = $f->{"max"}) defined $value
Truth table100%3/3
A and B
A= defined(my $max = $f->{"max"})B= defined $value
ABresult
0X0
100
111
1478 457 my $mk = $f->{'mk'};
1479 457 ▶ if($mk eq 's') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mk eq 's') { } 261 196 457
elsif ($mk eq 'a') { } 16 180 196
elsif ($mk eq 'h') { } 8 172 180
1480 261 ▶ if(defined(my $len = $chars // _number_of_characters($value))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $len = $chars // _number_of_characters($value))) { } 261 0 261
Condition66%2/3
$chars // _number_of_characters($value)
l!l&&r!l&&!r
991620
MC/DC0%0/2
$chars // _number_of_characters($value)
$chars _number_of_characters($value)
Truth table66%2/3
A // B
A= $charsB= _number_of_characters($value)
ABresult
1X1
011
000
1481 261 ▶ if($len > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $max) 111 150 261
1482 111 _rule_error($logger, $frules, "$schema_description: Parameter $label too long, ($len characters), must be no longer than $max");
1483 }
1484 } else {
1485 0 _rule_error($logger, $frules, "$schema_description: Parameter $label can't be decoded");
1486 }
1487 } elsif($mk eq 'a') {
1488 16 ▶ if(scalar(@{$value}) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$value;} > $max) 10 6 16
1489 10 _rule_error($logger, $frules, "$schema_description: Parameter $label must contain no more than $max items");
1490 }
1491 } elsif($mk eq 'h') {
1492 8 ▶ if(scalar(keys(%{$value})) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$value;} > $max) 0 8 8
1493 0 _rule_error($logger, $frules, "$schema_description: Parameter $label must contain no more than $max keys");
1494 }
1495 } else { # 'n': integer, number or float
1496 172 ▶ if(Scalar::Util::looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($value)) { } 172 0 172
1497 172 ▶ if($value > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $max) 105 67 172
1498 105 ▶ if($frules->{'error_msg'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($frules->{'error_msg'}) { } 0 105 105
elsif ($f->{'tname'} eq 'integer' and $value == 0) { } 0 105 105
elsif ($f->{'tname'} eq 'integer' and $value == -1) { } 0 105 105
1499 0 _error($logger, $frules->{'error_msg'});
1500 ▶ } elsif(($f->{'tname'} eq 'integer') && ($value == 0)) {
Condition66%2/3
$f->{"tname"} eq "integer" and $value == 0
!ll&&!rl&&r
60450
MC/DC0%0/2
$f->{"tname"} eq "integer" and $value == 0
$f->{"tname"} eq "integer" $value == 0
Truth table66%2/3
A and B
A= $f->{"tname"} eq "integer"B= $value == 0
ABresult
0X0
100
111
1501 0 _error($logger, "$schema_description: Parameter $label ($value) must be a negative number");
1502 ▶ } elsif(($f->{'tname'} eq 'integer') && ($value == -1)) {
Condition66%2/3
$f->{"tname"} eq "integer" and $value == -1
!ll&&!rl&&r
60450
MC/DC0%0/2
$f->{"tname"} eq "integer" and $value == -1
$f->{"tname"} eq "integer" $value == -1
Truth table66%2/3
A and B
A= $f->{"tname"} eq "integer"B= $value == -1
ABresult
0X0
100
111
1503 0 _error($logger, "$schema_description: Parameter $label ($value) must be a negative, non-zero number");
1504 } else {
1505 105 _error($logger, "$schema_description: Parameter $label ($value) must be no more than $max");
1506 }
1507 }
1508 } else {
1509 0 _rule_error($logger, $frules, "$schema_description: Parameter $label ($value) must be a number");
1510 }
1511 }
1512 }
1513  
1514 3625 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3271 354 3625
1515 3271 ▶ if(my $re = $f->{'re'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $re = $f->{"re"}) 435 2836 3271
1516 # As in the generic 'matches' handler, a failed match is raised
1517 # inside an eval and then re-raised wrapped, so the messages agree.
1518 435 eval {
1519 435 ▶ if($value !~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$re/) 332 103 435
1520 332 _rule_error($logger, $frules, "$schema_description: Parameter $label ($value) must match pattern '$re'");
1521 }
1522 103 1;
1523 };
1524 435 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 332 103 435
1525 332 _rule_error($logger, $frules, "$schema_description: Parameter $label regex '$f->{match_src}' error: $@");
1526 }
1527 }
1528 2939 ▶ if(my $list = $f->{'list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list = $f->{"list"}) 725 2214 2939
1529 725 ▶ unless(_value_in_list($value, $list, $f->{'type'}, $frules->{'case_sensitive'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_value_in_list($value, $list, $f->{"type"}, $frules->{"case_sensitive"})) 589 136 725
1530 589 _rule_error($logger, $frules, "$schema_description: Parameter $label ($value) must be one of ", join(', ', @{$list}));
1531 }
1532 }
1533 }
1534  
1535 2704 $validated_args{$key} = $value;
1536 2704 next;
1537 }
1538  
1539 35148 my $rules = $schema->{$key};
1540  
1541 # For named-arg schemas: resolve which input key provides this parameter.
1542 # If the canonical name is absent, try each alias in order.
1543 35148 my $lookup_key = $key;
1544 35148 ▶ if($are_positional_args != 1 && ref($rules) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if ($are_positional_args != 1 and ref $rules eq "HASH" and ref $rules->{"aliases"} eq "ARRAY" and not exists $args->{$key}) 58 35090 35148
Condition100%9/9
$are_positional_args != 1 and ref $rules eq "HASH" and ref $rules->{"aliases"} eq "ARRAY" and not exists $args->{$key}
!ll&&!rl&&r
35088258
$are_positional_args != 1 and ref $rules eq "HASH" and ref $rules->{"aliases"} eq "ARRAY"
!ll&&!rl&&r
3033478560
$are_positional_args != 1 and ref $rules eq "HASH"
!ll&&!rl&&r
4825534845
MC/DC100%4/4
$are_positional_args != 1 and ref $rules eq "HASH" and ref $rules->{"aliases"} eq "ARRAY" and not exists $args->{$key}
$are_positional_args != 1 ref $rules eq "HASH" ref $rules->{"aliases"} eq "ARRAY" not exists $args->{$key}
Truth table100%5/5
A and B and C and not(D)
A= $are_positional_args != 1B= ref $rules eq "HASH"C= ref $rules->{"aliases"} eq "ARRAY"D= not exists $args->{$key}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1545 && ref($rules->{'aliases'}) eq 'ARRAY'
1546 && !exists($args->{$key})) {
1547 58 for my $alias (@{$rules->{'aliases'}}) {
1548 102 ▶ if(exists($args->{$alias})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$alias}) 23 79 102
1549 23 $lookup_key = $alias;
1550 23 last;
1551 }
1552 }
1553 }
1554  
1555 35148 my $value;
1556 35148 ▶ if($are_positional_args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($are_positional_args == 1) { } 48 35100 35148
1557 48 ▶ if(ref($args) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args ne "ARRAY") 0 48 48
1558 0 _error($logger, "::validate_strict: position $rules->{position} given for '$key', but args isn't an array");
1559 }
1560 48 ▶ if(ref($rules) eq 'HASH' && $rules->{'slurp'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq 'HASH' and $rules->{'slurp'}) { } 4 44 48
Condition66%2/3
ref $rules eq "HASH" and $rules->{"slurp"}
!ll&&!rl&&r
0444
MC/DC50%1/2
ref $rules eq "HASH" and $rules->{"slurp"}
ref $rules eq "HASH" $rules->{"slurp"}
Truth table66%2/3
A and B
A= ref $rules eq "HASH"B= $rules->{"slurp"}
ABresult
0X0
100
111
1561 4 my $pos = $rules->{'position'};
1562 4 $value = [@{$args}[$pos .. $#$args]];
1563 } else {
1564 44 $value = $args->[$rules->{'position'}];
1565 }
1566 } else {
1567 35100 $value = $args->{$lookup_key};
1568 }
1569  
1570 35148 ▶ if(!defined($rules)) { # Allow anything
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rules) 56 35092 35148
1571 56 $validated_args{$key} = $value;
1572 56 next;
1573 }
1574  
1575 # If rules are a simple type string
1576 35092 ▶ if(ref($rules) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq "") 179 34913 35092
1577 179 $rules = { type => $rules };
1578 }
1579  
1580 35092 ▶ $has_needs = 1 if(ref($rules) eq 'HASH' && defined($rules->{'needs'}));
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules eq "HASH" and defined $rules->{"needs"} 1035 34057 35092
Condition100%3/3
ref $rules eq "HASH" and defined $rules->{"needs"}
!ll&&!rl&&r
20340371035
MC/DC100%2/2
ref $rules eq "HASH" and defined $rules->{"needs"}
ref $rules eq "HASH" defined $rules->{"needs"}
Truth table100%3/3
A and B
A= ref $rules eq "HASH"B= defined $rules->{"needs"}
ABresult
0X0
100
111
1581  
1582 35092 my $is_optional = 0;
1583  
1584 35092 my $rule_description = $schema_description; # Can be overridden in each element
1585 35092 my $param_label = "'$key'";
1586  
1587 35092 ▶ if(ref($rules) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq "HASH") 35072 20 35092
1588 35072 ▶ if(exists($rules->{'description'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rules->{"description"}) 1899 33173 35072
1589 1899 $param_label = "'$key' ($rules->{description})";
1590 }
1591 # For stringref: validate and dereference before transform so that
1592 # transform (and all subsequent rule handlers) see the plain string.
1593 # Preserve the original ref so optional => CODE receives what the caller passed.
1594 35072 my $pre_deref_value = $value;
1595 35072 ▶ my $is_stringref_type = defined($value) && defined($rules->{'type'}) && !ref($rules->{'type'}) && lc($rules->{'type'}) eq 'stringref';
Condition88%8/9
defined $value && defined $rules->{'type'} && !ref($rules->{'type'}) && lc $rules->{'type'} eq 'stringref'
!ll&&!rl&&r
291932037116
defined $value and defined $rules->{"type"} and not ref $rules->{"type"}
!ll&&!rl&&r
281510432153
defined $value and defined $rules->{"type"}
!ll&&!rl&&r
2815032257
MC/DC75%3/4
defined $value && defined $rules->{'type'} && !ref($rules->{'type'}) && lc $rules->{'type'} eq 'stringref'
defined $value defined $rules->{"type"} not ref $rules->{"type"} lc $rules->{'type'} eq 'stringref'
Truth table80%4/5
A and B and not(C) && D
A= defined $valueB= defined $rules->{"type"}C= not ref $rules->{"type"}D= lc $rules->{'type'} eq 'stringref'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1596 35072 ▶ if($is_stringref_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_stringref_type) 116 34956 35072
1597 116 ▶ if(ref($value) ne 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "SCALAR") 42 74 116
1598 42 ▶ my $got = ref($value) ? 'a ' . ref($value) . ' reference' : 'a plain scalar';
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 26 16 42
1599 42 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a string reference, not $got");
1600 }
1601 74 $value = ${$value};
1602 }
1603 35030 ▶ if($rules->{'transform'} && defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"transform"} and defined $value) 1064 33966 35030
Condition100%3/3
$rules->{"transform"} and defined $value
!ll&&!rl&&r
33891751064
MC/DC100%2/2
$rules->{"transform"} and defined $value
$rules->{"transform"} defined $value
Truth table100%3/3
A and B
A= $rules->{"transform"}B= defined $value
ABresult
0X0
100
111
1604 1064 ▶ if(ref($rules->{'transform'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules->{'transform'} eq 'CODE') { } 1061 3 1064
1605 1061 $value = &{$rules->{'transform'}}($value);
1606 } else {
1607 3 _error($logger, "$rule_description: transforms must be a code ref");
1608 }
1609 }
1610 35027 ▶ if(exists($rules->{optional})) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $rules->{'optional'}) { } 5595 29432 35027
elsif ($rules->{'nullable'}) { } 1195 28237 29432
elsif (defined $rules->{'type'} and not ref $rules->{'type'} and lc $rules->{'type'} eq 'void') { } 17 28220 28237
elsif ($rules->{'slurp'}) { } 4 28216 28220
1611 5595 ▶ if(ref($rules->{'optional'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules->{'optional'} eq 'CODE') { } 991 4604 5595
1612 # For stringref the coderef receives the original SCALAR ref (what
1613 # the caller supplied), not the internally-dereferenced plain string.
1614 # For all other types the post-transform value is passed, as before.
1615 991 ▶ my $opt_arg = $is_stringref_type ? $pre_deref_value : $value;
Branch: 1 branch
BranchTrueFalseTotal
$is_stringref_type ? : 2 989 991
1616 991 $is_optional = &{$rules->{optional}}($opt_arg, $args);
1617 } else {
1618 4604 $is_optional = $rules->{'optional'};
1619 }
1620 } elsif($rules->{nullable}) {
1621 1195 $is_optional = $rules->{'nullable'};
1622 ▶ } elsif(defined($rules->{'type'}) && !ref($rules->{'type'}) && lc($rules->{'type'}) eq 'void') {
Condition83%5/6
defined $rules->{"type"} and not ref $rules->{"type"} and lc $rules->{"type"} eq "void"
!ll&&!rl&&r
1042811617
defined $rules->{"type"} and not ref $rules->{"type"}
!ll&&!rl&&r
010428133
MC/DC66%2/3
defined $rules->{"type"} and not ref $rules->{"type"} and lc $rules->{"type"} eq "void"
defined $rules->{"type"} not ref $rules->{"type"} lc $rules->{"type"} eq "void"
Truth table75%3/4
A and not(B) and C
A= defined $rules->{"type"}B= not ref $rules->{"type"}C= lc $rules->{"type"} eq "void"
ABCresult
0XX0
10X0
1100
1111
1623 17 $is_optional = 1;
1624 } elsif($rules->{'slurp'}) {
1625 4 $is_optional = 1;
1626 }
1627 }
1628  
1629 # Handle optional parameters
1630 35047 ▶ if((ref($rules) eq 'HASH') && $is_optional) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rules eq 'HASH' and $is_optional) { } 5825 29222 35047
elsif (ref $args eq 'HASH' and not exists $args->{$lookup_key}) { } 1044 28215 29259
Condition100%3/3
ref $rules eq "HASH" and $is_optional
!ll&&!rl&&r
20292025825
MC/DC100%2/2
ref $rules eq "HASH" and $is_optional
ref $rules eq "HASH" $is_optional
Truth table100%3/3
A and B
A= ref $rules eq "HASH"B= $is_optional
ABresult
0X0
100
111
1631 5825 my $missing;
1632 5825 ▶ if($are_positional_args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($are_positional_args == 1) { } 10 5815 5825
1633 # A slurp parameter is never missing: at worst it yields an empty arrayref.
1634 10 ▶ $missing = $rules->{'slurp'} ? 0 : !defined($args->[$rules->{position}]);
Branch: 1 branch
BranchTrueFalseTotal
$rules->{'slurp'} ? : 4 6 10
1635 } else {
1636 5815 $missing = !exists($args->{$lookup_key});
1637 }
1638 5825 ▶ if($missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing) 549 5276 5825
1639 549 ▶ if($are_positional_args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($are_positional_args == 1) 5 544 549
1640 5 ▶ if(scalar(@{$args}) < $rules->{'position'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$args;} < $rules->{"position"}) 1 4 5
1641 # arg array is too short, so it must be missing
1642 1 _error($logger, "$rule_description: Required parameter $param_label is missing");
1643 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
1644 }
1645 }
1646 548 ▶ if(exists($rules->{'default'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rules->{"default"}) 62 486 548
1647 # Populate missing optional parameters with the specified output values
1648 62 ▶ $validated_args{$key} //= $rules->{'default'};
Condition66%2/3
$validated_args{$key} //= $rules->{"default"}
l!l&&r!l&&!r
0611
MC/DC50%1/2
$validated_args{$key} //= $rules->{"default"}
$validated_args{$key} $rules->{"default"}
Truth table66%2/3
A //= B
A= $validated_args{$key}B= $rules->{"default"}
ABresult
1X1
011
000
1649 62 next; # default wins; do not fall through to the schema branch
1650 }
1651  
1652 486 ▶ if($rules->{'schema'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{'schema'}) { } 40 446 486
1653 40 $value = _apply_nested_defaults({}, $rules->{'schema'});
1654 40 ▶ next unless scalar(%{$value});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar %{$value;} 38 2 40
1655 # The nested schema has a default value
1656 } else {
1657 446 next; # optional and missing
1658 }
1659 }
1660 ▶ } elsif((ref($args) eq 'HASH') && !exists($args->{$lookup_key})) {
Condition100%3/3
ref $args eq "HASH" and not exists $args->{$lookup_key}
!ll&&!rl&&r
37281411044
MC/DC100%2/2
ref $args eq "HASH" and not exists $args->{$lookup_key}
ref $args eq "HASH" not exists $args->{$lookup_key}
Truth table100%3/3
A and not(B)
A= ref $args eq "HASH"B= not exists $args->{$lookup_key}
ABresult
0X0
100
111
1661 # The parameter is required
1662 # Use exists rather than defined, so that an undefined value can be passed, but the key is there
1663 1044 _error($logger, "$rule_description: Required parameter $param_label is missing");
1664 }
1665  
1666 # Normalise union type shorthand: { type => ['string', 'integer'], ... }
1667 # or pipe-separated string { type => 'string|integer' }
1668 # into the array-of-rules form that the ARRAY handler below already supports.
1669 # Each candidate type inherits all other constraints from the parent rule
1670 # (min, max, matches, optional, etc.) so they are each fully validated.
1671 # Must run after optional/transform handling above but before rule dispatch below.
1672 33456 ▶ if(ref($rules) eq 'HASH' && !ref($rules->{'type'}) && defined($rules->{'type'}) && index($rules->{'type'}, '|') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq "HASH" and not ref $rules->{"type"} and defined $rules->{"type"} and index($rules->{"type"}, "|") != -1) 90 33366 33456
Condition88%8/9
ref $rules eq "HASH" and not ref $rules->{"type"} and defined $rules->{"type"} and index($rules->{"type"}, "|") != -1
!ll&&!rl&&r
1243324290
ref $rules eq "HASH" and not ref $rules->{"type"} and defined $rules->{"type"}
!ll&&!rl&&r
124033332
ref $rules eq "HASH" and not ref $rules->{"type"}
!ll&&!rl&&r
2010433332
MC/DC75%3/4
ref $rules eq "HASH" and not ref $rules->{"type"} and defined $rules->{"type"} and index($rules->{"type"}, "|") != -1
ref $rules eq "HASH" not ref $rules->{"type"} defined $rules->{"type"} index($rules->{"type"}, "|") != -1
Truth table80%4/5
A and not(B) and C and D
A= ref $rules eq "HASH"B= not ref $rules->{"type"}C= defined $rules->{"type"}D= index($rules->{"type"}, "|") != -1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1673 90 $rules = { %$rules, type => [ split /\s*\|\s*/, $rules->{'type'} ] };
1674 }
1675 33456 ▶ if(ref($rules) eq 'HASH' && ref($rules->{'type'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq "HASH" and ref $rules->{"type"} eq "ARRAY") 194 33262 33456
Condition100%3/3
ref $rules eq "HASH" and ref $rules->{"type"} eq "ARRAY"
!ll&&!rl&&r
2033242194
MC/DC100%2/2
ref $rules eq "HASH" and ref $rules->{"type"} eq "ARRAY"
ref $rules eq "HASH" ref $rules->{"type"} eq "ARRAY"
Truth table100%3/3
A and B
A= ref $rules eq "HASH"B= ref $rules->{"type"} eq "ARRAY"
ABresult
0X0
100
111
1676 194 my %base = %{$rules};
1677 194 my @type_list = @{delete $base{'type'}};
1678 194 ▶ if(!@type_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@type_list) 2 192 194
1679 2 _error($logger, "$rule_description: Parameter $param_label: union type list must not be empty");
1680 }
1681 # Expand into one full rule hash per candidate type
1682 192 $rules = [ map { { %base, type => $_ } } @type_list ];
1683 }
1684  
1685 # Validate based on rules
1686 33454 ▶ if(ref($rules) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $rules eq 'HASH') { } 33242 212 33454
elsif (ref $rules eq 'ARRAY') { } 210 2 212
elsif (ref $rules) { } 2 0 2
1687 33242 ▶ if(defined(my $min = $rules->{'min'} // $rules->{'minimum'}) && defined(my $max = $rules->{'max'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $min = $rules->{"min"} // $rules->{"minimum"}) and defined(my $max = $rules->{"max"})) 2493 30749 33242
Condition100%6/6
defined(my $min = $rules->{"min"} // $rules->{"minimum"}) and defined(my $max = $rules->{"max"})
!ll&&!rl&&r
2372070292493
$rules->{"min"} // $rules->{"minimum"}
l!l&&r!l&&!r
9513923720
MC/DC100%3/3
defined(my $min = $rules->{"min"} // $rules->{"minimum"}) and defined(my $max = $rules->{"max"})
$rules->{"min"} $rules->{"minimum"} defined(my $max = $rules->{"max"})
Truth table100%5/5
A // B and C
A= $rules->{"min"}B= $rules->{"minimum"}C= defined(my $max = $rules->{"max"})
ABCresult
00X0
1X00
0100
1X11
0111
1688 2493 ▶ if($min > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $max) 943 1550 2493
1689 943 _error($logger, "validate_strict($key): min must be <= max ($min > $max)");
1690 }
1691 }
1692  
1693 # memberof and its synonym enum cannot be combined with min or max
1694 32299 ▶ if($rules->{'memberof'} || $rules->{'enum'} || $rules->{'values'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"memberof"} or $rules->{"enum"} or $rules->{"values"}) 4345 27954 32299
Condition100%6/6
$rules->{"memberof"} or $rules->{"enum"} or $rules->{"values"}
l!l&&r!l&&!r
386947627954
$rules->{"memberof"} or $rules->{"enum"}
l!l&&r!l&&!r
338848128430
MC/DC100%3/3
$rules->{"memberof"} or $rules->{"enum"} or $rules->{"values"}
$rules->{"memberof"} $rules->{"enum"} $rules->{"values"}
Truth table100%4/4
A or B or C
A= $rules->{"memberof"}B= $rules->{"enum"}C= $rules->{"values"}
ABCresult
1XX1
01X1
0011
0000
1695 4345 ▶ if(defined(my $min = $rules->{'min'} // $rules->{'minimum'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $min = $rules->{"min"} // $rules->{"minimum"})) 944 3401 4345
Condition66%2/3
$rules->{"min"} // $rules->{"minimum"}
l!l&&r!l&&!r
94403401
MC/DC50%1/2
$rules->{"min"} // $rules->{"minimum"}
$rules->{"min"} $rules->{"minimum"}
Truth table66%2/3
A // B
A= $rules->{"min"}B= $rules->{"minimum"}
ABresult
1X1
011
000
1696 944 _error($logger, "validate_strict($key): min ($min) makes no sense with memberof/enum/values");
1697 }
1698 3401 ▶ if(defined(my $max = $rules->{'max'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max = $rules->{"max"})) 5 3396 3401
1699 5 _error($logger, "validate_strict($key): max ($max) makes no sense with memberof/enum/values");
1700 }
1701 }
1702  
1703 31350 foreach my $rule_name ('type', grep { $_ ne 'type' } keys %$rules) {
1704 44785 my $rule_value = $rules->{$rule_name};
1705  
1706 44785 ▶ if((ref($rule_value) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value eq "CODE" and $rule_name ne "validate" and $rule_name ne "callback" and $rule_name ne "validator" and $rule_name ne "transform" and $rule_name ne "optional") 16 44769 44785
Condition100%15/15
ref $rule_value eq "CODE" and $rule_name ne "validate" and $rule_name ne "callback" and $rule_name ne "validator" and $rule_name ne "transform" and $rule_name ne "optional"
!ll&&!rl&&r
4443733216
ref $rule_value eq "CODE" and $rule_name ne "validate" and $rule_name ne "callback" and $rule_name ne "validator" and $rule_name ne "transform"
!ll&&!rl&&r
43926511348
ref $rule_value eq "CODE" and $rule_name ne "validate" and $rule_name ne "callback" and $rule_name ne "validator"
!ll&&!rl&&r
439224859
ref $rule_value eq "CODE" and $rule_name ne "validate" and $rule_name ne "callback"
!ll&&!rl&&r
43148774863
ref $rule_value eq "CODE" and $rule_name ne "validate"
!ll&&!rl&&r
4313991637
MC/DC100%6/6
ref $rule_value eq "CODE" and $rule_name ne "validate" and $rule_name ne "callback" and $rule_name ne "validator" and $rule_name ne "transform" and $rule_name ne "optional"
ref $rule_value eq "CODE" $rule_name ne "validate" $rule_name ne "callback" $rule_name ne "validator" $rule_name ne "transform" $rule_name ne "optional"
Truth table100%7/7
A and B and C and D and E and F
A= ref $rule_value eq "CODE"B= $rule_name ne "validate"C= $rule_name ne "callback"D= $rule_name ne "validator"E= $rule_name ne "transform"F= $rule_name ne "optional"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
1707 && ($rule_name ne 'validate')
1708 && ($rule_name ne 'callback')
1709 && ($rule_name ne 'validator')
1710 && ($rule_name ne 'transform') # already applied before this loop
1711 && ($rule_name ne 'optional')) { # already applied before this loop
1712 16 $rule_value = &{$rule_value}($value, $args);
1713 }
1714  
1715 # Better OOP, the routine has been given an object rather than a scalar
1716 44785 ▶ if(ref($rule_value) && Scalar::Util::blessed($rule_value) && $rule_value->can('as_string')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value and builtin::blessed($rule_value) and $rule_value->can("as_string")) 2 44783 44785
Condition100%6/6
ref $rule_value and builtin::blessed($rule_value) and $rule_value->can("as_string")
!ll&&!rl&&r
442625212
ref $rule_value and builtin::blessed($rule_value)
!ll&&!rl&&r
404903772523
MC/DC100%3/3
ref $rule_value and builtin::blessed($rule_value) and $rule_value->can("as_string")
ref $rule_value builtin::blessed($rule_value) $rule_value->can("as_string")
Truth table100%4/4
A and B and C
A= ref $rule_valueB= builtin::blessed($rule_value)C= $rule_value->can("as_string")
ABCresult
0XX0
10X0
1100
1111
1717 2 $rule_value = $rule_value->as_string();
1718 }
1719  
1720 44785 ▶ if($rule_name eq 'type') {
Branch: 34 branches
BranchTrueFalseTotal
if ($rule_name eq 'type') { } 31350 13435 44785
elsif ($rule_name eq 'min' or $rule_name eq 'minimum') { } 3050 10385 13435
elsif ($rule_name eq 'max') { } 1200 9185 10385
elsif ($rule_name eq 'matches' or $rule_name eq 'regex') { } 1003 8182 9185
elsif ($rule_name eq 'nomatch') { } 352 7830 8182
elsif ($rule_name eq 'bnf') { } 26 7804 7830
elsif ($rule_name eq 'memberof' or $rule_name eq 'enum' or $rule_name eq 'values') { } 1260 6544 7804
elsif ($rule_name eq 'notmemberof') { } 376 6168 6544
elsif ($rule_name eq 'isa') { } 22 6146 6168
elsif ($rule_name eq 'can') { } 44 6102 6146
elsif ($rule_name eq 'classcan') { } 6 6096 6102
elsif ($rule_name eq 'does') { } 3 6093 6096
elsif ($rule_name eq 'classisa') { } 3 6090 6093
elsif ($rule_name eq 'subclass') { } 2 6088 6090
elsif ($rule_name eq 'classdoes') { } 2 6086 6088
elsif ($rule_name eq 'driver') { } 3 6083 6086
elsif ($rule_name eq 'element_type') { } 43 6040 6083
elsif ($rule_name eq 'element_isa') { } 5 6035 6040
elsif ($rule_name eq 'needs') { } 353 5682 6035
elsif ($rule_name eq 'optional') { } 1639 4043 5682
elsif ($rule_name eq 'nullable') { } 514 3529 4043
elsif ($rule_name eq 'default') { } 350 3179 3529
elsif ($rule_name eq 'error_msg') { } 345 2834 3179
elsif ($rule_name eq 'transform') { } 511 2323 2834
elsif ($rule_name eq 'case_sensitive') { } 517 1806 2323
elsif ($rule_name eq 'description') { } 497 1309 1806
elsif ($rule_name =~ /^_/) { } 0 1309 1309
elsif ($rule_name eq 'semantic') { } 36 1273 1309
elsif ($rule_name eq 'schema') { } 88 1185 1273
elsif ($rule_name eq 'validate' or $rule_name eq 'validator') { } 15 1170 1185
elsif ($rule_name eq 'callback') { } 775 395 1170
elsif ($rule_name eq 'position') { } 39 356 395
elsif ($rule_name eq 'slurp') { } 3 353 356
elsif ($rule_name eq 'aliases') { } 25 328 353
1721 31350 my $type = lc($rule_value);
1722  
1723 31350 ▶ if(($type eq 'string') || ($type eq 'str')) {
Branch: 19 branches
BranchTrueFalseTotal
if ($type eq 'string' or $type eq 'str') { } 5616 25734 31350
elsif ($type eq 'integer' or $type eq 'int') { } 5071 20663 25734
elsif ($type eq 'number' or $type eq 'float' or $type eq 'num' or $type eq 'double') { } 6438 14225 20663
elsif ($type eq 'arrayref') { } 3234 10991 14225
elsif ($type eq 'hashref') { } 1616 9375 10991
elsif ($type eq 'scalar') { } 1860 7515 9375
elsif ($type eq 'scalarref') { } 71 7444 7515
elsif ($type eq 'stringref') { } 76 7368 7444
elsif ($type eq 'void') { } 16 7352 7368
elsif ($type eq 'boolean' or $type eq 'bool') { } 3555 3797 7352
elsif ($type eq 'coderef') { } 1830 1967 3797
elsif ($type eq 'regex') { } 3 1964 1967
elsif ($type eq 'handle') { } 3 1961 1964
elsif ($type eq 'arraylike') { } 2 1959 1961
elsif ($type eq 'hashlike') { } 2 1957 1959
elsif ($type eq 'codelike') { } 2 1955 1957
elsif ($type eq 'invocant') { } 8 1947 1955
elsif ($type eq 'object') { } 1882 65 1947
elsif (my $custom_type = $custom_types->{$type}) { } 62 3 65
Condition100%3/3
$type eq "string" or $type eq "str"
l!l&&r!l&&!r
4155146125734
MC/DC100%2/2
$type eq "string" or $type eq "str"
$type eq "string" $type eq "str"
Truth table100%3/3
A or B
A= $type eq "string"B= $type eq "str"
ABresult
1X1
011
000
1724 5616 ▶ if(ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 1176 4440 5616
1725 1176 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a string");
1726 }
1727 4440 ▶ unless((ref($value) eq '') || (defined($value) && length($value))) { # Allow undef for optional strings
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "" or defined $value and length $value) 0 4440 4440
Condition16%1/6
ref $value eq "" or defined $value and length $value
l!l&&r!l&&!r
444000
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value eq "" or defined $value and length $value
ref $value eq "" defined $value length $value
Truth table25%1/4
A or B and C
A= ref $value eq ""B= defined $valueC= length $value
ABCresult
1XX1
0111
00X0
0100
1728 0 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a string");
1729 }
1730 ▶ } elsif(($type eq 'integer') || ($type eq 'int')) {
Condition100%3/3
$type eq "integer" or $type eq "int"
l!l&&r!l&&!r
3328174320663
MC/DC100%2/2
$type eq "integer" or $type eq "int"
$type eq "integer" $type eq "int"
Truth table100%3/3
A or B
A= $type eq "integer"B= $type eq "int"
ABresult
1X1
011
000
1731 5071 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 185 4886 5071
1732 185 next; # Skip if number is undefined
1733 }
1734 4886 ▶ if(!Scalar::Util::looks_like_number($value) || ($value - $value) != 0 || $value != int($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &Scalar::Util::looks_like_number($value) or $value - $value != 0 or $value != int $value) 3261 1625 4886
Condition100%6/6
not &Scalar::Util::looks_like_number($value) or $value - $value != 0 or $value != int $value
l!l&&r!l&&!r
30761851625
not &Scalar::Util::looks_like_number($value) or $value - $value != 0
l!l&&r!l&&!r
28941821810
MC/DC100%3/3
not &Scalar::Util::looks_like_number($value) or $value - $value != 0 or $value != int $value
not &Scalar::Util::looks_like_number($value) $value - $value != 0 $value != int $value
Truth table100%4/4
not(A) or B or C
A= not &Scalar::Util::looks_like_number($value)B= $value - $value != 0C= $value != int $value
ABCresult
1XX1
01X1
0011
0000
1735 3261 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must be an integer");
1736 }
1737 1625 $value = int($value); # Coerce to integer
1738 ▶ } elsif(($type eq 'number') || ($type eq 'float') || ($type eq 'num') || ($type eq 'double')) {
Condition100%9/9
$type eq "number" or $type eq "float" or $type eq "num" or $type eq "double"
l!l&&r!l&&!r
4694174414225
$type eq "number" or $type eq "float" or $type eq "num"
l!l&&r!l&&!r
2950174415969
$type eq "number" or $type eq "float"
l!l&&r!l&&!r
1508144217713
MC/DC100%4/4
$type eq "number" or $type eq "float" or $type eq "num" or $type eq "double"
$type eq "number" $type eq "float" $type eq "num" $type eq "double"
Truth table100%5/5
A or B or C or D
A= $type eq "number"B= $type eq "float"C= $type eq "num"D= $type eq "double"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1739 6438 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 246 6192 6438
1740 246 next; # Skip if number is undefined
1741 }
1742 6192 ▶ if(!Scalar::Util::looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Scalar::Util::looks_like_number($value)) 3909 2283 6192
1743 3909 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a number");
1744 }
1745 # $value = eval $value; # Coerce to number (be careful with eval)
1746 2283 $value = 0 + $value; # Numeric coercion
1747 } elsif($type eq 'arrayref') {
1748 3234 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 119 3115 3234
1749 119 next; # Skip if arrayref is undefined
1750 }
1751 3115 ▶ if(ref($value) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY") 2698 417 3115
1752 2698 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be an arrayref, not " . ref($value));
1753 }
1754 } elsif($type eq 'hashref') {
1755 1616 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 61 1555 1616
1756 61 next; # Skip if hashref is undefined
1757 }
1758 1555 ▶ if(ref($value) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "HASH") 1343 212 1555
1759 1343 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be an hashref");
1760 }
1761 } elsif($type eq 'scalar') {
1762 1860 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 70 1790 1860
1763 70 next; # Skip if undefined
1764 }
1765 1790 ▶ if(ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 502 1288 1790
1766 502 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a scalar, not a " . ref($value) . ' reference');
1767 }
1768 } elsif($type eq 'scalarref') {
1769 71 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 2 69 71
1770 2 next; # Skip if undefined
1771 }
1772 69 ▶ if(ref($value) ne 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "SCALAR") 41 28 69
1773 41 ▶ my $got = ref($value) ? 'a ' . ref($value) . ' reference' : 'a plain scalar';
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 23 18 41
1774 41 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a scalar reference, not $got");
1775 }
1776 } elsif($type eq 'stringref') {
1777 76 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 2 74 76
1778 2 next; # Skip if undefined
1779 }
1780 # The early-deref block validated the SCALAR ref and set $value to the
1781 # plain string. If transform subsequently returned a reference, reject it.
1782 74 ▶ if(ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 2 72 74
1783 2 _rule_error($logger, $rules, "$rule_description: Parameter $param_label stringref transform must return a plain string, not a " . ref($value) . ' reference');
1784 }
1785 } elsif($type eq 'void') {
1786 16 ▶ if(scalar(keys %{$schema}) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$schema;} != 1) 2 14 16
1787 2 _error($logger, "$rule_description: type 'void' requires exactly one parameter in the schema");
1788 }
1789 14 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 11 3 14
1790 11 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be undef (void type accepts no value)");
1791 }
1792 ▶ } elsif(($type eq 'boolean') || ($type eq 'bool')) {
Condition100%3/3
$type eq "boolean" or $type eq "bool"
l!l&&r!l&&!r
180917463797
MC/DC100%2/2
$type eq "boolean" or $type eq "bool"
$type eq "boolean" $type eq "bool"
Truth table100%3/3
A or B
A= $type eq "boolean"B= $type eq "bool"
ABresult
1X1
011
000
1793 3555 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 136 3419 3555
1794 136 next; # Skip if bool is undefined
1795 }
1796 3419 ▶ if(defined(my $b = $Readonly::Values::Boolean::booleans{$value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $b = $Readonly::Values::Boolean::booleans{$value})) { } 598 2821 3419
1797 598 $value = $b;
1798 } else {
1799 2821 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must be a boolean");
1800 }
1801 } elsif($type eq 'coderef') {
1802 1830 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 71 1759 1830
1803 71 next; # Skip if code is undefined
1804 }
1805 1759 ▶ if(ref($value) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "CODE") 1685 74 1759
1806 1685 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a coderef, not a ref to " . ref($value));
1807 }
1808 } elsif($type eq 'regex') {
1809 3 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 2 3
1810 1 next;
1811 }
1812 2 ▶ if(ref($value) ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "Regexp") 1 1 2
1813 1 ▶ _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a compiled regex (qr//)" .
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 1 1
1814 (ref($value) ? ", not a " . ref($value) . " reference" : ", not a plain scalar"));
1815 }
1816 } elsif($type eq 'handle') {
1817 3 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 2 3
1818 1 next;
1819 }
1820 2 my $is_handle = 0;
1821 2 ▶ if(ref($value) eq 'GLOB' && defined(fileno($value))) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'GLOB' and defined fileno $value) { } 1 1 2
elsif (builtin::blessed($value) and $value->isa('IO::Handle')) { } 0 1 1
Condition66%2/3
ref $value eq "GLOB" and defined fileno $value
!ll&&!rl&&r
101
MC/DC50%1/2
ref $value eq "GLOB" and defined fileno $value
ref $value eq "GLOB" defined fileno $value
Truth table66%2/3
A and B
A= ref $value eq "GLOB"B= defined fileno $value
ABresult
0X0
100
111
1822 1 $is_handle = 1;
1823 ▶ } elsif(Scalar::Util::blessed($value) && $value->isa('IO::Handle')) {
Condition33%1/3
builtin::blessed($value) and $value->isa("IO::Handle")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($value) and $value->isa("IO::Handle")
builtin::blessed($value) $value->isa("IO::Handle")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("IO::Handle")
ABresult
0X0
100
111
1824 0 $is_handle = 1;
1825 } else {
1826 1 $is_handle = defined(eval { fileno($value) });
1827 }
1828 2 ▶ unless($is_handle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_handle) 1 1 2
1829 1 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a file handle");
1830 }
1831 } elsif($type eq 'arraylike') {
1832 2 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
1833 0 next;
1834 }
1835 2 ▶ unless(ref($value) eq 'ARRAY' || (Scalar::Util::blessed($value) && overload::Method($value, '@{}'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY" or builtin::blessed($value) and overload::Method($value, "\@{}")) 1 1 2
Condition50%3/6
ref $value eq "ARRAY" or builtin::blessed($value) and overload::Method($value, "\@{}")
l!l&&r!l&&!r
101
builtin::blessed($value) and overload::Method($value, "\@{}")
!ll&&!rl&&r
100
MC/DC33%1/3
ref $value eq "ARRAY" or builtin::blessed($value) and overload::Method($value, "\@{}")
ref $value eq "ARRAY" builtin::blessed($value) overload::Method($value, "\@{}")
Truth table50%2/4
A or B and C
A= ref $value eq "ARRAY"B= builtin::blessed($value)C= overload::Method($value, "\@{}")
ABCresult
1XX1
0111
00X0
0100
1836 1 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be an array reference or array-like object");
1837 }
1838 } elsif($type eq 'hashlike') {
1839 2 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
1840 0 next;
1841 }
1842 2 ▶ unless(ref($value) eq 'HASH' || (Scalar::Util::blessed($value) && overload::Method($value, '%{}'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "HASH" or builtin::blessed($value) and overload::Method($value, "%{}")) 1 1 2
Condition50%3/6
ref $value eq "HASH" or builtin::blessed($value) and overload::Method($value, "%{}")
l!l&&r!l&&!r
101
builtin::blessed($value) and overload::Method($value, "%{}")
!ll&&!rl&&r
100
MC/DC33%1/3
ref $value eq "HASH" or builtin::blessed($value) and overload::Method($value, "%{}")
ref $value eq "HASH" builtin::blessed($value) overload::Method($value, "%{}")
Truth table50%2/4
A or B and C
A= ref $value eq "HASH"B= builtin::blessed($value)C= overload::Method($value, "%{}")
ABCresult
1XX1
0111
00X0
0100
1843 1 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a hash reference or hash-like object");
1844 }
1845 } elsif($type eq 'codelike') {
1846 2 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
1847 0 next;
1848 }
1849 2 ▶ unless(ref($value) eq 'CODE' || (Scalar::Util::blessed($value) && overload::Method($value, '&{}'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "CODE" or builtin::blessed($value) and overload::Method($value, "&{}")) 1 1 2
Condition50%3/6
ref $value eq "CODE" or builtin::blessed($value) and overload::Method($value, "&{}")
l!l&&r!l&&!r
101
builtin::blessed($value) and overload::Method($value, "&{}")
!ll&&!rl&&r
100
MC/DC33%1/3
ref $value eq "CODE" or builtin::blessed($value) and overload::Method($value, "&{}")
ref $value eq "CODE" builtin::blessed($value) overload::Method($value, "&{}")
Truth table50%2/4
A or B and C
A= ref $value eq "CODE"B= builtin::blessed($value)C= overload::Method($value, "&{}")
ABCresult
1XX1
0111
00X0
0100
1850 1 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a code reference or code-like object");
1851 }
1852 } elsif($type eq 'invocant') {
1853 8 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 8 8
1854 0 next;
1855 }
1856 8 ▶ unless(Scalar::Util::blessed($value) || (!ref($value) && $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value) or not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) 1 7 8
Condition83%5/6
builtin::blessed($value) or not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
l!l&&r!l&&!r
341
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
014
MC/DC66%2/3
builtin::blessed($value) or not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
builtin::blessed($value) not ref $value $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
Truth table75%3/4
A or not(B) and C
A= builtin::blessed($value)B= not ref $valueC= $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
ABCresult
1XX1
0111
00X0
0100
1857 1 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be a blessed object or a class name");
1858 }
1859 } elsif($type eq 'object') {
1860 1882 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 71 1811 1882
1861 71 next; # Skip if object is undefined
1862 }
1863 1811 ▶ if(!Scalar::Util::blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value)) 1686 125 1811
1864 1686 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must be an object");
1865 }
1866 } elsif(my $custom_type = $custom_types->{$type}) {
1867 62 ▶ if($custom_type->{'transform'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_type->{"transform"}) 9 53 62
1868 # The custom type has a transform embedded within it
1869 9 ▶ if(ref($custom_type->{'transform'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $custom_type->{'transform'} eq 'CODE') { } 8 1 9
1870 8 $value = &{$custom_type->{'transform'}}($value);
1871 } else {
1872 1 _error($logger, "$rule_description: transforms must be a code ref");
1873 }
1874 }
1875 61 validate_strict({ input => { $key => $value }, schema => { $key => $custom_type }, custom_types => $custom_types });
1876 } else {
1877 3 _error($logger, "$rule_description: Unknown type '$type'");
1878 }
1879 ▶ } elsif(($rule_name eq 'min') || ($rule_name eq 'minimum')) {
Condition100%3/3
$rule_name eq "min" or $rule_name eq "minimum"
l!l&&r!l&&!r
3041910385
MC/DC100%2/2
$rule_name eq "min" or $rule_name eq "minimum"
$rule_name eq "min" $rule_name eq "minimum"
Truth table100%3/3
A or B
A= $rule_name eq "min"B= $rule_name eq "minimum"
ABresult
1X1
011
000
1880 3050 ▶ if(!defined($rules->{'type'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rules->{"type"}) 0 3050 3050
1881 0 _error($logger, "$rule_description: Don't know type of $param_label to determine its minimum value $rule_value");
1882 }
1883 3050 my $type = lc($rules->{'type'});
1884 3050 ▶ if(exists($custom_types->{$type}->{'min'}) || exists($custom_types->{$type}->{minimum})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $custom_types->{$type}{"min"} or exists $custom_types->{$type}{"minimum"}) 3 3047 3050
Condition66%2/3
exists $custom_types->{$type}{"min"} or exists $custom_types->{$type}{"minimum"}
l!l&&r!l&&!r
303047
MC/DC50%1/2
exists $custom_types->{$type}{"min"} or exists $custom_types->{$type}{"minimum"}
exists $custom_types->{$type}{"min"} exists $custom_types->{$type}{"minimum"}
Truth table66%2/3
A or B
A= exists $custom_types->{$type}{"min"}B= exists $custom_types->{$type}{"minimum"}
ABresult
1X1
011
000
1885 3 ▶ $rule_value = $custom_types->{$type}->{'min'} // $custom_types->{$type}->{minimum};
Condition33%1/3
$custom_types->{$type}{"min"} // $custom_types->{$type}{"minimum"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$custom_types->{$type}{"min"} // $custom_types->{$type}{"minimum"}
$custom_types->{$type}{"min"} $custom_types->{$type}{"minimum"}
Truth table33%1/3
A // B
A= $custom_types->{$type}{"min"}B= $custom_types->{$type}{"minimum"}
ABresult
1X1
011
000
1886 3 $type = $custom_types->{$type}->{'type'};
1887 }
1888 3050 ▶ if(($type eq 'string') || ($type eq 'str') || ($type eq 'stringref')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'string' or $type eq 'str' or $type eq 'stringref') { } 933 2117 3050
elsif ($type eq 'arrayref') { } 183 1934 2117
elsif ($type eq 'hashref') { } 55 1879 1934
elsif ($type eq 'integer' or $type eq 'number' or $type eq 'float') { } 641 1238 1879
Condition100%6/6
$type eq "string" or $type eq "str" or $type eq "stringref"
l!l&&r!l&&!r
916172117
$type eq "string" or $type eq "str"
l!l&&r!l&&!r
6522642134
MC/DC100%3/3
$type eq "string" or $type eq "str" or $type eq "stringref"
$type eq "string" $type eq "str" $type eq "stringref"
Truth table100%4/4
A or B or C
A= $type eq "string"B= $type eq "str"C= $type eq "stringref"
ABCresult
1XX1
01X1
0011
0000
1889 933 ▶ if($rule_value < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value < 0) 117 816 933
1890 117 _rule_error($logger, $rules, "$rule_description: Parameter $param_label has meaningless minimum value that is less than zero");
1891 }
1892 816 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 39 777 816
1893 39 ▶ if($rule_value > 0 && !$is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value > 0 and not $is_optional) 19 20 39
Condition100%3/3
$rule_value > 0 and not $is_optional
!ll&&!rl&&r
16419
MC/DC100%2/2
$rule_value > 0 and not $is_optional
$rule_value > 0 not $is_optional
Truth table100%3/3
A and not(B)
A= $rule_value > 0B= not $is_optional
ABresult
0X0
100
111
1894 19 ▶ _rule_error($logger, $rules, "$rule_description: Parameter $param_label is undefined, but must be at least $rule_value character" . ($rule_value == 1 ? '' : 's'));
Branch: 1 branch
BranchTrueFalseTotal
$rule_value == 1 ? : 13 6 19
1895 0 $invalid_args{$key} = 1;
1896 }
1897 20 next;
1898 }
1899 777 ▶ if(defined(my $len = _number_of_characters($value))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $len = _number_of_characters($value))) { } 777 0 777
1900 777 ▶ if($len < $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $rule_value) 79 698 777
1901 79 _rule_error($logger, $rules, "$rule_description: Parameter $param_label too short, ($len characters), must be at least $rule_value characters");
1902 0 $invalid_args{$key} = 1;
1903 }
1904 } else {
1905 0 _rule_error($logger, $rules, "$rule_description: Parameter $param_label can't be decoded");
1906 0 $invalid_args{$key} = 1;
1907 }
1908 } elsif($type eq 'arrayref') {
1909 183 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 27 156 183
1910 27 ▶ if($rule_value > 0 && !$is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value > 0 and not $is_optional) 13 14 27
Condition100%3/3
$rule_value > 0 and not $is_optional
!ll&&!rl&&r
12213
MC/DC100%2/2
$rule_value > 0 and not $is_optional
$rule_value > 0 not $is_optional
Truth table100%3/3
A and not(B)
A= $rule_value > 0B= not $is_optional
ABresult
0X0
100
111
1911 13 ▶ _rule_error($logger, $rules, "$rule_description: Parameter $param_label is undefined, but must have at least $rule_value member" . ($rule_value > 1 ? 's' : ''));
Branch: 1 branch
BranchTrueFalseTotal
$rule_value > 1 ? : 2 11 13
1912 0 $invalid_args{$key} = 1;
1913 }
1914 14 next;
1915 }
1916 156 ▶ if(scalar(@{$value}) < $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$value;} < $rule_value) 27 129 156
1917 27 ▶ _rule_error($logger, $rules, "$rule_description: Parameter $param_label must have at least $rule_value member" . (($rule_value > 1) ? 's' : ''));
Branch: 1 branch
BranchTrueFalseTotal
$rule_value > 1 ? : 9 18 27
1918 0 $invalid_args{$key} = 1;
1919 }
1920 } elsif($type eq 'hashref') {
1921 55 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 14 41 55
1922 14 ▶ if($rule_value > 0 && !$is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value > 0 and not $is_optional) 7 7 14
Condition100%3/3
$rule_value > 0 and not $is_optional
!ll&&!rl&&r
617
MC/DC100%2/2
$rule_value > 0 and not $is_optional
$rule_value > 0 not $is_optional
Truth table100%3/3
A and not(B)
A= $rule_value > 0B= not $is_optional
ABresult
0X0
100
111
1923 7 _rule_error($logger, $rules, "$rule_description: Parameter $param_label is undefined, but must contain at least $rule_value keys");
1924 0 $invalid_args{$key} = 1;
1925 }
1926 7 next;
1927 }
1928 41 ▶ if(scalar(keys(%{$value})) < $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$value;} < $rule_value) 15 26 41
1929 15 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must contain at least $rule_value keys");
1930 0 $invalid_args{$key} = 1;
1931 }
1932 ▶ } elsif(($type eq 'integer') || ($type eq 'number') || ($type eq 'float')) {
Condition100%6/6
$type eq "integer" or $type eq "number" or $type eq "float"
l!l&&r!l&&!r
5111301238
$type eq "integer" or $type eq "number"
l!l&&r!l&&!r
3571541368
MC/DC100%3/3
$type eq "integer" or $type eq "number" or $type eq "float"
$type eq "integer" $type eq "number" $type eq "float"
Truth table100%4/4
A or B or C
A= $type eq "integer"B= $type eq "number"C= $type eq "float"
ABCresult
1XX1
01X1
0011
0000
1933 641 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 53 588 641
1934 53 ▶ if($rule_value > 0 && !$is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value > 0 and not $is_optional) 25 28 53
Condition100%3/3
$rule_value > 0 and not $is_optional
!ll&&!rl&&r
24425
MC/DC100%2/2
$rule_value > 0 and not $is_optional
$rule_value > 0 not $is_optional
Truth table100%3/3
A and not(B)
A= $rule_value > 0B= not $is_optional
ABresult
0X0
100
111
1935 25 _rule_error($logger, $rules, "$rule_description: Parameter $param_label is undefined, but must be at least $rule_value");
1936 0 $invalid_args{$key} = 1;
1937 }
1938 28 next;
1939 }
1940 588 ▶ if(Scalar::Util::looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($value)) { } 588 0 588
1941 588 ▶ if($value < $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $rule_value) 125 463 588
1942 125 ▶ if($rules->{'error_msg'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rules->{'error_msg'}) { } 15 110 125
elsif ($type eq 'integer' and $value == 0) { } 15 95 110
elsif ($type eq 'integer' and $value == 1) { } 2 93 95
1943 15 _error($logger, $rules->{'error_msg'});
1944 ▶ } elsif(($type eq 'integer') && ($value == 0)) {
Condition100%3/3
$type eq "integer" and $value == 0
!ll&&!rl&&r
445115
MC/DC100%2/2
$type eq "integer" and $value == 0
$type eq "integer" $value == 0
Truth table100%3/3
A and B
A= $type eq "integer"B= $value == 0
ABresult
0X0
100
111
1945 15 _error($logger, "$rule_description: Parameter $param_label ($value) must be a positive number");
1946 ▶ } elsif(($type eq 'integer') && ($value == 1)) {
Condition100%3/3
$type eq "integer" and $value == 1
!ll&&!rl&&r
44492
MC/DC100%2/2
$type eq "integer" and $value == 1
$type eq "integer" $value == 1
Truth table100%3/3
A and B
A= $type eq "integer"B= $value == 1
ABresult
0X0
100
111
1947 2 _error($logger, "$rule_description: Parameter $param_label ($value) must be a positive, non-zero number");
1948 } else {
1949 93 _error($logger, "$rule_description: Parameter $param_label ($value) must be at least $rule_value");
1950 }
1951 0 $invalid_args{$key} = 1;
1952 0 next;
1953 }
1954 } else {
1955 0 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must be a number");
1956 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
1957 }
1958 } else {
1959 1238 _error($logger, "$rule_description: Parameter $param_label of type '$type' has meaningless min value $rule_value");
1960 }
1961 } elsif($rule_name eq 'max') {
1962 1200 ▶ if(!defined($rules->{'type'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rules->{"type"}) 0 1200 1200
1963 0 _error($logger, "$rule_description: Don't know type of $param_label to determine its maximum value $rule_value");
1964 }
1965 1200 my $type = lc($rules->{'type'});
1966 1200 ▶ if(exists($custom_types->{$type}->{'max'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $custom_types->{$type}{"max"}) 4 1196 1200
1967 4 $rule_value = $custom_types->{$type}->{'max'};
1968 4 $type = $custom_types->{$type}->{'type'};
1969 }
1970 1200 ▶ if(($type eq 'string') || ($type eq 'str') || ($type eq 'stringref')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'string' or $type eq 'str' or $type eq 'stringref') { } 385 815 1200
elsif ($type eq 'arrayref') { } 46 769 815
elsif ($type eq 'hashref') { } 28 741 769
elsif ($type eq 'integer' or $type eq 'number' or $type eq 'float') { } 265 476 741
Condition100%6/6
$type eq "string" or $type eq "str" or $type eq "stringref"
l!l&&r!l&&!r
37312815
$type eq "string" or $type eq "str"
l!l&&r!l&&!r
28588827
MC/DC100%3/3
$type eq "string" or $type eq "str" or $type eq "stringref"
$type eq "string" $type eq "str" $type eq "stringref"
Truth table100%4/4
A or B or C
A= $type eq "string"B= $type eq "str"C= $type eq "stringref"
ABCresult
1XX1
01X1
0011
0000
1971 385 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 13 372 385
1972 13 next; # Skip if string is undefined
1973 }
1974 372 ▶ if(defined(my $len = _number_of_characters($value))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $len = _number_of_characters($value))) { } 372 0 372
1975 372 ▶ if($len > $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $rule_value) 141 231 372
1976 141 _rule_error($logger, $rules, "$rule_description: Parameter $param_label too long, ($len characters), must be no longer than $rule_value");
1977 0 $invalid_args{$key} = 1;
1978 }
1979 } else {
1980 0 _rule_error($logger, $rules, "$rule_description: Parameter $param_label can't be decoded");
1981 0 $invalid_args{$key} = 1;
1982 }
1983 } elsif($type eq 'arrayref') {
1984 46 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 7 39 46
1985 7 next; # Skip if string is undefined
1986 }
1987 39 ▶ if(scalar(@{$value}) > $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$value;} > $rule_value) 19 20 39
1988 19 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must contain no more than $rule_value items");
1989 0 $invalid_args{$key} = 1;
1990 }
1991 } elsif($type eq 'hashref') {
1992 28 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 4 24 28
1993 4 next; # Skip if hash is undefined
1994 }
1995 24 ▶ if(scalar(keys(%{$value})) > $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$value;} > $rule_value) 8 16 24
1996 8 _rule_error($logger, $rules, "$rule_description: Parameter $param_label must contain no more than $rule_value keys");
1997 0 $invalid_args{$key} = 1;
1998 }
1999 ▶ } elsif(($type eq 'integer') || ($type eq 'number') || ($type eq 'float')) {
Condition100%6/6
$type eq "integer" or $type eq "number" or $type eq "float"
l!l&&r!l&&!r
21451476
$type eq "integer" or $type eq "number"
l!l&&r!l&&!r
15361527
MC/DC100%3/3
$type eq "integer" or $type eq "number" or $type eq "float"
$type eq "integer" $type eq "number" $type eq "float"
Truth table100%4/4
A or B or C
A= $type eq "integer"B= $type eq "number"C= $type eq "float"
ABCresult
1XX1
01X1
0011
0000
2000 265 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 16 249 265
2001 16 next; # Skip if hash is undefined
2002 }
2003 249 ▶ if(Scalar::Util::looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($value)) { } 249 0 249
2004 249 ▶ if($value > $rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $rule_value) 122 127 249
2005 122 ▶ if($rules->{'error_msg'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rules->{'error_msg'}) { } 0 122 122
elsif ($type eq 'integer' and $value == 0) { } 0 122 122
elsif ($type eq 'integer' and $value == -1) { } 0 122 122
2006 0 _error($logger, $rules->{'error_msg'});
2007 ▶ } elsif(($type eq 'integer') && ($value == 0)) {
Condition66%2/3
$type eq "integer" and $value == 0
!ll&&!rl&&r
66560
MC/DC0%0/2
$type eq "integer" and $value == 0
$type eq "integer" $value == 0
Truth table66%2/3
A and B
A= $type eq "integer"B= $value == 0
ABresult
0X0
100
111
2008 0 _error($logger, "$rule_description: Parameter $param_label ($value) must be a negative number");
2009 ▶ } elsif(($type eq 'integer') && ($value == -1)) {
Condition66%2/3
$type eq "integer" and $value == -1
!ll&&!rl&&r
66560
MC/DC0%0/2
$type eq "integer" and $value == -1
$type eq "integer" $value == -1
Truth table66%2/3
A and B
A= $type eq "integer"B= $value == -1
ABresult
0X0
100
111
2010 0 _error($logger, "$rule_description: Parameter $param_label ($value) must be a negative, non-zero number");
2011 } else {
2012 122 _error($logger, "$rule_description: Parameter $param_label ($value) must be no more than $rule_value");
2013 }
2014 0 $invalid_args{$key} = 1;
2015 0 next;
2016 }
2017 } else {
2018 0 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must be a number");
2019 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
2020 }
2021 } else {
2022 476 _error($logger, "$rule_description: Parameter $param_label of type '$type' has meaningless max value $rule_value");
2023 }
2024 ▶ } elsif(($rule_name eq 'matches') || ($rule_name eq 'regex')) {
Condition100%3/3
$rule_name eq "matches" or $rule_name eq "regex"
l!l&&r!l&&!r
100038182
MC/DC100%2/2
$rule_name eq "matches" or $rule_name eq "regex"
$rule_name eq "matches" $rule_name eq "regex"
Truth table100%3/3
A or B
A= $rule_name eq "matches"B= $rule_name eq "regex"
ABresult
1X1
011
000
2025 1003 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 106 897 1003
2026 106 next; # Skip if string is undefined
2027 }
2028 897 eval {
2029 897 ▶ my $re = (ref($rule_value) eq 'Regexp') ? $rule_value : qr/\Q$rule_value\E/;
Branch: 1 branch
BranchTrueFalseTotal
ref $rule_value eq 'Regexp' ? : 448 449 897
2030 897 ▶ if(($rules->{'type'} eq 'arrayref') || ($rules->{'type'} eq 'ArrayRef')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rules->{'type'} eq 'arrayref' or $rules->{'type'} eq 'ArrayRef') { } 37 860 897
elsif (not $value =~ /$re/) { } 673 187 860
Condition100%3/3
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
l!l&&r!l&&!r
2116860
MC/DC100%2/2
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
$rules->{"type"} eq "arrayref" $rules->{"type"} eq "ArrayRef"
Truth table100%3/3
A or B
A= $rules->{"type"} eq "arrayref"B= $rules->{"type"} eq "ArrayRef"
ABresult
1X1
011
000
2031 # all{} short-circuits on first failure and allocates no temp array
2032 37 ▶ unless(all { $_ =~ $re } @{$value}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_ =~ /$re/; } , @{$value;})) 14 23 37
2033 14 _rule_error($logger, $rules, "$rule_description: All members of parameter $param_label [", join(', ', @{$value}), "] must match pattern '$rule_value'");
2034 }
2035 } elsif($value !~ $re) {
2036 673 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must match pattern '$re'");
2037 }
2038 210 1;
2039 };
2040 897 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 687 210 897
2041 687 _rule_error($logger, $rules, "$rule_description: Parameter $param_label regex '$rule_value' error: $@");
2042 0 $invalid_args{$key} = 1;
2043 }
2044 } elsif($rule_name eq 'nomatch') {
2045 352 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 36 316 352
2046 36 next; # Skip if string is undefined
2047 }
2048 # Compile string patterns with \Q...\E so metacharacters are
2049 # treated as literals, matching the behaviour of 'matches'.
2050 316 ▶ my $re = (ref($rule_value) eq 'Regexp') ? $rule_value : qr/\Q$rule_value\E/;
Branch: 1 branch
BranchTrueFalseTotal
ref $rule_value eq 'Regexp' ? : 26 290 316
2051 316 eval {
2052 316 ▶ if(($rules->{'type'} eq 'arrayref') || ($rules->{'type'} eq 'ArrayRef')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rules->{'type'} eq 'arrayref' or $rules->{'type'} eq 'ArrayRef') { } 13 303 316
elsif ($value =~ /$re/) { } 10 293 303
Condition100%3/3
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
l!l&&r!l&&!r
94303
MC/DC100%2/2
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
$rules->{"type"} eq "arrayref" $rules->{"type"} eq "ArrayRef"
Truth table100%3/3
A or B
A= $rules->{"type"} eq "arrayref"B= $rules->{"type"} eq "ArrayRef"
ABresult
1X1
011
000
2053 # any{} short-circuits on first match and allocates no temp array
2054 13 ▶ if(any { $_ =~ $re } @{$value}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ =~ /$re/; } , @{$value;})) 2 11 13
2055 2 _rule_error($logger, $rules, "$rule_description: No member of parameter $param_label [", join(', ', @{$value}), "] must match pattern '$rule_value'");
2056 }
2057 } elsif($value =~ $re) {
2058 10 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must not match pattern '$rule_value'");
2059 0 $invalid_args{$key} = 1;
2060 }
2061 304 1;
2062 };
2063 316 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 12 304 316
2064 12 _rule_error($logger, $rules, "$rule_description: Parameter $param_label regex '$rule_value' error: $@");
2065 0 $invalid_args{$key} = 1;
2066 }
2067 } elsif($rule_name eq 'bnf') {
2068 26 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 3 23 26
2069 3 next; # Skip if value is undefined
2070 }
2071 23 ▶ if(ref($rule_value) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value ne "ARRAY") 3 20 23
2072 3 _error($logger, "$rule_description: Parameter $param_label 'bnf' rule must be an arrayref of grammar lines");
2073 }
2074 20 require Params::Validate::Strict::BNF;
2075 20 my $matcher = Params::Validate::Strict::BNF::bnf_to_matcher($rule_value);
2076 16 ▶ unless($matcher->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$matcher($value)) 8 8 16
2077 8 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) does not match the BNF grammar");
2078 0 $invalid_args{$key} = 1;
2079 }
2080 ▶ } elsif(($rule_name eq 'memberof') || ($rule_name eq 'enum') || ($rule_name eq 'values')) {
Condition100%6/6
$rule_name eq "memberof" or $rule_name eq "enum" or $rule_name eq "values"
l!l&&r!l&&!r
10921686544
$rule_name eq "memberof" or $rule_name eq "enum"
l!l&&r!l&&!r
9191736712
MC/DC100%3/3
$rule_name eq "memberof" or $rule_name eq "enum" or $rule_name eq "values"
$rule_name eq "memberof" $rule_name eq "enum" $rule_name eq "values"
Truth table100%4/4
A or B or C
A= $rule_name eq "memberof"B= $rule_name eq "enum"C= $rule_name eq "values"
ABCresult
1XX1
01X1
0011
0000
2081 1260 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 126 1134 1260
2082 126 next; # Skip if string is undefined
2083 }
2084 1134 ▶ if(ref($rule_value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value eq 'ARRAY') { } 842 292 1134
2085 842 ▶ unless(_value_in_list($value, $rule_value, $rules->{'type'} // '', $rules->{'case_sensitive'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_value_in_list($value, $rule_value, $rules->{"type"} // "", $rules->{"case_sensitive"})) 632 210 842
Condition50%1/2
$rules->{"type"} // ""
l!l
8420
MC/DC0%0/1
$rules->{"type"} // ""
$rules->{"type"}
Truth table50%1/2
A
A= $rules->{"type"}
Aresult
11
00
2086 632 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must be one of ", join(', ', @{$rule_value}));
2087 0 $invalid_args{$key} = 1;
2088 }
2089 } else {
2090 292 _rule_error($logger, $rules, "$rule_description: Parameter $param_label rule ($rule_value) must be an array reference");
2091 }
2092 } elsif($rule_name eq 'notmemberof') {
2093 376 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 36 340 376
2094 36 next; # Skip if string is undefined
2095 }
2096 340 ▶ if(ref($rule_value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value eq 'ARRAY') { } 339 1 340
2097 339 ▶ if(_value_in_list($value, $rule_value, $rules->{'type'} // '', $rules->{'case_sensitive'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_value_in_list($value, $rule_value, $rules->{"type"} // "", $rules->{"case_sensitive"})) 41 298 339
Condition50%1/2
$rules->{"type"} // ""
l!l
3390
MC/DC0%0/1
$rules->{"type"} // ""
$rules->{"type"}
Truth table50%1/2
A
A= $rules->{"type"}
Aresult
11
00
2098 41 _rule_error($logger, $rules, "$rule_description: Parameter $param_label ($value) must not be one of ", join(', ', @{$rule_value}));
2099 0 $invalid_args{$key} = 1;
2100 }
2101 } else {
2102 1 _rule_error($logger, $rules, "$rule_description: Parameter $param_label rule ($rule_value) must be an array reference");
2103 }
2104 } elsif($rule_name eq 'isa') {
2105 22 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 22 22
2106 0 next; # Skip if object not given
2107 }
2108 22 ▶ if($rules->{'type'} eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{'type'} eq 'object') { } 19 3 22
2109 19 ▶ if(!$value->isa($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa($rule_value)) 5 14 19
2110 5 ▶ _error($logger, "$rule_description: Parameter $param_label must be a '$rule_value' object got a " . (ref($value) ? ref($value) : $value) . ' object instead');
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 5 0 5
2111 0 $invalid_args{$key} = 1;
2112 }
2113 } else {
2114 3 _error($logger, "$rule_description: Parameter $param_label has meaningless isa value $rule_value");
2115 }
2116 } elsif($rule_name eq 'can') {
2117 44 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 44 44
2118 0 next; # Skip if object not given
2119 }
2120 44 ▶ if(($rules->{'type'} eq 'object') || ($rules->{'type'} eq 'invocant')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{'type'} eq 'object' or $rules->{'type'} eq 'invocant') { } 40 4 44
Condition100%3/3
$rules->{"type"} eq "object" or $rules->{"type"} eq "invocant"
l!l&&r!l&&!r
3554
MC/DC100%2/2
$rules->{"type"} eq "object" or $rules->{"type"} eq "invocant"
$rules->{"type"} eq "object" $rules->{"type"} eq "invocant"
Truth table100%3/3
A or B
A= $rules->{"type"} eq "object"B= $rules->{"type"} eq "invocant"
ABresult
1X1
011
000
2121 40 ▶ if(ref($rule_value) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rule_value eq 'ARRAY') { } 16 24 40
elsif (not ref $rule_value) { } 23 1 24
2122 # List of methods
2123 16 foreach my $method(@{$rule_value}) {
2124 31 ▶ if(!$value->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->can($method)) 7 24 31
2125 7 _error($logger, "$rule_description: Parameter $param_label must be an object that understands the $method method");
2126 0 $invalid_args{$key} = 1;
2127 }
2128 }
2129 } elsif(!ref($rule_value)) {
2130 23 ▶ if(!$value->can($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->can($rule_value)) 11 12 23
2131 11 _error($logger, "$rule_description: Parameter $param_label must be an object that understands the $rule_value method");
2132 0 $invalid_args{$key} = 1;
2133 }
2134 } else {
2135 1 _error($logger, "$rule_description: 'can' rule for Parameter $param_label must be either a scalar or an arrayref");
2136 }
2137 } else {
2138 4 _error($logger, "$rule_description: Parameter $param_label has meaningless can value '$rule_value' for parameter type $rules->{type}");
2139 }
2140 } elsif($rule_name eq 'classcan') {
2141 6 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 6 6
2142 0 next;
2143 }
2144 6 ▶ if(ref($rule_value) && ref($rule_value) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value and ref $rule_value ne "ARRAY") 1 5 6
Condition100%3/3
ref $rule_value and ref $rule_value ne "ARRAY"
!ll&&!rl&&r
321
MC/DC100%2/2
ref $rule_value and ref $rule_value ne "ARRAY"
ref $rule_value ref $rule_value ne "ARRAY"
Truth table100%3/3
A and B
A= ref $rule_valueB= ref $rule_value ne "ARRAY"
ABresult
0X0
100
111
2145 1 _error($logger, "$rule_description: 'classcan' rule for Parameter $param_label must be either a scalar or an arrayref");
2146 }
2147 5 ▶ unless(!ref($value) && $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) 1 4 5
Condition66%2/3
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
014
MC/DC50%1/2
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
not ref $value $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
Truth table66%2/3
not(A) and B
A= not ref $valueB= $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
ABresult
0X0
100
111
2148 1 $invalid_args{$key} = 1;
2149 1 _error($logger, "$rule_description: Parameter $param_label must be a valid class name");
2150 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
2151 }
2152 4 ▶ foreach my $method (ref($rule_value) ? @{$rule_value} : ($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $rule_value ? : 2 2 4
2153 # Always a method call, so an overridden can() is respected
2154 6 ▶ unless($value->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->can($method)) 2 4 6
2155 2 _error($logger, "$rule_description: Parameter $param_label ($value) must be a class that understands the $method method");
2156 0 $invalid_args{$key} = 1;
2157 }
2158 }
2159 } elsif($rule_name eq 'does') {
2160 3 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 3 3
2161 0 next; # Skip if object not given
2162 }
2163 3 ▶ if($rules->{'type'} eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{'type'} eq 'object') { } 2 1 3
2164 2 ▶ unless(Scalar::Util::blessed($value) && $value->DOES($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value) and $value->DOES($rule_value)) 1 1 2
Condition66%2/3
builtin::blessed($value) and $value->DOES($rule_value)
!ll&&!rl&&r
011
MC/DC50%1/2
builtin::blessed($value) and $value->DOES($rule_value)
builtin::blessed($value) $value->DOES($rule_value)
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->DOES($rule_value)
ABresult
0X0
100
111
2165 1 _error($logger, "$rule_description: Parameter $param_label must be an object that does '$rule_value'");
2166 0 $invalid_args{$key} = 1;
2167 }
2168 } else {
2169 1 _error($logger, "$rule_description: Parameter $param_label has meaningless does value '$rule_value' for parameter type $rules->{type}");
2170 }
2171 } elsif($rule_name eq 'classisa') {
2172 3 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 2 3
2173 1 next;
2174 }
2175 2 ▶ unless(!ref($value) && $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ && $value->isa($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value->isa($rule_value)) 1 1 2
Condition50%3/6
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value->isa($rule_value)
!ll&&!rl&&r
011
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
002
MC/DC33%1/3
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value->isa($rule_value)
not ref $value $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ $value->isa($rule_value)
Truth table50%2/4
not(A) and B and C
A= not ref $valueB= $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/C= $value->isa($rule_value)
ABCresult
0XX0
10X0
1100
1111
2176 1 _error($logger, "$rule_description: Parameter $param_label ($value) must be a class that isa '$rule_value'");
2177 0 $invalid_args{$key} = 1;
2178 }
2179 } elsif($rule_name eq 'subclass') {
2180 2 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
2181 0 next;
2182 }
2183 2 ▶ unless(!ref($value) && $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value ne $rule_value and $value->isa($rule_value)) 1 1 2
Condition55%5/9
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value ne $rule_value and $value->isa($rule_value)
!ll&&!rl&&r
101
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value ne $rule_value
!ll&&!rl&&r
011
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
002
MC/DC25%1/4
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value ne $rule_value and $value->isa($rule_value)
not ref $value $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ $value ne $rule_value $value->isa($rule_value)
Truth table40%2/5
not(A) and B and C and D
A= not ref $valueB= $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/C= $value ne $rule_valueD= $value->isa($rule_value)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2184 && $value ne $rule_value && $value->isa($rule_value)) {
2185 1 _error($logger, "$rule_description: Parameter $param_label ($value) must be a strict subclass of '$rule_value'");
2186 0 $invalid_args{$key} = 1;
2187 }
2188 } elsif($rule_name eq 'classdoes') {
2189 2 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
2190 0 next;
2191 }
2192 2 ▶ unless(!ref($value) && $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ && $value->DOES($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value->DOES($rule_value)) 1 1 2
Condition50%3/6
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value->DOES($rule_value)
!ll&&!rl&&r
011
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
002
MC/DC33%1/3
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ and $value->DOES($rule_value)
not ref $value $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ $value->DOES($rule_value)
Truth table50%2/4
not(A) and B and C
A= not ref $valueB= $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/C= $value->DOES($rule_value)
ABCresult
0XX0
10X0
1100
1111
2193 1 _error($logger, "$rule_description: Parameter $param_label ($value) must be a class that does '$rule_value'");
2194 0 $invalid_args{$key} = 1;
2195 }
2196 } elsif($rule_name eq 'driver') {
2197 3 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 3 3
2198 0 next;
2199 }
2200 3 ▶ unless(!ref($value) && $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) 1 2 3
Condition66%2/3
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
012
MC/DC50%1/2
not ref $value and $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
not ref $value $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
Truth table66%2/3
not(A) and B
A= not ref $valueB= $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
ABresult
0X0
100
111
2201 1 $invalid_args{$key} = 1;
2202 1 _error($logger, "$rule_description: Parameter $param_label must be a valid class name");
2203 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
2204 }
2205 2 (my $file = $value) =~ s{::}{/}g;
2206 2 $file .= '.pm';
2207 2 eval { require $file };
2208 2 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
2209 0 $invalid_args{$key} = 1;
2210 0 _error($logger, "$rule_description: Parameter $param_label ($value) could not be loaded: $@");
2211 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
2212 }
2213 2 ▶ unless($value->isa($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa($rule_value)) 1 1 2
2214 1 _error($logger, "$rule_description: Parameter $param_label ($value) must be a class that isa '$rule_value'");
2215 0 $invalid_args{$key} = 1;
2216 }
2217 } elsif($rule_name eq 'element_type') {
2218 43 ▶ if(($rules->{'type'} eq 'arrayref') || ($rules->{'type'} eq 'ArrayRef')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{'type'} eq 'arrayref' or $rules->{'type'} eq 'ArrayRef') { } 40 3 43
Condition66%2/3
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
l!l&&r!l&&!r
4003
MC/DC50%1/2
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
$rules->{"type"} eq "arrayref" $rules->{"type"} eq "ArrayRef"
Truth table66%2/3
A or B
A= $rules->{"type"} eq "arrayref"B= $rules->{"type"} eq "ArrayRef"
ABresult
1X1
011
000
2219 40 my $type = $rule_value;
2220 40 my $custom_type = $custom_types->{$rule_value};
2221 40 ▶ if($custom_type && $custom_type->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_type and $custom_type->{"type"}) 4 36 40
Condition100%3/3
$custom_type and $custom_type->{"type"}
!ll&&!rl&&r
3424
MC/DC100%2/2
$custom_type and $custom_type->{"type"}
$custom_type $custom_type->{"type"}
Truth table100%3/3
A and B
A= $custom_typeB= $custom_type->{"type"}
ABresult
0X0
100
111
2222 4 $type = $custom_type->{'type'};
2223 }
2224 40 foreach my $member(@{$value}) {
2225 91 ▶ if($custom_type && $custom_type->{'transform'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_type and $custom_type->{"transform"}) 5 86 91
Condition100%3/3
$custom_type and $custom_type->{"transform"}
!ll&&!rl&&r
7795
MC/DC100%2/2
$custom_type and $custom_type->{"transform"}
$custom_type $custom_type->{"transform"}
Truth table100%3/3
A and B
A= $custom_typeB= $custom_type->{"transform"}
ABresult
0X0
100
111
2226 # The custom type has a transform embedded within it
2227 5 ▶ if(ref($custom_type->{'transform'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $custom_type->{'transform'} eq 'CODE') { } 4 1 5
2228 4 $member = &{$custom_type->{'transform'}}($member);
2229 } else {
2230 1 _error($logger, "$rule_description: transforms must be a code ref");
2231 }
2232 }
2233 90 ▶ if(($type eq 'string') || ($type eq 'Str')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'string' or $type eq 'Str') { } 32 58 90
elsif ($type eq 'integer') { } 46 12 58
elsif ($type eq 'number' or $rule_value eq 'float') { } 11 1 12
elsif ($type eq 'object') { } 0 1 1
Condition66%2/3
$type eq "string" or $type eq "Str"
l!l&&r!l&&!r
32058
MC/DC50%1/2
$type eq "string" or $type eq "Str"
$type eq "string" $type eq "Str"
Truth table66%2/3
A or B
A= $type eq "string"B= $type eq "Str"
ABresult
1X1
011
000
2234 32 ▶ if(ref($member)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $member) 2 30 32
2235 2 _rule_error($logger, $rules, "$param_label can only contain strings");
2236 0 $invalid_args{$key} = 1;
2237 }
2238 } elsif($type eq 'integer') {
2239 46 ▶ if(ref($member) || ($member =~ /\D/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $member or $member =~ /\D/) 7 39 46
Condition66%2/3
ref $member or $member =~ /\D/
l!l&&r!l&&!r
0739
MC/DC50%1/2
ref $member or $member =~ /\D/
ref $member $member =~ /\D/
Truth table66%2/3
A or B
A= ref $memberB= $member =~ /\D/
ABresult
1X1
011
000
2240 7 _rule_error($logger, $rules, "$param_label can only contain integers (found $member)");
2241 0 $invalid_args{$key} = 1;
2242 }
2243 ▶ } elsif(($type eq 'number') || ($rule_value eq 'float')) {
Condition100%3/3
$type eq "number" or $rule_value eq "float"
l!l&&r!l&&!r
921
MC/DC100%2/2
$type eq "number" or $rule_value eq "float"
$type eq "number" $rule_value eq "float"
Truth table100%3/3
A or B
A= $type eq "number"B= $rule_value eq "float"
ABresult
1X1
011
000
2244 11 ▶ if(ref($member) || ($member !~ /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $member or not $member =~ /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)$/) 2 9 11
Condition66%2/3
ref $member or not $member =~ /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)$/
l!l&&r!l&&!r
029
MC/DC50%1/2
ref $member or not $member =~ /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)$/
ref $member not $member =~ /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)$/
Truth table66%2/3
A or not(B)
A= ref $memberB= not $member =~ /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)$/
ABresult
1X1
011
000
2245 2 _rule_error($logger, $rules, "$param_label can only contain numbers (found $member)");
2246 0 $invalid_args{$key} = 1;
2247 }
2248 } elsif($type eq 'object') {
2249 0 ▶ if(!Scalar::Util::blessed($member)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($member)) 0 0 0
2250 0 _rule_error($logger, $rules, "$param_label can only contain objects (found $member)");
2251 0 $invalid_args{$key} = 1;
2252 }
2253 } else {
2254 1 _error($logger, "BUG: Add $type to element_type list");
2255 }
2256 }
2257 } else {
2258 3 _error($logger, "$rule_description: Parameter $param_label has meaningless element_type value $rule_value");
2259 }
2260 } elsif($rule_name eq 'element_isa') {
2261 5 ▶ if(($rules->{'type'} eq 'arrayref') || ($rules->{'type'} eq 'ArrayRef')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{'type'} eq 'arrayref' or $rules->{'type'} eq 'ArrayRef') { } 4 1 5
Condition66%2/3
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
l!l&&r!l&&!r
401
MC/DC50%1/2
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
$rules->{"type"} eq "arrayref" $rules->{"type"} eq "ArrayRef"
Truth table66%2/3
A or B
A= $rules->{"type"} eq "arrayref"B= $rules->{"type"} eq "ArrayRef"
ABresult
1X1
011
000
2262 4 foreach my $member (@{$value}) {
2263 5 ▶ unless(Scalar::Util::blessed($member) && $member->isa($rule_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($member) and $member->isa($rule_value)) 2 3 5
Condition100%3/3
builtin::blessed($member) and $member->isa($rule_value)
!ll&&!rl&&r
113
MC/DC100%2/2
builtin::blessed($member) and $member->isa($rule_value)
builtin::blessed($member) $member->isa($rule_value)
Truth table100%3/3
A and B
A= builtin::blessed($member)B= $member->isa($rule_value)
ABresult
0X0
100
111
2264 2 $invalid_args{$key} = 1;
2265 2 _rule_error($logger, $rules, "$rule_description: Parameter $param_label can only contain '$rule_value' objects");
2266 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
2267 }
2268 }
2269 } else {
2270 1 _error($logger, "$rule_description: Parameter $param_label has meaningless element_isa value $rule_value");
2271 }
2272 } elsif($rule_name eq 'needs') {
2273 # Desugared into dependency relationships before validation finishes
2274 } elsif($rule_name eq 'optional') {
2275 # Already handled at the beginning of the loop
2276 } elsif($rule_name eq 'nullable') {
2277 # Already handled at the beginning of the loop (same as optional)
2278 } elsif($rule_name eq 'default') {
2279 # Handled earlier
2280 } elsif($rule_name eq 'error_msg') {
2281 # Handled inline
2282 } elsif($rule_name eq 'transform') {
2283 # Handled before the loop
2284 } elsif($rule_name eq 'case_sensitive') {
2285 # Handled inline
2286 } elsif($rule_name eq 'description') {
2287 # A la, Data::Processor
2288 } elsif($rule_name =~ /^_/) {
2289 # Ignore internal/metadata fields from schema extraction
2290 } elsif($rule_name eq 'semantic') {
2291 36 ▶ if($rule_value eq 'unix_timestamp') {
Branch: 4 branches
BranchTrueFalseTotal
if ($rule_value eq 'unix_timestamp') { } 9 27 36
elsif ($rule_value eq 'identifier') { } 3 24 27
elsif ($rule_value eq 'class_name') { } 3 21 24
elsif ($rule_value eq 'filename') { } 19 2 21
2292 9 ▶ if($value < 0 || $value > 2147483647) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0 or $value > 2147483647) 4 5 9
Condition100%3/3
$value < 0 or $value > 2147483647
l!l&&r!l&&!r
225
MC/DC100%2/2
$value < 0 or $value > 2147483647
$value < 0 $value > 2147483647
Truth table100%3/3
A or B
A= $value < 0B= $value > 2147483647
ABresult
1X1
011
000
2293 4 _error($logger, "Invalid Unix timestamp: $value");
2294 }
2295 } elsif($rule_value eq 'identifier') {
2296 3 ▶ if(defined($value) && $value !~ /\A[A-Za-z_]\w*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not $value =~ /\A[A-Za-z_]\w*\z/) 2 1 3
Condition66%2/3
defined $value and not $value =~ /\A[A-Za-z_]\w*\z/
!ll&&!rl&&r
012
MC/DC50%1/2
defined $value and not $value =~ /\A[A-Za-z_]\w*\z/
defined $value not $value =~ /\A[A-Za-z_]\w*\z/
Truth table66%2/3
A and not(B)
A= defined $valueB= not $value =~ /\A[A-Za-z_]\w*\z/
ABresult
0X0
100
111
2297 2 _error($logger, "Invalid Perl identifier: $value");
2298 }
2299 } elsif($rule_value eq 'class_name') {
2300 3 ▶ if(defined($value) && $value !~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/) 1 2 3
Condition66%2/3
defined $value and not $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
!ll&&!rl&&r
021
MC/DC50%1/2
defined $value and not $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
defined $value not $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
Truth table66%2/3
A and not(B)
A= defined $valueB= not $value =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/
ABresult
0X0
100
111
2301 1 _error($logger, "Invalid Perl class name: $value");
2302 }
2303 } elsif($rule_value eq 'filename') {
2304 19 ▶ if(defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 18 1 19
2305 # A single path component, at most 255 bytes when encoded as UTF-8
2306 18 my $bytes = $value;
2307 18 utf8::encode($bytes);
2308 18 ▶ if(($value eq '') || ($value eq '.') || ($value eq '..')
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "" or $value eq "." or $value eq ".." or $value =~ m[[/\0]] or length $bytes > 255) 10 8 18
Condition100%12/12
$value eq "" or $value eq "." or $value eq ".." or $value =~ m[[/\0]] or length $bytes > 255
l!l&&r!l&&!r
828
$value eq "" or $value eq "." or $value eq ".." or $value =~ m[[/\0]]
l!l&&r!l&&!r
3510
$value eq "" or $value eq "." or $value eq ".."
l!l&&r!l&&!r
2115
$value eq "" or $value eq "."
l!l&&r!l&&!r
1116
MC/DC100%5/5
$value eq "" or $value eq "." or $value eq ".." or $value =~ m[[/\0]] or length $bytes > 255
$value eq "" $value eq "." $value eq ".." $value =~ m[[/\0]] length $bytes > 255
Truth table100%6/6
A or B or C or D or E
A= $value eq ""B= $value eq "."C= $value eq ".."D= $value =~ m[[/\0]]E= length $bytes > 255
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
2309 || ($value =~ m{[/\0]}) || (length($bytes) > 255)) {
2310 10 _error($logger, "Invalid filename: $value");
2311 }
2312 }
2313 } else {
2314 2 _warn($logger, "semantic type $rule_value is not yet supported");
2315 }
2316 } elsif($rule_name eq 'schema') {
2317 # Nested schema Run the given schema against each element of the array
2318 88 ▶ if(($rules->{'type'} eq 'arrayref') || ($rules->{'type'} eq 'ArrayRef')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rules->{'type'} eq 'arrayref' or $rules->{'type'} eq 'ArrayRef') { } 16 72 88
elsif ($rules->{'type'} eq 'hashref') { } 71 1 72
Condition66%2/3
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
l!l&&r!l&&!r
16072
MC/DC50%1/2
$rules->{"type"} eq "arrayref" or $rules->{"type"} eq "ArrayRef"
$rules->{"type"} eq "arrayref" $rules->{"type"} eq "ArrayRef"
Truth table66%2/3
A or B
A= $rules->{"type"} eq "arrayref"B= $rules->{"type"} eq "ArrayRef"
ABresult
1X1
011
000
2319 16 ▶ if(ref($value) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 16 0 16
elsif (defined $value) { } 0 0 0
2320 16 foreach my $member(@{$value}) {
2321 # Distinguish two schema forms:
2322 # (a) Rule hash - has a top-level 'type' key, e.g. { type=>'string', matches=>qr/.../ }
2323 # => validate each element against that rule directly.
2324 # (b) Field-schema hash - keys are field names whose values are rule hashes,
2325 # e.g. { name=>{type=>'string'}, age=>{type=>'integer'} }
2326 # => validate each hashref element against the field schema directly.
2327 26 ▶ my $is_field_schema = (ref($rule_value) eq 'HASH') && !exists($rule_value->{'type'});
Condition66%2/3
ref $rule_value eq 'HASH' && !exists $rule_value->{'type'}
!ll&&!rl&&r
0197
MC/DC50%1/2
ref $rule_value eq 'HASH' && !exists $rule_value->{'type'}
ref $rule_value eq 'HASH' !exists $rule_value->{'type'}
Truth table66%2/3
A && not(B)
A= ref $rule_value eq 'HASH'B= !exists $rule_value->{'type'}
ABresult
0X0
100
111
2328 26 my %inner = (custom_types => $custom_types);
2329 26 ▶ if($is_field_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_field_schema) { } 7 19 26
2330 7 $inner{input} = $member;
2331 7 $inner{schema} = $rule_value;
2332 } else {
2333 19 $inner{input} = { $key => $member };
2334 19 $inner{schema} = { $key => $rule_value };
2335 }
2336 26 ▶ if(!validate_strict(\%inner)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (validate_strict(\%inner)) 0 18 18
2337 0 $invalid_args{$key} = 1;
2338 }
2339 }
2340 } elsif(defined($value)) { # Allow undef for optional values
2341 0 _error($logger, "$rule_description: nested schema: Parameter '$value' must be an arrayref");
2342 }
2343 } elsif($rules->{'type'} eq 'hashref') {
2344 71 ▶ if(ref($rule_value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value eq 'HASH') { } 71 0 71
2345 # Apply nested defaults before validation
2346 71 my $nested_with_defaults = _apply_nested_defaults($value, $rule_value);
2347 71 ▶ if(scalar keys(%{$nested_with_defaults})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$nested_with_defaults;}) 69 2 71
2348 69 ▶ if(my $new_args = validate_strict({ input => $nested_with_defaults, schema => $rule_value, custom_types => $custom_types })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_args = validate_strict({'input', $nested_with_defaults, 'schema', $rule_value, 'custom_types', $custom_types})) { } 45 0 45
2349 45 $value = $new_args;
2350 } else {
2351 0 $invalid_args{$key} = 1;
2352 }
2353 }
2354 } else {
2355 0 _error($logger, "$rule_description: nested schema: Parameter '$value' must be an hashref");
2356 }
2357 } else {
2358 1 _error($logger, "$rule_description: Parameter $param_label: 'schema' only supports arrayref and hashref, not $rules->{type}");
2359 }
2360 ▶ } elsif(($rule_name eq 'validate') || ($rule_name eq 'validator')) {
Condition100%3/3
$rule_name eq "validate" or $rule_name eq "validator"
l!l&&r!l&&!r
1051170
MC/DC100%2/2
$rule_name eq "validate" or $rule_name eq "validator"
$rule_name eq "validate" $rule_name eq "validator"
Truth table100%3/3
A or B
A= $rule_name eq "validate"B= $rule_name eq "validator"
ABresult
1X1
011
000
2361 15 ▶ if(ref($rule_value) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule_value eq 'CODE') { } 13 2 15
2362 13 ▶ if(my $error = &{$rule_value}($args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &{$rule_value;}($args)) 5 8 13
2363 5 _error($logger, "$rule_description: $param_label not valid: $error");
2364 0 $invalid_args{$key} = 1;
2365 }
2366 } else {
2367 # _error($logger, "$rule_description: Parameter $param_label: 'validate' only supports coderef, not $value");
2368 2 ▶ _error($logger, "$rule_description: Parameter $param_label: 'validate' only supports coderef, not " . ref($rule_value) // $rule_value);
Condition33%1/3
"${rule_description}: Parameter ${param_label}: 'validate' only supports coderef, not " . ref($rule_value) // $rule_value
l!l&&r!l&&!r
200
MC/DC0%0/2
"${rule_description}: Parameter ${param_label}: 'validate' only supports coderef, not " . ref($rule_value) // $rule_value
"${rule_description}: Parameter ${param_label}: 'validate' only supports coderef, not " . ref($rule_value) $rule_value
Truth table33%1/3
A // B
A= "${rule_description}: Parameter ${param_label}: 'validate' only supports coderef, not " . ref($rule_value)B= $rule_value
ABresult
1X1
011
000
2369 }
2370 } elsif ($rule_name eq 'callback') {
2371 # Custom validation code
2372 775 ▶ unless (defined &$rule_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$rule_value) 1 774 775
2373 1 _error($logger, "$rule_description: callback for $param_label must be a code reference");
2374 }
2375 774 my $res = $rule_value->($value, $args, $schema, $key);
2376 772 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 356 416 772
2377 356 _rule_error($logger, $rules, "$rule_description: Parameter $param_label failed custom validation");
2378 0 $invalid_args{$key} = 1;
2379 }
2380 } elsif($rule_name eq 'position') {
2381 39 ▶ if($rule_value < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value < 0) 0 39 39
2382 # A negative number always contains a non-digit ('-'), so the
2383 # /\D/ check below (and the positional detection) catch it first.
2384 0 _unreachable($logger);
2385 }
2386 39 ▶ if($rule_value =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value =~ /\D/) 1 38 39
2387 1 _error($logger, "$rule_description: Parameter $param_label: 'position' must be a positive integer");
2388 }
2389 } elsif($rule_name eq 'slurp') {
2390 3 ▶ if($rule_value && $are_positional_args != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_value and $are_positional_args != 1) 0 3 3
Condition33%1/3
$rule_value and $are_positional_args != 1
!ll&&!rl&&r
030
MC/DC0%0/2
$rule_value and $are_positional_args != 1
$rule_value $are_positional_args != 1
Truth table33%1/3
A and B
A= $rule_valueB= $are_positional_args != 1
ABresult
0X0
100
111
2391 0 _error($logger, "$rule_description: Parameter $param_label: 'slurp' is only valid in positional-argument schemas (all parameters need a 'position')");
2392 }
2393 # Pre-processed: value was already collected as arrayref of remaining positional args
2394 } elsif($rule_name eq 'aliases') {
2395 # Pre-processed: alternative input-key names resolved during value fetch
2396 } else {
2397 328 _error($logger, "$rule_description: Unknown rule '$rule_name'");
2398 }
2399 }
2400 } elsif(ref($rules) eq 'ARRAY') {
2401 210 ▶ if(scalar(@{$rules})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$rules;}) { } 208 2 210
2402 # An argument can be one of several different types.
2403 # This path handles both explicit array-of-rules schemas and the
2404 # normalised form of union type shorthand (type => ['a', 'b', ...]).
2405 208 my $rc = 0;
2406 208 my @types;
2407 208 foreach my $rule(@{$rules}) {
2408 276 ▶ if(ref($rule) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule ne "HASH") 1 275 276
2409 1 _error($logger, "$rule_description: Parameter $param_label rules must be a hash reference");
2410 }
2411 275 ▶ if(!defined($rule->{'type'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule->{"type"}) 0 275 275
2412 0 _error($logger, "$rule_description: Parameter $param_label is missing a type in an alternative");
2413 }
2414 275 push @types, $rule->{'type'};
2415 275 my $result;
2416 275 eval {
2417 275 $result = validate_strict({ input => { $key => $value }, schema => { $key => $rule }, logger => undef, custom_types => $custom_types });
2418 };
2419 275 ▶ if(!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 174 101 275
2420 # Capture coercion performed by the successful sub-validation
2421 # (e.g. integer/number coercion) so the outer scope sees it.
2422 174 ▶ $value = $result->{$key} if(defined($result));
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 174 0 174
2423 174 $rc = 1;
2424 174 last;
2425 }
2426 }
2427 207 ▶ if(!$rc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 33 174 207
2428 33 _error($logger, "$rule_description: Parameter $param_label must be one of " . join(', ', @types));
2429 0 $invalid_args{$key} = 1;
2430 }
2431 } else {
2432 2 _error($logger, "$rule_description: Parameter $param_label schema is empty arrayref");
2433 }
2434 } elsif(ref($rules)) {
2435 2 _error($logger, 'rules must be a hash reference or string');
2436 }
2437  
2438 7441 $validated_args{$key} = $value;
2439 }
2440  
2441 # Validate parameter relationships
2442 9731 ▶ my @relationships = ref($params->{'relationships'}) eq 'ARRAY' ? @{$params->{'relationships'}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'relationships'} eq 'ARRAY' ? : 59 9672 9731
2443  
2444 # Desugar per-parameter 'needs' into dependency relationships
2445 # (the sort, which keeps the relationship order stable, is only needed when
2446 # some parameter actually has a 'needs' rule)
2447 9731 ▶ foreach my $name ($has_needs ? sort(keys %{$schema}) : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$has_needs ? : 369 9362 9731
2448 688 my $rules = $schema->{$name};
2449 688 ▶ next unless(ref($rules) eq 'HASH' && defined($rules->{'needs'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules eq "HASH" and defined $rules->{"needs"} 319 369 688
Condition100%3/3
ref $rules eq "HASH" and defined $rules->{"needs"}
!ll&&!rl&&r
102217369
MC/DC100%2/2
ref $rules eq "HASH" and defined $rules->{"needs"}
ref $rules eq "HASH" defined $rules->{"needs"}
Truth table100%3/3
A and B
A= ref $rules eq "HASH"B= defined $rules->{"needs"}
ABresult
0X0
100
111
2450 369 my $needs = $rules->{'needs'};
2451 369 ▶ if(ref($needs) && ref($needs) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $needs and ref $needs ne "ARRAY") 1 368 369
Condition100%3/3
ref $needs and ref $needs ne "ARRAY"
!ll&&!rl&&r
23661
MC/DC100%2/2
ref $needs and ref $needs ne "ARRAY"
ref $needs ref $needs ne "ARRAY"
Truth table100%3/3
A and B
A= ref $needsB= ref $needs ne "ARRAY"
ABresult
0X0
100
111
2452 1 _error($logger, "$schema_description: 'needs' for parameter '$name' must be a string or an arrayref");
2453 }
2454 368 ▶ foreach my $required (ref($needs) ? @{$needs} : ($needs)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $needs ? : 366 2 368
2455 372 push @relationships, { type => 'dependency', param => $name, requires => $required };
2456 }
2457 }
2458  
2459 9730 ▶ if(@relationships) {
Branch: 1 branch
BranchTrueFalseTotal
if (@relationships) 425 9305 9730
2460 425 _validate_relationships(\%validated_args, \@relationships, $logger, $schema_description);
2461 }
2462  
2463 9407 ▶ if(my $cross_validation = $params->{'cross_validation'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cross_validation = $params->{"cross_validation"}) 57 9350 9407
2464 57 foreach my $validator_name(keys %{$cross_validation}) {
2465 64 my $validator = $cross_validation->{$validator_name};
2466 64 ▶ if((!ref($validator)) || (ref($validator) ne 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $validator or ref $validator ne "CODE") 2 62 64
Condition66%2/3
not ref $validator or ref $validator ne "CODE"
l!l&&r!l&&!r
2062
MC/DC50%1/2
not ref $validator or ref $validator ne "CODE"
not ref $validator ref $validator ne "CODE"
Truth table66%2/3
not(A) or B
A= not ref $validatorB= ref $validator ne "CODE"
ABresult
1X1
011
000
2467 2 _error($logger, "$schema_description: cross_validation $validator is not a code snippet");
2468 0 _unreachable($logger); # Defensive: _error always croaks, so this is never reached.
2469 }
2470 62 ▶ if(my $error = &{$validator}(\%validated_args, $validator)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &{$validator;}(\%validated_args, $validator)) 26 35 61
2471 26 _error($logger, $error);
2472 # We have no idea which parameters are still valid, so let's invalidate them all
2473 0 return;
2474 }
2475 }
2476 }
2477  
2478 9378 foreach my $key(keys %invalid_args) {
2479 0 delete $validated_args{$key};
2480 }
2481  
2482 9378 ▶ if($are_positional_args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($are_positional_args == 1) 24 9354 9378
2483 24 my @rc;
2484 24 foreach my $key (keys %{$schema}) {
2485 # Use exists() rather than if(my $value = ...) so that falsy but
2486 # valid coerced values (integer 0, empty string, undef from an
2487 # absent optional) are not silently dropped from the return array.
2488 40 ▶ if(exists $validated_args{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $validated_args{$key}) 38 2 40
2489 38 my $value = delete $validated_args{$key};
2490 38 my $position = $schema->{$key}->{'position'};
2491 38 ▶ if(defined($rc[$position])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc[$position]) 2 36 38
2492 2 _error($logger, "$schema_description: $key: position $position appears twice");
2493 }
2494 36 $rc[$position] = $value;
2495 }
2496 }
2497 22 return \@rc;
2498 }
2499 9354 return \%validated_args;
2500 }
2501  
2502 =head2 compile_schema
2503  
2504 my $validator = compile_schema(\%schema);
2505 my $result = $validator->(\%input);
2506  
2507 # with optional keyword args
2508 my $validator = compile_schema(\%schema,
2509 description => 'User registration',
2510 custom_types => \%types,
2511 unknown_parameter_handler => 'warn',
2512 );
2513  
2514 Pre-captures a schema (and any optional keyword arguments accepted by
2515 C<validate_strict>) into a reusable validator closure. Calling the returned
2516 coderef is equivalent to:
2517  
2518 validate_strict(schema => \%schema, input => \%input, %opts);
2519  
2520 but the schema is analysed once, when C<compile_schema> is called, rather than
2521 on every call - useful when the same schema is applied repeatedly in a hot
2522 path. The result, the errors raised, the warnings and the logger calls are
2523 exactly those of C<validate_strict>; only the speed differs.
2524  
2525 Fields that use a built-in type (C<string>, C<integer>, C<number>, C<boolean>,
2526 C<arrayref>, C<hashref>, C<scalar>, C<coderef>, C<object> and their synonyms)
2527 with any of the rules C<min>, C<max>, C<optional>, C<nullable>, C<default>,
2528 C<description>, C<error_msg>, C<matches>, C<memberof> (also C<enum> and
2529 C<values>) and C<case_sensitive> are validated by a fast path. Any other
2530 field - one that uses C<callback>, C<transform>, union types, C<aliases>,
2531 C<needs>, nested C<schema>, custom types, the class and element rules, and so
2532 on - is validated by the general code, as before, so using such a rule never
2533 changes behaviour; it just does not get faster. Positional schemas (those with
2534 C<position>) are not precompiled. On a typical five-field schema a compiled
2535 validator runs about three times faster than calling C<validate_strict>
2536 directly.
2537  
2538 B<The schema must not be modified after it has been compiled>, since what was
2539 learnt about it is kept. Compile a new validator instead.
2540  
2541 =head3 Arguments
2542  
2543 =over 4
2544  
2545 =item * C<\%schema> (required)
2546  
2547 The validation schema as a hashref or arrayref, identical to the C<schema>
2548 argument of C<validate_strict>.
2549  
2550 =item * C<%opts> (optional)
2551  
2552 Any keyword arguments accepted by C<validate_strict> other than C<schema>
2553 and C<input>: C<description>, C<custom_types>,
2554 C<unknown_parameter_handler>, C<logger>, C<relationships>,
2555 C<cross_validation>, etc.
2556  
2557 =back
2558  
2559 =head3 Returns
2560  
2561 A code reference C<sub ($input) -E<gt> \%validated>.
2562  
2563 =head3 API SPECIFICATION
2564  
2565 =head4 Input
2566  
2567 {
2568 schema => { type => ['hashref', 'arrayref'] },
2569 opts => { type => 'hashref', optional => 1 }, # passed as trailing key/value pairs
2570 }
2571  
2572 =head4 Output
2573  
2574 { type => 'coderef' }
2575  
2576 The returned coderef has the signature C<sub ($input) -E<gt> \%validated>,
2577 and croaks in the same way as C<validate_strict>.
2578  
2579 =cut
2580  
2581 sub compile_schema
2582 852 ▶{
Subroutine compile_schema: called CC=7 SCAR=19.5
Pod documented
2583 852 my ($schema, %opts) = @_;
2584 852 ▶ unless(ref($schema) eq 'HASH' || ref($schema) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH" or ref $schema eq "ARRAY") 3 849 852
Condition100%3/3
ref $schema eq "HASH" or ref $schema eq "ARRAY"
l!l&&r!l&&!r
84723
MC/DC100%2/2
ref $schema eq "HASH" or ref $schema eq "ARRAY"
ref $schema eq "HASH" ref $schema eq "ARRAY"
Truth table100%3/3
A or B
A= ref $schema eq "HASH"B= ref $schema eq "ARRAY"
ABresult
1X1
011
000
2585 3 Carp::croak('compile_schema: schema must be a hash or array reference');
2586 }
2587  
2588 # Options that would override the schema or input are left to
2589 # validate_strict to resolve, exactly as before, with nothing precompiled.
2590 849 ▶ if(grep { exists($opts{$_}) } qw(schema input args _compiled)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {exists $opts{$_};} "schema", "input", "args", "_compiled") 1 848 849
2591 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2592 1 my $input = shift;
2593 1 return validate_strict(schema => $schema, input => $input, %opts);
2594 1 };
2595 }
2596  
2597 # An arrayref schema is normalised once here rather than on every call. Any
2598 # error it would raise is left to surface at call time, as before.
2599 848 ▶ if(ref($schema) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "ARRAY") 2 846 848
2600 2 my $normalised = eval { _schema_from_arrayref($schema, undef) };
2601 2 ▶ $schema = $normalised if($normalised);
Branch: 1 branch
BranchTrueFalseTotal
if $normalised 1 1 2
2602 }
2603  
2604 848 my $compiled = _compile_schema($schema, $opts{'custom_types'});
2605 848 my %params = (schema => $schema, %opts);
2606 848 ▶ $params{'_compiled'} = $compiled if($compiled);
Branch: 1 branch
BranchTrueFalseTotal
if $compiled 847 1 848
2607  
2608 23156 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2609 # Setting input in place avoids copying the option hash on every call.
2610 # local restores it on exit, so re-entrant and recursive use is safe.
2611 23156 local $params{'input'} = shift;
2612 23156 return validate_strict(\%params);
2613 848 };
2614 }
2615  
2616 # _compile_schema($schema, $custom_types)
2617 #
2618 # Pre-compute everything about a named-parameter schema that does not depend on
2619 # the input, for use by compile_schema. Returns undef when the schema is not
2620 # suitable (positional, wrapped in 'members', and so on), in which case
2621 # validate_strict simply interprets it as usual.
2622 #
2623 # For each field whose rules are *fully* understood - a plain built-in type and
2624 # a small set of common rules - a record is stored in {fields}; validate_strict
2625 # validates such fields without the generic rule interpreter. Anything unusual
2626 # (callbacks, transforms, unions, aliases, nested schemas, custom types, bad
2627 # constraint values, ...) is left out and so takes the generic path, which keeps
2628 # a single implementation of the complicated rules and of every error path.
2629 sub _compile_schema
2630 923 ▶{
Subroutine _compile_schema: called CC=13 SCAR=25.7
2631 923 my ($schema, $custom_types) = @_;
2632  
2633 923 ▶ return unless(ref($schema) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 2 921 923
2634 921 ▶ return if(exists($schema->{'members'})); # 'members' may denote a wrapper
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"members"} 1 920 921
2635  
2636 920 my %type_info = (
2637 string => [_T_STRING, 's'], str => [_T_STRING, 's'],
2638 integer => [_T_INT, 'n'], int => [_T_INT, undef],
2639 number => [_T_NUM, 'n'], float => [_T_NUM, 'n'],
2640 num => [_T_NUM, undef], double => [_T_NUM, undef],
2641 boolean => [_T_BOOL, undef], bool => [_T_BOOL, undef],
2642 arrayref => [_T_ARRAY, 'a'], hashref => [_T_HASH, 'h'],
2643 scalar => [_T_SCALAR, undef], coderef => [_T_CODE, undef],
2644 object => [_T_OBJECT, undef],
2645 );
2646 # Types whose values are plain scalars, so that 'matches' behaves simply
2647 920 my %scalar_like = map { $_ => 1 } qw(string str integer int number float num double boolean bool scalar);
2648 920 my %allowed = map { $_ => 1 } qw(type min max optional nullable default description error_msg
2649 matches memberof enum values case_sensitive);
2650  
2651 920 my $has_aliases = 0;
2652 920 my $has_needs = 0;
2653 920 my %fields;
2654  
2655 920 foreach my $key (keys %{$schema}) {
2656 994 my $rules = $schema->{$key};
2657 994 ▶ next unless(defined($rules));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 6 988 994
2658  
2659 988 ▶ if(ref($rules) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rules eq 'HASH') { } 957 31 988
elsif (ref $rules eq '') { } 31 0 31
2660 # Positional schemas are not compiled, and a slurp rule is an error
2661 # when there is no position; leave both to the generic code.
2662 957 ▶ return if(exists($rules->{'position'}) || exists($rules->{'slurp'}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $rules->{"position"} or exists $rules->{"slurp"} 2 955 957
Condition100%3/3
exists $rules->{"position"} or exists $rules->{"slurp"}
l!l&&r!l&&!r
11955
MC/DC100%2/2
exists $rules->{"position"} or exists $rules->{"slurp"}
exists $rules->{"position"} exists $rules->{"slurp"}
Truth table100%3/3
A or B
A= exists $rules->{"position"}B= exists $rules->{"slurp"}
ABresult
1X1
011
000
2663 955 ▶ $has_aliases = 1 if(ref($rules->{'aliases'}) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules->{"aliases"} eq "ARRAY" 7 948 955
2664 955 ▶ $has_needs = 1 if(defined($rules->{'needs'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"needs"} 26 929 955
2665 } elsif(ref($rules) eq '') {
2666 31 $rules = { type => $rules }; # as the generic code does
2667 } else {
2668 0 next;
2669 }
2670  
2671 986 my $f = _compile_field($key, $rules, \%type_info, \%scalar_like, \%allowed, $custom_types);
2672 986 ▶ $fields{$key} = $f if($f);
Branch: 1 branch
BranchTrueFalseTotal
if $f 520 466 986
2673 }
2674  
2675 return {
2676 schema => $schema,
2677 fields => \%fields,
2678 has_aliases => $has_aliases,
2679 has_needs => $has_needs,
2680 918 ▶ positional => (%{$schema} ? 0 : -1),
Branch: 1 branch
BranchTrueFalseTotal
%{$schema;} ? : 912 6 918
2681 };
2682 }
2683  
2684 # _compile_field($key, \%rules, ...)
2685 #
2686 # Return the record for one field, or undef if its rules are not entirely within
2687 # what the fast path in validate_strict reproduces. When in any doubt, undef.
2688 sub _compile_field
2689 986 ▶{
Subroutine _compile_field: called CC=43 SCAR=37.6
2690 986 my ($key, $rules, $type_info, $scalar_like, $allowed, $custom_types) = @_;
2691  
2692 986 ▶ return if(grep { !$allowed->{$_} } keys %{$rules});
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $allowed->{$_};} keys %{$rules;} 169 817 986
2693  
2694 # Every value must be a plain scalar, apart from the two that may be refs
2695 817 foreach my $name (keys %{$rules}) {
2696 1845 ▶ next if($name eq 'default');
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "default" 43 1802 1845
2697 1802 my $v = $rules->{$name};
2698 1802 ▶ if(ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 210 1592 1802
2699 210 ▶ next if($name eq 'matches' && ref($v) eq 'Regexp');
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "matches" and ref $v eq "Regexp" 49 161 210
Condition100%3/3
$name eq "matches" and ref $v eq "Regexp"
!ll&&!rl&&r
1421949
MC/DC100%2/2
$name eq "matches" and ref $v eq "Regexp"
$name eq "matches" ref $v eq "Regexp"
Truth table100%3/3
A and B
A= $name eq "matches"B= ref $v eq "Regexp"
ABresult
0X0
100
111
2700 161 ▶ next if(($name eq 'memberof' || $name eq 'enum' || $name eq 'values') && ref($v) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "memberof" || $name eq "enum" || $name eq "values" and ref $v eq "ARRAY" 116 45 161
Condition88%8/9
$name eq "memberof" || $name eq "enum" || $name eq "values" and ref $v eq "ARRAY"
!ll&&!rl&&r
450116
$name eq "memberof" or $name eq "enum" or $name eq "values"
l!l&&r!l&&!r
971945
$name eq "memberof" or $name eq "enum"
l!l&&r!l&&!r
772064
MC/DC75%3/4
$name eq "memberof" || $name eq "enum" || $name eq "values" and ref $v eq "ARRAY"
$name eq "memberof" $name eq "enum" $name eq "values" ref $v eq "ARRAY"
Truth table57%4/7
A or B or C and D
A= $name eq "memberof"B= $name eq "enum"C= $name eq "values"D= ref $v eq "ARRAY"
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
2701 45 return;
2702 }
2703 }
2704  
2705 772 my $type = $rules->{'type'};
2706 772 ▶ return unless(defined($type) && !ref($type));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and not ref $type 2 770 772
Condition66%2/3
defined $type and not ref $type
!ll&&!rl&&r
20770
MC/DC50%1/2
defined $type and not ref $type
defined $type not ref $type
Truth table66%2/3
A and not(B)
A= defined $typeB= not ref $type
ABresult
0X0
100
111
2707 770 ▶ my $info = $type_info->{lc($type)} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $$type_info{lc $type} or return 758 12 770
2708 758 ▶ return if(ref($custom_types) eq 'HASH' && exists($custom_types->{lc($type)}));
Branch: 1 branch
BranchTrueFalseTotal
if ref $custom_types eq "HASH" and exists $$custom_types{lc $type} 2 756 758
Condition100%3/3
ref $custom_types eq "HASH" and exists $$custom_types{lc $type}
!ll&&!rl&&r
75512
MC/DC100%2/2
ref $custom_types eq "HASH" and exists $$custom_types{lc $type}
ref $custom_types eq "HASH" exists $$custom_types{lc $type}
Truth table100%3/3
A and B
A= ref $custom_types eq "HASH"B= exists $$custom_types{lc $type}
ABresult
0X0
100
111
2709  
2710 756 my $label = "'$key'";
2711 756 ▶ if(exists($rules->{'description'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rules->{"description"}) 56 700 756
2712 56 ▶ return unless(defined($rules->{'description'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules->{"description"} 1 55 56
2713 55 $label = "'$key' ($rules->{description})";
2714 }
2715  
2716 my %f = (
2717 rules => $rules,
2718 type => $type,
2719 tname => lc($type),
2720 t => $info->[0],
2721 mk => $info->[1],
2722 label => $label,
2723 opt => 0,
2724 has_default => exists($rules->{'default'}) ? 1 : 0,
2725 755 ▶ default => $rules->{'default'},
Branch: 1 branch
BranchTrueFalseTotal
exists $rules->{'default'} ? : 43 712 755
2726 );
2727  
2728 # optional wins over nullable if both are present, as in validate_strict
2729 755 ▶ if(exists($rules->{'optional'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $rules->{'optional'}) { } 150 605 755
elsif ($rules->{'nullable'}) { } 37 568 605
2730 150 ▶ $f{'opt'} = $rules->{'optional'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rules->{'optional'} ? : 113 37 150
2731 } elsif($rules->{'nullable'}) {
2732 37 $f{'opt'} = 1;
2733 }
2734  
2735 755 foreach my $bound (qw(min max)) {
2736 1378 ▶ next unless(exists($rules->{$bound}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$bound} 996 382 1378
2737 382 my $v = $rules->{$bound};
2738 382 ▶ return unless(defined($v) && $info->[1] && Scalar::Util::looks_like_number($v));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $info->[1] and &Scalar::Util::looks_like_number($v) 153 229 382
Condition100%6/6
defined $v and $info->[1] and &Scalar::Util::looks_like_number($v)
!ll&&!rl&&r
14211229
defined $v and $info->[1]
!ll&&!rl&&r
19123240
MC/DC100%3/3
defined $v and $info->[1] and &Scalar::Util::looks_like_number($v)
defined $v $info->[1] &Scalar::Util::looks_like_number($v)
Truth table100%4/4
A and B and C
A= defined $vB= $info->[1]C= &Scalar::Util::looks_like_number($v)
ABCresult
0XX0
10X0
1100
1111
2739 229 ▶ return if($bound eq 'min' && $info->[1] eq 's' && $v < 0); # meaningless; generic code reports it
Branch: 1 branch
BranchTrueFalseTotal
if $bound eq "min" and $info->[1] eq "s" and $v < 0 4 225 229
Condition100%6/6
$bound eq "min" and $info->[1] eq "s" and $v < 0
!ll&&!rl&&r
180454
$bound eq "min" and $info->[1] eq "s"
!ll&&!rl&&r
7810249
MC/DC100%3/3
$bound eq "min" and $info->[1] eq "s" and $v < 0
$bound eq "min" $info->[1] eq "s" $v < 0
Truth table100%4/4
A and B and C
A= $bound eq "min"B= $info->[1] eq "s"C= $v < 0
ABCresult
0XX0
10X0
1100
1111
2740 225 $f{$bound} = $v;
2741 }
2742 598 ▶ return if(defined($f{'min'}) && defined($f{'max'}) && $f{'min'} > $f{'max'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $f{"min"} and defined $f{"max"} and $f{"min"} > $f{"max"} 11 587 598
Condition100%6/6
defined $f{"min"} and defined $f{"max"} and $f{"min"} > $f{"max"}
!ll&&!rl&&r
5513611
defined $f{"min"} and defined $f{"max"}
!ll&&!rl&&r
45110047
MC/DC100%3/3
defined $f{"min"} and defined $f{"max"} and $f{"min"} > $f{"max"}
defined $f{"min"} defined $f{"max"} $f{"min"} > $f{"max"}
Truth table100%4/4
A and B and C
A= defined $f{"min"}B= defined $f{"max"}C= $f{"min"} > $f{"max"}
ABCresult
0XX0
10X0
1100
1111
2743  
2744 587 ▶ if(exists($rules->{'matches'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rules->{"matches"}) 68 519 587
2745 68 ▶ return unless($scalar_like->{lc($type)});
Branch: 1 branch
BranchTrueFalseTotal
unless $$scalar_like{lc $type} 16 52 68
2746 52 my $m = $rules->{'matches'};
2747 52 ▶ return unless(defined($m));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 52 52
2748 52 ▶ $f{'re'} = (ref($m) eq 'Regexp') ? $m : qr/\Q$m\E/;
Branch: 1 branch
BranchTrueFalseTotal
ref $m eq 'Regexp' ? : 38 14 52
2749 52 $f{'match_src'} = $m;
2750 }
2751  
2752 571 my @list_names = grep { exists($rules->{$_}) } qw(memberof enum values);
2753 571 ▶ if(@list_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (@list_names) { } 126 445 571
elsif (exists $rules->{'case_sensitive'}) { } 19 426 445
2754 # exactly one, and not combined with min/max (which is an error)
2755 126 ▶ return if(@list_names > 1 || exists($rules->{'min'}) || exists($rules->{'max'}));
Branch: 1 branch
BranchTrueFalseTotal
if @list_names > 1 or exists $rules->{"min"} or exists $rules->{"max"} 13 113 126
Condition100%6/6
@list_names > 1 or exists $rules->{"min"} or exists $rules->{"max"}
l!l&&r!l&&!r
121113
@list_names > 1 or exists $rules->{"min"}
l!l&&r!l&&!r
111114
MC/DC100%3/3
@list_names > 1 or exists $rules->{"min"} or exists $rules->{"max"}
@list_names > 1 exists $rules->{"min"} exists $rules->{"max"}
Truth table100%4/4
A or B or C
A= @list_names > 1B= exists $rules->{"min"}C= exists $rules->{"max"}
ABCresult
1XX1
01X1
0011
0000
2756 113 ▶ return unless(ref($rules->{$list_names[0]}) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$rules{$list_names[0]} eq "ARRAY" 19 94 113
2757 94 $f{'list'} = $rules->{$list_names[0]};
2758 } elsif(exists($rules->{'case_sensitive'})) {
2759 19 return; # meaningless without a list; leave it to the generic code
2760 }
2761  
2762 520 return \%f;
2763 }
2764  
2765 # _schema_from_arrayref($arrayref, $logger)
2766 #
2767 # Normalise an arrayref schema:
2768 # [ { name => 'param', type => 'string', ... }, ... ]
2769 # to the standard named-parameter hashref form:
2770 # { param => { type => 'string', ... }, ... }
2771 #
2772 # The 'name' key is consumed during conversion and does not become a rule.
2773 # Croaks if any element is not a hashref, is missing 'name', or if a name
2774 # appears more than once.
2775 sub _schema_from_arrayref
2776 20 ▶{
Subroutine _schema_from_arrayref: called CC=5 SCAR=16.1
2777 20 my ($arrayref, $logger) = @_;
2778  
2779 20 my %schema;
2780 20 foreach my $spec (@{$arrayref}) {
2781 27 ▶ _error($logger, "validate_strict: each arrayref schema element must be a hashref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 2 25 27
2782 unless ref($spec) eq 'HASH';
2783 _error($logger, "validate_strict: arrayref schema element must have a 'name' key")
2784 25 ▶ unless exists($spec->{'name'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"name"} 4 21 25
2785 21 my %rule = %{$spec};
2786 21 my $name = delete $rule{'name'};
2787 _error($logger, "validate_strict: duplicate parameter '$name' in arrayref schema")
2788 21 ▶ if exists($schema{$name});
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema{$name} 2 19 21
2789 19 $schema{$name} = \%rule;
2790 }
2791 12 return \%schema;
2792 }
2793  
2794 # Return number of visible characters not number of bytes
2795 # Ensure string is decoded into Perl characters
2796 sub _number_of_characters
2797 1771 ▶{
Subroutine _number_of_characters: called CC=4 SCAR=13.9
2798 1771 my $value = $_[0];
2799  
2800 1771 ▶ return if(!defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1770 1771
2801  
2802 1770 ▶ if($value !~ /[^[:ascii:]]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /[^[:ascii:]]/) 1626 144 1770
2803 1626 return length($value);
2804 }
2805 # Decode only if it's not already a Perl character string
2806 144 ▶ $value = decode_utf8($value) unless utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($value) 23 121 144
2807  
2808 # Count grapheme clusters (visible characters).
2809 # \X matches one extended grapheme cluster; Perl's Unicode tables are kept
2810 # current with each release, correctly handling ZWJ sequences and emoji
2811 # modifier sequences that Unicode::GCString 2013.10 could not.
2812 144 return scalar(() = $value =~ /\X/g);
2813 }
2814  
2815 125 ▶sub _apply_nested_defaults {
Subroutine _apply_nested_defaults: called CC=9 SCAR=22.2
2816 125 my ($input, $schema) = @_;
2817 125 my %result = %$input;
2818  
2819 125 foreach my $key (keys %$schema) {
2820 207 my $rules = $schema->{$key};
2821  
2822 207 ▶ if (ref $rules eq 'HASH' && exists $rules->{default} && !exists $result{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq "HASH" and exists $rules->{"default"} and not exists $result{$key}) 11 196 207
Condition83%5/6
ref $rules eq "HASH" and exists $rules->{"default"} and not exists $result{$key}
!ll&&!rl&&r
189711
ref $rules eq "HASH" and exists $rules->{"default"}
!ll&&!rl&&r
018918
MC/DC66%2/3
ref $rules eq "HASH" and exists $rules->{"default"} and not exists $result{$key}
ref $rules eq "HASH" exists $rules->{"default"} not exists $result{$key}
Truth table75%3/4
A and B and not(C)
A= ref $rules eq "HASH"B= exists $rules->{"default"}C= not exists $result{$key}
ABCresult
0XX0
10X0
1100
1111
2823 11 ▶ $result{$key} //= $rules->{default};
Condition33%1/3
$result{$key} //= $rules->{"default"}
l!l&&r!l&&!r
0110
MC/DC0%0/2
$result{$key} //= $rules->{"default"}
$result{$key} $rules->{"default"}
Truth table33%1/3
A //= B
A= $result{$key}B= $rules->{"default"}
ABresult
1X1
011
000
2824 }
2825  
2826 # Recursively handle nested schema
2827 207 ▶ if((ref $rules eq 'HASH') && $rules->{schema} && (ref $result{$key} eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules eq "HASH" and $rules->{"schema"} and ref $result{$key} eq "HASH") 10 197 207
Condition83%5/6
ref $rules eq "HASH" and $rules->{"schema"} and ref $result{$key} eq "HASH"
!ll&&!rl&&r
190710
ref $rules eq "HASH" and $rules->{"schema"}
!ll&&!rl&&r
019017
MC/DC66%2/3
ref $rules eq "HASH" and $rules->{"schema"} and ref $result{$key} eq "HASH"
ref $rules eq "HASH" $rules->{"schema"} ref $result{$key} eq "HASH"
Truth table75%3/4
A and B and C
A= ref $rules eq "HASH"B= $rules->{"schema"}C= ref $result{$key} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
2828 10 $result{$key} = _apply_nested_defaults($result{$key}, $rules->{schema});
2829 }
2830 }
2831  
2832 125 return \%result;
2833 }
2834  
2835 425 ▶sub _validate_relationships {
Subroutine _validate_relationships: called CC=10 SCAR=23.1
2836 425 my ($validated_args, $relationships, $logger, $description) = @_;
2837  
2838 425 ▶ return unless ref($relationships) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $relationships eq "ARRAY" 0 425 425
2839  
2840 425 foreach my $rel (@$relationships) {
2841 429 ▶ my $type = $rel->{type} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $rel->{"type"} or next 429 0 429
2842  
2843 429 ▶ if ($type eq 'mutually_exclusive') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'mutually_exclusive') { } 10 419 429
elsif ($type eq 'required_group') { } 7 412 419
elsif ($type eq 'conditional_requirement') { } 9 403 412
elsif ($type eq 'dependency') { } 378 25 403
elsif ($type eq 'value_constraint') { } 17 8 25
elsif ($type eq 'value_conditional') { } 7 1 8
2844 10 _validate_mutually_exclusive($validated_args, $rel, $logger, $description);
2845 } elsif ($type eq 'required_group') {
2846 7 _validate_required_group($validated_args, $rel, $logger, $description);
2847 } elsif ($type eq 'conditional_requirement') {
2848 9 _validate_conditional_requirement($validated_args, $rel, $logger, $description);
2849 } elsif ($type eq 'dependency') {
2850 378 _validate_dependency($validated_args, $rel, $logger, $description);
2851 } elsif ($type eq 'value_constraint') {
2852 17 _validate_value_constraint($validated_args, $rel, $logger, $description);
2853 } elsif ($type eq 'value_conditional') {
2854 7 _validate_value_conditional($validated_args, $rel, $logger, $description);
2855 } else {
2856 1 _error($logger, "Unknown relationship type $type");
2857 }
2858 }
2859 }
2860  
2861 14 ▶sub _validate_mutually_exclusive {
Subroutine _validate_mutually_exclusive: called CC=5 SCAR=16.2
2862 14 my ($args, $rel, $logger, $description) = @_;
2863  
2864 14 ▶ my @params = @{$rel->{params} || []};
Condition50%1/2
$rel->{'params'} || []
l!l
140
MC/DC0%0/1
$rel->{'params'} || []
$rel->{'params'}
Truth table50%1/2
A
A= $rel->{'params'}
Aresult
11
00
2865 14 ▶ return unless @params >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @params >= 2 0 14 14
2866  
2867 14 my @present = grep { _param_defined($args, $_) } @params;
2868  
2869 14 ▶ if (@present > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@present > 1) 7 7 14
2870 7 ▶ my $msg = $rel->{description} || 'Cannot specify both ' . join(' and ', @present);
Condition100%2/2
$rel->{'description'} || 'Cannot specify both ' . join(' and ', @present)
l!l
25
MC/DC100%1/1
$rel->{'description'} || 'Cannot specify both ' . join(' and ', @present)
$rel->{'description'}
Truth table100%2/2
A
A= $rel->{'description'}
Aresult
11
00
2871 7 _error($logger, "$description: $msg");
2872 }
2873 }
2874  
2875 9 ▶sub _validate_required_group {
Subroutine _validate_required_group: called CC=5 SCAR=16.1
2876 9 my ($args, $rel, $logger, $description) = @_;
2877  
2878 9 ▶ my @params = @{$rel->{params} || []};
Condition50%1/2
$rel->{'params'} || []
l!l
90
MC/DC0%0/1
$rel->{'params'} || []
$rel->{'params'}
Truth table50%1/2
A
A= $rel->{'params'}
Aresult
11
00
2879 9 ▶ return unless @params >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @params >= 2 1 8 9
2880  
2881 8 my @present = grep { _param_defined($args, $_) } @params;
2882  
2883 8 ▶ if (@present == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@present == 0) 4 4 8
2884 my $msg = $rel->{description} ||
2885 4 ▶ 'Must specify at least one of: ' . join(', ', @params);
Condition100%2/2
$rel->{'description'} || 'Must specify at least one of: ' . join(', ', @params)
l!l
13
MC/DC100%1/1
$rel->{'description'} || 'Must specify at least one of: ' . join(', ', @params)
$rel->{'description'}
Truth table100%2/2
A
A= $rel->{'description'}
Aresult
11
00
2886 4 _error($logger, "$description: $msg");
2887 }
2888 }
2889  
2890 13 ▶sub _validate_conditional_requirement {
Subroutine _validate_conditional_requirement: called CC=7 SCAR=19.5
2891 13 my ($args, $rel, $logger, $description) = @_;
2892  
2893 13 ▶ my $if_param = $rel->{if} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $if_param = $rel->{"if"} or return 12 1 13
2894 12 ▶ my $then_param = $rel->{then_required} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $then_param = $rel->{"then_required"} or return 11 1 12
2895  
2896 # If the condition parameter is present and defined
2897 11 ▶ if (_param_defined($args, $if_param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_param_defined($args, $if_param)) 9 2 11
2898 # Check if it's truthy (for booleans and general values)
2899 9 ▶ if ($args->{$if_param}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$if_param}) 7 2 9
2900 # Then the required parameter must also be present
2901 7 ▶ unless (_param_defined($args, $then_param)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_param_defined($args, $then_param)) 3 4 7
2902 3 ▶ my $msg = $rel->{description} || "When $if_param is specified, $then_param is required";
Condition50%1/2
$rel->{'description'} || "When $if_param is specified, $then_param is required"
l!l
03
MC/DC0%0/1
$rel->{'description'} || "When $if_param is specified, $then_param is required"
$rel->{'description'}
Truth table50%1/2
A
A= $rel->{'description'}
Aresult
11
00
2903 3 _error($logger, "$description: $msg");
2904 }
2905 }
2906 }
2907 }
2908  
2909 381 ▶sub _validate_dependency {
Subroutine _validate_dependency: called CC=6 SCAR=17.9
2910 381 my ($args, $rel, $logger, $description) = @_;
2911  
2912 381 ▶ my $param = $rel->{param} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $param = $rel->{"param"} or return 380 1 381
2913 380 ▶ my $requires = $rel->{requires} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $requires = $rel->{"requires"} or return 380 0 380
2914  
2915 # If param is present, requires must also be present
2916 380 ▶ if (_param_defined($args, $param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_param_defined($args, $param)) 315 65 380
2917 315 ▶ unless (_param_defined($args, $requires)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_param_defined($args, $requires)) 300 15 315
2918 300 ▶ my $msg = $rel->{description} || "$param requires $requires to be specified";
Condition100%2/2
$rel->{'description'} || "$param requires $requires to be specified"
l!l
1299
MC/DC100%1/1
$rel->{'description'} || "$param requires $requires to be specified"
$rel->{'description'}
Truth table100%2/2
A
A= $rel->{'description'}
Aresult
11
00
2919 300 _error($logger, "$description: $msg");
2920 }
2921 }
2922 }
2923  
2924 32 ▶sub _validate_value_constraint {
Subroutine _validate_value_constraint: called CC=16 SCAR=27.9
2925 32 my ($args, $rel, $logger, $description) = @_;
2926  
2927 32 ▶ my $if_param = $rel->{if} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $if_param = $rel->{"if"} or return 32 0 32
2928 32 ▶ my $then_param = $rel->{then} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $then_param = $rel->{"then"} or return 32 0 32
2929 32 ▶ my $operator = $rel->{operator} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $operator = $rel->{"operator"} or return 32 0 32
2930 32 my $value = $rel->{value};
2931 32 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 32 32
2932  
2933 # If the condition parameter is present and truthy
2934 32 ▶ if (_param_defined($args, $if_param) && $args->{$if_param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (_param_defined($args, $if_param) and $args->{$if_param}) 29 3 32
Condition100%3/3
_param_defined($args, $if_param) and $args->{$if_param}
!ll&&!rl&&r
2129
MC/DC100%2/2
_param_defined($args, $if_param) and $args->{$if_param}
_param_defined($args, $if_param) $args->{$if_param}
Truth table100%3/3
A and B
A= _param_defined($args, $if_param)B= $args->{$if_param}
ABresult
0X0
100
111
2935 # Check if the then parameter exists
2936 29 ▶ if (_param_defined($args, $then_param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_param_defined($args, $then_param)) 29 0 29
2937 29 my $actual = $args->{$then_param};
2938 29 my $valid = 0;
2939  
2940 29 ▶ if ($operator eq '==') {
Branch: 6 branches
BranchTrueFalseTotal
if ($operator eq '==') { } 8 21 29
elsif ($operator eq '!=') { } 4 17 21
elsif ($operator eq '<') { } 4 13 17
elsif ($operator eq '<=') { } 4 9 13
elsif ($operator eq '>') { } 4 5 9
elsif ($operator eq '>=') { } 4 1 5
2941 8 $valid = ($actual == $value);
2942 } elsif ($operator eq '!=') {
2943 4 $valid = ($actual != $value);
2944 } elsif ($operator eq '<') {
2945 4 $valid = ($actual < $value);
2946 } elsif ($operator eq '<=') {
2947 4 $valid = ($actual <= $value);
2948 } elsif ($operator eq '>') {
2949 4 $valid = ($actual > $value);
2950 } elsif ($operator eq '>=') {
2951 4 $valid = ($actual >= $value);
2952 }
2953  
2954 29 ▶ unless ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 17 12 29
2955 17 ▶ my $msg = $rel->{description} || "When $if_param is specified, $then_param must be $operator $value (got $actual)";
Condition100%2/2
$rel->{'description'} || "When $if_param is specified, $then_param must be $operator $value (got $actual)"
l!l
314
MC/DC100%1/1
$rel->{'description'} || "When $if_param is specified, $then_param must be $operator $value (got $actual)"
$rel->{'description'}
Truth table100%2/2
A
A= $rel->{'description'}
Aresult
11
00
2956 17 _error($logger, "$description: $msg");
2957 }
2958 }
2959 }
2960 }
2961  
2962 11 ▶sub _validate_value_conditional {
Subroutine _validate_value_conditional: called CC=8 SCAR=20.9
2963 11 my ($args, $rel, $logger, $description) = @_;
2964  
2965 11 ▶ my $if_param = $rel->{if} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $if_param = $rel->{"if"} or return 11 0 11
2966 11 my $equals = $rel->{equals};
2967 11 ▶ my $then_param = $rel->{then_required} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $then_param = $rel->{"then_required"} or return 11 0 11
2968 11 ▶ return unless defined $equals;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $equals 0 11 11
2969  
2970 # If the parameter has the specific value
2971 11 ▶ if (_param_defined($args, $if_param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_param_defined($args, $if_param)) 9 2 11
2972 9 ▶ if ($args->{$if_param} eq $equals) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$if_param} eq $equals) 6 3 9
2973 # Then the required parameter must be present
2974 6 ▶ unless (_param_defined($args, $then_param)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_param_defined($args, $then_param)) 4 2 6
2975 my $msg = $rel->{description} ||
2976 4 ▶ "When $if_param equals '$equals', $then_param is required";
Condition100%2/2
$rel->{'description'} || "When $if_param equals '${equals}', $then_param is required"
l!l
13
MC/DC100%1/1
$rel->{'description'} || "When $if_param equals '${equals}', $then_param is required"
$rel->{'description'}
Truth table100%2/2
A
A= $rel->{'description'}
Aresult
11
00
2977 4 _error($logger, "$description: $msg");
2978 }
2979 }
2980 }
2981 }
2982  
2983 # Emit either the rule's custom error_msg or the supplied default message.
2984 # Accepts a list for @default_parts so callers can pass join() fragments
2985 # without pre-allocating a concatenated string.
2986 sub _rule_error
2987 30614 ▶{
Subroutine _rule_error: called CC=2 SCAR=7.1
2988 30614 my ($logger, $rules, @default_parts) = @_;
2989 30614 ▶ _error($logger, $rules->{'error_msg'} || join('', @default_parts));
Condition66%2/3
$rules->{'error_msg'} || join('', @default_parts)
l!l&&r!l&&!r
1289293250
MC/DC0%0/2
$rules->{'error_msg'} || join('', @default_parts)
$rules->{'error_msg'} join('', @default_parts)
Truth table66%2/3
A || B
A= $rules->{'error_msg'}B= join('', @default_parts)
ABresult
1X1
011
000
2990 }
2991  
2992 # Package-level cache: maps "refaddr(list):mode" -> [weak_list_ref, lookup_hash].
2993 # Each entry holds a WEAK reference to the original list arrayref alongside the
2994 # compiled lookup hash. When the list goes out of scope and is freed, the weak
2995 # reference becomes undef; the next access detects the stale entry and rebuilds,
2996 # preventing false cache hits after address reuse.
2997 36 my %_pvs_memberof_cache;
2998  
2999 # Return true if $value is present in $list, respecting numeric vs string
3000 # comparison and the case_sensitive flag. Used by both memberof and notmemberof.
3001 # On the first call for a given ($list, mode) pair the lookup hash is built
3002 # (O(k)); subsequent calls with the same live list object are O(1).
3003 sub _value_in_list
3004 1906 ▶{
Subroutine _value_in_list: called CC=14 SCAR=26.4
3005 1906 my ($value, $list, $type, $case_sensitive) = @_;
3006 1906 ▶ my $is_numeric = ($type eq 'integer') || ($type eq 'number') || ($type eq 'float');
Condition100%6/6
$type eq 'integer' || $type eq 'number' || $type eq 'float'
l!l&&r!l&&!r
2211091576
$type eq "integer" or $type eq "number"
l!l&&r!l&&!r
1061151685
MC/DC100%3/3
$type eq 'integer' || $type eq 'number' || $type eq 'float'
$type eq "integer" $type eq "number" $type eq 'float'
Truth table100%4/4
A or B || C
A= $type eq "integer"B= $type eq "number"C= $type eq 'float'
ABCresult
1XX1
01X1
0011
0000
3007 1906 ▶ my $is_icase = !$is_numeric && defined($case_sensitive) && !$case_sensitive;
Condition100%6/6
!$is_numeric && defined $case_sensitive && !$case_sensitive
!ll&&!rl&&r
1397245264
not $is_numeric and defined $case_sensitive
!ll&&!rl&&r
3301067509
MC/DC100%3/3
!$is_numeric && defined $case_sensitive && !$case_sensitive
not $is_numeric defined $case_sensitive !$case_sensitive
Truth table100%4/4
not(A) and B && not(C)
A= not $is_numericB= defined $case_sensitiveC= !$case_sensitive
ABCresult
0XX0
10X0
1100
1111
3008  
3009 # Key combines address and comparison mode so the same list object can be
3010 # cached under multiple modes without collision.
3011 1906 ▶ my $ckey = Scalar::Util::refaddr($list) . ($is_numeric ? 'n' : $is_icase ? 'i' : 's');
Branch: 2 branches
BranchTrueFalseTotal
$is_numeric ? : 330 1576 1906
$is_icase ? : 264 1312 1576
3012 1906 my $entry = $_pvs_memberof_cache{$ckey};
3013  
3014 # Stale check: if the weak ref is dead the list was freed and its address
3015 # may have been reused by a different list - discard the cached hash.
3016 1906 ▶ if(defined($entry) && !defined($entry->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry and not defined $entry->[0]) 0 1906 1906
Condition66%2/3
defined $entry and not defined $entry->[0]
!ll&&!rl&&r
33815680
MC/DC0%0/2
defined $entry and not defined $entry->[0]
defined $entry not defined $entry->[0]
Truth table66%2/3
A and not(B)
A= defined $entryB= not defined $entry->[0]
ABresult
0X0
100
111
3017 0 delete $_pvs_memberof_cache{$ckey};
3018 0 $entry = undef;
3019 }
3020  
3021 1906 ▶ unless(defined $entry) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry) 338 1568 1906
3022 338 my $lookup;
3023 338 ▶ if($is_numeric) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_numeric) { } 58 280 338
elsif ($is_icase) { } 44 236 280
3024 # Normalise to numeric value so "1" and "1.0" hash identically.
3025 58 $lookup = { map { ($_ + 0) => 1 } @{$list} };
3026 } elsif($is_icase) {
3027 44 $lookup = { map { lc($_) => 1 } @{$list} };
3028 } else {
3029 236 $lookup = { map { $_ => 1 } @{$list} };
3030 }
3031 # Store [weak_ref_to_list, lookup_hash] - weak ref does not prevent GC.
3032 338 my $weak = $list;
3033 338 Scalar::Util::weaken($weak);
3034 338 $_pvs_memberof_cache{$ckey} = [$weak, $lookup];
3035 338 $entry = $_pvs_memberof_cache{$ckey};
3036 }
3037  
3038 1906 my $lookup = $entry->[1];
3039 return $is_numeric ? exists($lookup->{$value + 0})
3040 : $is_icase ? exists($lookup->{lc($value)})
3041 1906 ▶ : exists($lookup->{$value});
Branch: 2 branches
BranchTrueFalseTotal
$is_numeric ? : 330 1576 1906
$is_icase ? : 264 1312 1576
3042 }
3043  
3044 # Return true when $args->{$param} is both present (exists) and defined.
3045 sub _param_defined
3046 835 ▶{
Subroutine _param_defined: called CC=2 SCAR=6.9
3047 835 my ($args, $param) = @_;
3048 835 ▶ return exists($args->{$param}) && defined($args->{$param});
Condition100%3/3
exists $args->{$param} && defined $args->{$param}
!ll&&!rl&&r
36236437
MC/DC100%2/2
exists $args->{$param} && defined $args->{$param}
exists $args->{$param} defined $args->{$param}
Truth table100%3/3
A && B
A= exists $args->{$param}B= defined $args->{$param}
ABresult
0X0
100
111
3049 }
3050  
3051 # Called from code paths that path analysis shows can never run. If one ever
3052 # does, fail loudly rather than carry on with undefined behaviour.
3053 sub _unreachable
3054 2 ▶{
Subroutine _unreachable: called CC=2 SCAR=6.9
3055 2 my $logger = shift;
3056 2 my @call_details = caller(0);
3057 2 my $message = 'BUG: this code should not be reachable. Please report it to the maintainer';
3058  
3059 2 ▶ if($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 1 1 2
3060 1 $logger->error(__PACKAGE__, ' line ', $call_details[2], ": $message");
3061 }
3062 2 Carp::croak(__PACKAGE__, ' line ', $call_details[2], ": $message");
3063 }
3064  
3065 # Helper to log error or croak
3066 sub _error
3067 40074 ▶{
Subroutine _error: called CC=2 SCAR=6.9
3068 40074 my $logger = shift;
3069 40074 my $message = join('', @_);
3070 # Strip ASCII control characters to prevent log-injection / CRLF attacks
3071 # when user-supplied values appear in the message.
3072 40074 $message =~ s/[[:cntrl:]]/ /g;
3073  
3074 40074 my @call_details = caller(0);
3075 40074 ▶ if($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 59 40015 40074
3076 59 $logger->error(__PACKAGE__, ' line ', $call_details[2], ": $message");
3077 }
3078 40074 Carp::croak(__PACKAGE__, ' line ', $call_details[2], ": $message");
3079 }
3080  
3081 # Helper to log warning or carp
3082 sub _warn
3083 28 ▶{
Subroutine _warn: called CC=2 SCAR=6.9
3084 28 my $logger = shift;
3085 28 my $message = join('', @_);
3086 # Strip ASCII control characters to prevent log-injection / CRLF attacks.
3087 28 $message =~ s/[[:cntrl:]]/ /g;
3088  
3089 28 ▶ if($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) { } 7 21 28
3090 7 $logger->warn(__PACKAGE__, ": $message");
3091 } else {
3092 21 Carp::carp(__PACKAGE__, ": $message");
3093 }
3094 }
3095  
3096 =head1 AUTHOR
3097  
3098 Nigel Horne, C<< <njh at nigelhorne.com> >>
3099  
3100 =encoding utf-8
3101  
3102 =head1 FORMAL SPECIFICATION
3103  
3104 [PARAM_NAME, VALUE, TYPE_NAME, CONSTRAINT_VALUE]
3105  
3106 ValidationRule ::= SimpleType | ComplexRule | UnionType
3107  
3108 SimpleType ::= string | integer | number | float | boolean | scalar
3109 | scalarref | stringref | arrayref | hashref | coderef
3110 | object | void | regex | handle
3111 | arraylike | hashlike | codelike | invocant
3112  
3113 UnionType ::= seq SimpleType -- at least two members; written as type => ['a', 'b']
3114  
3115 ComplexRule == [
3116 type: SimpleType | UnionType;
3117 min: ℕ₁;
3118 max: ℕ₁;
3119 optional: 𝔹;
3120 matches: REGEX;
3121 regex: REGEX;
3122 nomatch: REGEX;
3123 memberof: seq VALUE;
3124 enum: seq VALUE;
3125 values: seq VALUE;
3126 notmemberof: seq VALUE;
3127 callback: FUNCTION;
3128 isa: TYPE_NAME;
3129 does: ROLE_NAME;
3130 can: METHOD_NAME | seq METHOD_NAME;
3131 classisa: TYPE_NAME;
3132 subclass: TYPE_NAME;
3133 classdoes: ROLE_NAME;
3134 classcan: METHOD_NAME | seq METHOD_NAME;
3135 element_isa: TYPE_NAME;
3136 needs: PARAM_NAME | seq PARAM_NAME;
3137 driver: TYPE_NAME;
3138 semantic: 'unix_timestamp' | 'identifier' | 'class_name' | 'filename';
3139 aliases: seq PARAM_NAME;
3140 slurp: 𝔹;
3141 position: β„•β‚€;
3142 default: VALUE;
3143 transform: FUNCTION;
3144 error_msg: STRING
3145 ]
3146  
3147 Schema == PARAM_NAME β‡Έ ValidationRule
3148  
3149 Arguments == PARAM_NAME β‡Έ VALUE
3150  
3151 ValidatedResult == PARAM_NAME β‡Έ VALUE
3152  
3153 βˆ€ rule: ComplexRule β€’
3154 rule.min ≀ rule.max ∧
3155 ¬((rule.memberof ∨ rule.enum ∨ rule.values) ∧ rule.min) ∧
3156 ¬((rule.memberof ∨ rule.enum ∨ rule.values) ∧ rule.max) ∧
3157 ¬(rule.notmemberof ∧ rule.min) ∧
3158 ¬(rule.notmemberof ∧ rule.max)
3159  
3160 βˆ€ schema: Schema; args: Arguments β€’
3161 dom(validate_strict(schema, args)) βŠ† dom(schema) βˆͺ dom(args)
3162  
3163 validate_strict: Schema Γ— Arguments β†’ ValidatedResult
3164  
3165 βˆ€ schema: Schema; args: Arguments β€’
3166 let result == validate_strict(schema, args) β€’
3167 (βˆ€ name: dom(schema) ∩ dom(args) β€’
3168 name ∈ dom(result) β‡’
3169 type_matches(result(name), schema(name))) ∧
3170 (βˆ€ name: dom(schema) β€’
3171 Β¬optional(schema(name)) β‡’ name ∈ dom(args))
3172  
3173 type_matches: VALUE Γ— ValidationRule β†’ 𝔹
3174  
3175 =head1 EXAMPLE
3176  
3177 use Params::Get;
3178 use Params::Validate::Strict;
3179  
3180 sub where_am_i
3181 {
3182 my $params = Params::Validate::Strict::validate_strict({
3183 args => Params::Get::get_params(undef, \@_),
3184 description => 'Print a string of latitude and longitude',
3185 error_msg => 'Latitude is a number between +/- 90, longitude is a number between +/- 180',
3186 members => {
3187 'latitude' => {
3188 type => 'number',
3189 min => -90,
3190 max => 90
3191 }, 'longitude' => {
3192 type => 'number',
3193 min => -180,
3194 max => 180
3195 }
3196 }
3197 });
3198  
3199 print 'You are at ', $params->{'latitude'}, ', ', $params->{'longitude'}, "\n";
3200 }
3201  
3202 where_am_i({ latitude => 3.14, longitude => -155 });
3203  
3204 =head1 BUGS
3205  
3206 Known limitations:
3207  
3208 =over 4
3209  
3210 =item * C<semantic =E<gt> 'filename'> checks for a single path component only.
3211 It does not reject characters that are illegal only on some platforms,
3212 such as C<:> or C<\> on Windows, and does not check that the file exists.
3213  
3214 =item * C<classisa>, C<subclass>, C<classdoes> and C<classcan> require the class to be loaded already;
3215 use C<driver> to load it first.
3216  
3217 =item * Grammars passed to C<bnf> are cached for the life of the process and never evicted.
3218  
3219 =back
3220  
3221 Please report any other bugs as described in L</SUPPORT>.
3222  
3223 =head1 SECURITY
3224  
3225 =head2 Taint mode
3226  
3227 This module does B<not> untaint its return values.
3228 When running under Perl's taint mode (C<-T>), any value that was derived from
3229 tainted external input (C<$ENV{}>, C<STDIN>, etc.) will remain tainted in the
3230 validated result, even if the module accepted it.
3231 Callers that require untainted values must perform their own regex capture after
3232 validation, for example:
3233  
3234 my $validated = validate_strict(%args);
3235 my ($safe_name) = ($validated->{name} =~ /\A([\w\s]+)\z/);
3236  
3237 =head2 User-supplied regex patterns
3238  
3239 The C<matches> rule accepts pre-compiled C<qr//> objects supplied by the caller.
3240 A pathologically constructed pattern (e.g. C<qr/(a+)+b/>) can cause catastrophic
3241 backtracking and peg a CPU core when matched against a hostile input value.
3242 Use possessive quantifiers (C<++>) or atomic groups (C<< (?>...) >>) in any
3243 C<matches> pattern that will be applied to untrusted data.
3244  
3245 =head2 Error message content
3246  
3247 Error and warning messages produced by this module may include the parameter
3248 value supplied by the caller.
3249 The module strips ASCII control characters (including CR and LF) from all
3250 messages before passing them to the logger or croaking, to prevent log-injection
3251 and HTTP response-splitting attacks.
3252 Callers should nevertheless apply their own output encoding before including any
3253 validated value in an HTTP response, HTML page, or structured log entry.
3254  
3255 =head1 SEE ALSO
3256  
3257 =over 4
3258  
3259 =item * L<Test Dashboard|https://nigelhorne.github.io/Params-Validate-Strict/coverage/>
3260  
3261 =item * L<Data::Processor>
3262  
3263 =item * L<Params::Get>
3264  
3265 =item * L<Params::Smart>
3266  
3267 This is where the ideas for C<aliases>, C<slurp> and C<compile_schema> came from.
3268  
3269 =item * L<Params::Util>
3270  
3271 This is where the ideas for C<regex>, C<handle>, C<arraylike>, C<hashlike>, C<codelike>, C<invocant> came from.
3272  
3273 =item * L<Params::SomeUtil>
3274  
3275 A maintained fork of L<Params::Util> 1.07 with bug fixes. The same type-predicate ideas apply.
3276  
3277 =item * L<Params::Validate>
3278  
3279 =item * L<Return::Set>
3280  
3281 =item * L<App::Test::Generator>
3282  
3283 =back
3284  
3285 =head1 SUPPORT
3286  
3287 This module is provided as-is without any warranty.
3288  
3289 Please report any bugs or feature requests to C<bug-params-validate-strict at rt.cpan.org>,
3290 or through the web interface at
3291 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Validate-Strict>.
3292 I will be notified, and then you'll
3293 automatically be notified of progress on your bug as I make changes.
3294  
3295 You can find documentation for this module with the perldoc command.
3296  
3297 perldoc Params::Validate::Strict
3298  
3299 You can also look for information at:
3300  
3301 =over 4
3302  
3303 =item * MetaCPAN
3304  
3305 L<https://metacpan.org/dist/Params-Validate-Strict>
3306  
3307 =item * RT: CPAN's request tracker
3308  
3309 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Params-Validate-Strict>
3310  
3311 =item * CPAN Testers' Matrix
3312  
3313 L<http://matrix.cpantesters.org/?dist=Params-Validate-Strict>
3314  
3315 =item * CPAN Testers Dependencies
3316  
3317 L<http://deps.cpantesters.org/?module=Params::Validate::Strict>
3318  
3319 =back
3320  
3321 =head1 LICENSE AND COPYRIGHT
3322  
3323 Copyright 2025-2026 Nigel Horne.
3324  
3325 This program is released under the following licence: GPL2.
3326 If you use it,
3327 please let me know.
3328  
3329 =cut
3330  
3331 36 1;
3332  
3333 __END__
↑ Summary blib/lib/Params/Validate/Strict/BNF.pm »