Strict.pm

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