lib/HTTP/Validate.pm

filter: Statementstmt 95.3% 638 / 669 Branchbran 76.0% 400 / 526 Conditioncond 68.8% 214 / 311 MC/DCmcdc 44.9% 84 / 187 Subroutinesub 100% 60 / 60 Podpod 43.2% 16 / 37 totaltotal 78.8% 1412 / 1790 SCAR 70.1
CC
388
Cov
83%
add_rules
127.8
validate_ruleset
114.9
process_doc
66.7
CRAP 1110.3
« blib/lib/HTTP/Validate.pm ↑ Summary Build »
1 package HTTP::Validate;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Scalar::Util qw( reftype weaken looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Check for the existence of the 'fc' function. If it exists, we can use it
11 # for casefolding enum values. Otherwise, we default to 'lc'.
12  
13 my $case_fold = $] >= 5.016 ? eval 'sub { return CORE::fc $_[0] }'
14 7 ▶ : $INC{'Unicode/CaseFold.pm'} ? eval 'sub { return Unicode:CaseFold::fc $_[0] }'
Branch: 2 branches
BranchTrueFalseTotal
$] >= 5.016 ? : 7 0 7
$INC{'Unicode/CaseFold.pm'} ? : 0 0 0
15 : eval 'sub { return lc $_[0] }';
16  
17 7 our $VERSION = '0.982';
18  
19 =head1 NAME
20  
21 HTTP::Validate - validate and clean HTTP parameter values according to a set of rules
22  
23 Version 0.982
24  
25 =head1 DESCRIPTION
26  
27 This module provides validation of HTTP request parameters against a set of
28 clearly defined rules. It is designed to work with L<Dancer>, L<Mojolicious>,
29 L<Catalyst>, and similar web application frameworks, both for interactive apps
30 and for data services. It can also be used with L<CGI>, although the use of
31 L<CGI::Fast> or another similar solution is recommended to avoid paying the
32 penalty of loading this module and initializing all of the rulesets over again
33 for each request. Both an object-oriented interface and a procedural
34 interface are provided.
35  
36 The rule definition mechanism is very flexible. A ruleset can be defined once
37 and used with multiple URL paths, and rulesets can be combined using the rule
38 types C<require> and C<allow>. This allows a complex application that accepts
39 many different paths to apply common rule patterns. If the parameters fail
40 the validation test, an error message is provided which tells the client how
41 to amend the request in order to make it valid. A suite of built-in validator
42 functions is available, and you can also define your own.
43  
44 This module also provides a mechanism for generating documentation about the
45 parameter rules. The documentation is generated in Pod format, which can
46 then be converted to HTML, TeX, nroff, etc. as needed.
47  
48 =head1 SYNOPSIS
49  
50 package MyWebApp;
51
52 use HTTP::Validate qw{:keywords :validators};
53
54 define_ruleset( 'filters' =>
55 { param => 'lat', valid => DECI_VALUE('-90.0','90.0') },
56 "Return all datasets associated with the given latitude.",
57 { param => 'lng', valid => DECI_VALUE('-180.0','180.0') },
58 "Return all datasets associated with the given longitude.",
59 { together => ['lat', 'lng'], errmsg => "you must specify 'lng' and 'lat' together" },
60 "If either 'lat' or 'lng' is given, the other must be as well.",
61 { param => 'id', valid => POS_VALUE },
62 "Return the dataset with the given identifier",
63 { param => 'name', valid => STR_VALUE },
64 "Return all datasets with the given name");
65
66 define_ruleset( 'display' =>
67 { optional => 'full', valid => FLAG_VALUE },
68 "If specified, then the full dataset descriptions are returned. No value is necessary",
69 { optional => 'short', valid => FLAG_VALUE },
70 "If specified, then a brief summary of the datasets is returned. No value is necessary",
71 { at_most_one => ['full', 'short'] },
72 { optional => 'limit', valid => [POS_ZERO_VALUE, ENUM('all')], default => 'all',
73 errmsg => "acceptable values for 'limit' are either 'all', 0, or a positive integer" },
74 "Limits the number of results returned. Acceptable values are 'all', 0, or a positive integer.");
75
76 define_ruleset( 'dataset_query' =>
77 "This URL queries for stored datasets. The following parameters select the datasets",
78 "to be displayed, and you must specify at least one of them:",
79 { require => 'filters',
80 errmsg => "you must specify at least one of the following: 'lat' and 'lng', 'id', 'name'" },
81 "The following optional parameters control how the data is returned:",
82 { allow => 'display' });
83
84 # Validate the parameters found in %ARGS against the ruleset
85 # 'dataset_query'. This is just one example, and in general the parameters
86 # may be found in various places depending upon which module (CGI,
87 # Dancer, Mojolicious, etc.) you are using to accept and process HTTP
88 # requests.
89
90 my $result = check_params('dataset_query', \%ARGS);
91
92 if ( my @error_list = $result->errors )
93 {
94 # if an error message was generated, do whatever is necessary to abort the
95 # request and report the error back to the end user
96 }
97
98 # Otherwise, $result->values will return the cleaned parameter
99 # values for use in processing the request.
100  
101 =head1 THE VALIDATION PROCESS
102  
103 The validation process starts with the definition of one or more sets of rules.
104 This is done via the L</define_ruleset> keyword. For example:
105  
106 define_ruleset 'some_params' =>
107 { param => 'id', valid => POS_VALUE };
108 { param => 'short', valid => FLAG_VALUE },
109 { param => 'full', valid => FLAG_VALUE },
110 { at_most_one => ['short', 'full'],
111 errmsg => "the parameters 'short' and 'full' cannot be used together" };
112  
113 This statement defines a ruleset named 'some_params' that enforces the following
114 rules:
115  
116 =over 4
117  
118 =item *
119  
120 The value of parameter 'id' must be a positive integer.
121  
122 =item *
123  
124 The parameter 'short' is considered to have a true value if it appears in a
125 request, and false otherwise. The value, if any, is ignored.
126  
127 =item *
128  
129 The parameter 'full' is treated likewise.
130  
131 =item *
132  
133 The parameters 'short' and 'full' must not be specified together in the same
134 request.
135  
136 =back
137  
138 You can define as many rulesets as you wish. For each URL path recognized by
139 your code, you can use the L</check_params> function to validate the request
140 parameters against the appropriate ruleset for that path. If the given
141 parameter values are not valid, one or more error messages will be returned.
142 These messages should be sent back to the HTTP client, in order to instruct
143 the user or programmer who originally generated the request how to amend the
144 parameters so that the request will succeed.
145  
146 During the validation process, a set of parameter values are considered to
147 "pass" against a given ruleset if they are consistent with all of its rules.
148 Rulesets may be included inside other rulesets by means of L</allow> and
149 L</require> rules. This allows you to define common rulesets to validate
150 various groups of parameters, and then combine them together into specific
151 rulesets for use with different URL paths.
152  
153 A ruleset is considered to be "fulfilled" by a request if at least one
154 parameter mentioned in a L</param> or L</mandatory> rule is included in that
155 request, or trivially if the ruleset does not contain any rules of those
156 types. When you use L</check_params> to validate a request against a
157 particular ruleset, the request will be rejected unless the following are both
158 true:
159  
160 =over 4
161  
162 =item *
163  
164 The request passes against the specified ruleset and all those that it
165 includes.
166  
167 =item *
168  
169 The specified ruleset is fulfilled, along with any other rulesets included by
170 L</require> rules. Rulesets included by L</allow> rules do not have to be
171 fulfilled.
172  
173 =back
174  
175 This provides you with a lot of flexibilty as to requiring or not requiring
176 various parameters. Note that a ruleset without any L</param> or
177 L</mandatory> rules is automatically fulfilled, which allows you to make all
178 of the paramters optional if you wish. You can augment this mechanism by
179 using L</together> and L</at_most_one> rules to specify which parameters must
180 or must not be used together.
181  
182 =head2 Ruleset names
183  
184 Each ruleset must have a unique name, which can be any non-empty
185 string. You may name them after paths, parameters, functionality ("display",
186 "filter") or whatever else makes sense to you.
187  
188 =head2 Ordering of rules
189  
190 The rules in a given ruleset are always checked in the order they were
191 defined. Rulesets that are included via L</allow> and L</require> rules are
192 checked immediately when the including rule is evaluated. Each ruleset is
193 checked at most once per validation, even if it is included multiple times.
194  
195 You should be cautious about including multiple parameter rules that
196 correspond to the same parameter name, as this can lead to situations where no
197 possible value is correct.
198  
199 =head2 Unrecognized parameters
200  
201 By default, a request will be rejected with an appropriate error message if it
202 contains any parameters not mentioned in any of the checked rulesets. This
203 can be overridden (see below) to generate warnings instead. However, please
204 think carefully before choosing this option. Allowing unrecognized parameters
205 opens up the possibility that optional parameters will be accidentally
206 misspelled and thus ignored, so that the results are mysteriously different
207 from what was expected. If you override this behavior, you should make sure that
208 any resulting warnings are explicitly displayed in the response that you
209 generate.
210  
211 =head2 Rule syntax
212  
213 Every rule is represented by a hashref that contains a key indicating the rule
214 type. For clarity, you should always write this key first. It is an error to
215 include more than one of these keys in a single rule. You may optionally
216 include additional keys to specify what are the acceptable values for this
217 parameter, what error message should be returned if the parameter value is not
218 acceptable, and L<many other options|/Other keys>.
219  
220 =head3 parameter rules
221  
222 The following three types of rules define the recognized parameter names.
223  
224 =head4 param
225  
226 { param => <parameter_name>, valid => <validator> ... }
227  
228 If the specified parameter is present with a non-empty value, then its value
229 must pass one of the specified validators. If it passes any of them, the rest
230 are ignored. If it does not pass any of them, then an appropriate error
231 message will be generated. If no validators are specified, then the value
232 will be accepted no matter what it is.
233  
234 If the specified parameter is present and its value is valid, then the
235 containing ruleset will be marked as "fulfilled". You could use this, for
236 example, with a query URL in order to require that the query not be empty
237 but instead contain at least one significant criterion. The parameters that
238 count as "significant" would be declared by C<param> rules, the others by
239 C<optional> rules.
240  
241 =head4 optional
242  
243 { optional => <parameter_name>, valid => <validator> ... }
244  
245 An C<optional> rule is identical to a C<param> rule, except that the presence
246 or absence of the parameter will have no effect on whether or not the
247 containing ruleset is fulfilled. A ruleset in which all of the parameter rules
248 are C<optional> will always be fulfilled. This kind of rule is useful in
249 validating URL parameters, especially for GET requests.
250  
251 =head4 mandatory
252  
253 { mandatory => <parameter_name>, valid => <validator> ... }
254  
255 A C<mandatory> rule is identical to a C<param> rule, except that this
256 parameter is required to be present with a non-empty value regardless of the
257 presence or absence of other parameters. If it is not, then an error message
258 will be generated. This kind of rule can be useful when validating HTML form
259 submissions, for use with fields such as "name" that must always be filled in.
260  
261 =head3 parameter constraint rules
262  
263 The following rule types can be used to specify additional constraints on the
264 presence or absence of parameter names.
265  
266 =head4 together
267  
268 { together => [ <parameter_name> ... ] }
269  
270 If one of the listed parameters is present, then all of them must be.
271 This can be used with parameters such as 'longitude' and 'latitude', where
272 neither one makes sense without the other.
273  
274 =head4 at_most_one
275  
276 { at_most_one => [ <parameter_name> ... ] }
277  
278 At most one of the listed parameters may be present. This can be used along
279 with a series of C<param> rules to require that exactly one of a particular
280 set of parameters is provided.
281  
282 =head4 ignore
283  
284 { ignore => [ <parameter_name> ... ] }
285  
286 The specified parameter or parameters will be ignored if present, and will not
287 be included in the set of reported parameter values. This rule can be used to
288 prevent requests from being rejected with "unrecognized parameter" errors in
289 cases where spurious parameters may be present. If you are specifying only one
290 parameter name, it does need not be in a listref.
291  
292 =head3 inclusion rules
293  
294 The following rule types can be used to include one ruleset inside of another.
295 This allows you, for example, to define rulesets for validating different
296 groups of parameters and then combine them into specific rulesets for use with
297 different URL paths.
298  
299 It is okay for an included ruleset to itself include other rulesets. A given
300 ruleset is checked at most once per validation no matter how many times it is
301 included.
302  
303 =head4 allow
304  
305 { allow => <ruleset_name> }
306  
307 A rule of this type is essentially an 'include' statement. If this rule is
308 encountered during a validation, it causes the named ruleset to be checked
309 immediately. The parameters must pass against this ruleset, but it does not
310 have to be fulfilled.
311  
312 =head4 require
313  
314 { require => <ruleset_name> }
315  
316 This is a variant of C<allow>, with an additional constraint. The validation
317 will fail unless the named ruleset not only passes but is also fulfilled by
318 the parameters. You could use this, for example, with a query URL in order to
319 require that the query not be empty but instead contain at least one
320 significant criterion. The parameters that count as "significant" would be
321 declared by L</param> rules, the others by L</optional> rules.
322  
323 =head3 inclusion constraint rules
324  
325 The following rule types can be used to specify additional constraints on the
326 inclusion of rulesets.
327  
328 =head4 require_one
329  
330 { require_one => [ <ruleset_name> ... ] }
331  
332 You can use a rule of this type to place an additional constraint on a list of
333 rulesets already included with L<inclusion rules|/inclusion rules>. Exactly
334 one of the named rulesets must be fulfilled, or else the request is rejected.
335 You can use this, for example, to ensure that a request includes either a
336 parameter from group A or one from group B, but not both.
337  
338 =head4 require_any
339  
340 { require_any => [ <ruleset_name> ... ] }
341  
342 This is a variant of C<require_one>. At least one of the named rulesets must be
343 fulfilled, or else the request will be rejected.
344  
345 =head4 allow_one
346  
347 { allow_one => [ <ruleset_name> ... ] }
348  
349 Another variant of C<require_one>. The request will be rejected if more than one
350 of the listed rulesets is fulfilled, but will pass if either none of them or
351 just one of them is fulfilled. This can be used to allow optional parameters
352 from either group A or group B, but not from both groups.
353  
354 =head3 other rules
355  
356 =head4 content_type
357  
358 { content_type => <parameter_name>, valid => [ <value> ... ] }
359  
360 You can use a rule of this type, if you wish, to direct that the value of the
361 specified parameter be used to indicate the content type of the response. Only one
362 of these rules should occur in any given validation. The key C<valid> gives a
363 list of acceptable values and the content types they should map to. For
364 example, if you are using this module with L<Dancer> then you could do
365 something like the following:
366  
367 define_ruleset '/some/path' =>
368 { require => 'some_params' },
369 { allow => 'other_params' },
370 { content_type => 'ct', valid => ['html', 'json', 'frob=application/frobnicate'] };
371
372 get '/some/path.:ct' => sub {
373
374 my $valid_request = check_params('/some/path', params);
375 content_type $valid_request->content_type;
376 ...
377 }
378  
379 This code specifies that the content type of the response will be set by the
380 URL path suffix, which may be either C<.html>, C<.json> or C<.frob>.
381  
382 If the value given in a request does not occur in the list, or if no value is
383 found, then an error message will be generated that lists the accepted types.
384  
385 To match an empty parameter value, include a string that looks like
386 '=some/type'. You need not specify the actual content type string for the
387 well-known types 'html', 'json', 'xml', 'txt' or 'csv', unless you wish to
388 override the default given by this module.
389  
390 =head2 Rule attributes
391  
392 Any rule definition may also include one or more of the following attributes,
393 specified as key/value pairs in the rule hash:
394  
395 =head3 errmsg
396  
397 This attribute specifies the error message to be returned if the rule fails,
398 overriding the default message. For example:
399  
400 define_ruleset( 'specifier' =>
401 { param => 'name', valid => STRING_VALUE },
402 { param => 'id', valid => POS_VALUE });
403
404 define_ruleset( 'my_route' =>
405 { require => 'specifier',
406 errmsg => "you must specify either of the parameters 'name' or 'id'" });
407  
408 Error messages may include any of the following placeholders: C<{param}>,
409 C<{value}>. These are replaced respectively by the relevant parameter name(s)
410 and original parameter value(s), single-quoted. This feature allows you to
411 define messages that quote the actual parameter values presented in the
412 request, as well as to define common messages and use them with multiple
413 rules.
414  
415 =head3 warn
416  
417 This attribute causes a warning to be generated rather than an error if the
418 rule fails. Unlike errors, warnings do not cause a request to be rejected.
419 At the end of the validation process, the list of generated warnings can be
420 retrieved by using the L</warnings> method of the result object.
421  
422 If the value of this key is 1, then what would otherwise be the error
423 message will be used as the warning message. Otherwise, the specified string
424 will be used as the warning message.
425  
426 For parameter rules, this attribute affects only errors resulting from
427 validation of the parameter values. Other error conditions (i.e. multiple
428 parameter values without the L</multiple> attribute) continue to be reported
429 as errors.
430  
431 =head3 key
432  
433 The attribute 'key' specifies the name under which any information generated by
434 the rule will be saved. For a parameter rule, the cleaned value will be saved
435 under this name. For all rules, any generated warnings or errors will be
436 stored under the specified name instead of the parameter name or rule number.
437 This allows you to easily determine after a validation which
438 warnings or errors were generated.
439  
440 The following keys can be used only with rules of type
441 L</param>, L</optional> or L</mandatory>:
442  
443 =head3 valid
444  
445 This attribute specifies the domain of acceptable values for the parameter. The
446 value must be either a single code reference or a list of them. You can
447 either select from the list of L<built-in validator functions|/VALIDATORS>
448 included with this module, or provide your own.
449  
450 If the parameter named by this rule is present, its value must pass at least
451 one of the specified validators or else an error message will be generated.
452 If multiple validators are given, then the error message returned will be the
453 one generated by the last validator in the list. This can be overridden by
454 using the L</errmsg> key.
455  
456 =head3 multiple
457  
458 This attribute specifies that the parameter may appear multiple times in the
459 request. Without this directive, multiple values for the same parameter will
460 generate an error. For example:
461  
462 define_ruleset( 'identifiers' =>
463 { param => 'id', valid => POS_VALUE, multiple => 1 });
464  
465 If this attribute is present with a true value, then the cleaned value of the
466 parameter will be an array ref if at least one valid value was found and
467 I<undef> otherwise. If you wish a request to be considered valid even if some
468 of the values fail the validator, then either use the L</list> attribute instead or
469 include a L</warn> key as well.
470  
471 =head3 split
472  
473 This attribute has the same effect as L</multiple>, and in addition causes
474 each parameter value string to be split (L<perlfunc/split>) as indicated by the
475 value of the directive. If this value is a string, then it will be compiled
476 into a regexp preceded and followed by C<\s*>. So in the
477 following example:
478  
479 define_ruleset( 'identifiers' =>
480 { param => 'id', valid => POS_VALUE, split => ',' });
481  
482 The value string will be considered to be valid if it contains one or more
483 positive integers separated by commas and optional whitespace. Empty strings
484 between separators are ignored.
485  
486 123,456 # returns [123, 456]
487 123 , ,456 # returns [123, 456]
488 , 456 # returns [456]
489 123 456 # not valid
490 123:456 # not valid
491  
492 If you wish more precise control over the separator expression, you can pass a
493 regexp quoted with L<qr> instead.
494  
495 =head3 list
496  
497 This attribute has the same effect as L</split>, but generates warnings
498 instead of error messages when invalid values are encountered (as if
499 C<< warn => 1 >> was also specified). The resulting cleaned value will be a
500 listref containing any values which pass the validator, or I<undef> if no
501 valid values were found. See also L</warn> and L</bad_value>.
502  
503 =head3 bad_value
504  
505 This attribute can be useful in conjunction with L</list>. If one or more
506 values are given for the parameter but none of them are valid, this attribute
507 comes into effect. If the value of this attribute is C<ERROR>, then the
508 validation will fail with an appropriate error message. Otherwise, this will
509 be used as the value of the parameter. It is recommended that you set the
510 value to something outside of the valid range, i.e. C<-1> for a C<POS_VALUE>
511 parameter.
512  
513 Using this attribute allows you to easily distinguish between the case when
514 the parameter appears with an empty value (or not at all, which is considered
515 equivalent) vs. when the parameter appears with one or more invalid values and
516 no good ones.
517  
518 =head3 alias
519  
520 This attribute specifies one or more aliases for the parameter name (use a
521 listref for multiple aliases). These names may be used interchangeably in
522 requests, but any request that contains more than one of them will be rejected
523 with an appropriate error message unless L</multiple> is also specified. The
524 parameter value and any error or warning messages will be reported under the
525 main parameter name for this rule, no matter which alias is used in the
526 request.
527  
528 =head3 clean
529  
530 This attribute specifies a subroutine which will be used to modify the
531 parameter values. This routine will be called with the raw value of the
532 parameter as its only argument, once for each value if multiple values are
533 allowed. The resulting values will be stored as the "cleaned" values. The
534 value of this directive may be either a code ref or one of the strings 'uc',
535 'lc' or 'fc'. These direct that the parameter values be converted to
536 uppercase, lowercase, or L<fold case|Unicode::CaseFold> respectively.
537  
538 =head3 default
539  
540 This attribute specifies a default value for the parameter, which will be
541 reported if the parameter is not present in the request or if it is present
542 with an empty value. If the rule also includes a validator and/or a cleaner,
543 the specified default value will be passed to it when the ruleset is defined.
544 An exception will be thrown if the default value does not pass the validator.
545  
546 =head3 undocumented
547  
548 If this attribute is given with a true value, then this rule will be ignored
549 by any calls to L</document_params>. This feature allows you to include
550 parameters that are recognized as valid but that are not included in any
551 generated documentation. Such parameters will be invisible to users, but
552 will be visible and clearly marked to anybody browsing your source code.
553  
554 =head2 Documentation
555  
556 A ruleset definition may include strings interspersed with the rule
557 definitions (see the L<example at the top of this page|/SYNOPSIS>) which can
558 be turned into documentation in Pod format by means of the L</document_params>
559 keyword. It is recommended that you use this function to auto-generate the
560 C<PARAMETERS> section of the documentation pages for the various URL paths
561 accepted by your web application, translating the output from Pod to whatever
562 format is appropriate. This will help you to keep the documentation and the
563 actual rules in synchrony with one another.
564  
565 The generated documentation will consist of one or more item lists, separated
566 by ordinary paragraphs. Each parameter rule will generate one item, whose body
567 consists of the documentation strings immediately following the rule
568 definition. Ordinary paragraphs (see below) can be used to separate the
569 parameters into groups for documentation purposes, or at the start or end of a
570 list as introductory or concluding material. Each L</require> or L</allow>
571 rule causes the documentation for the indicated ruleset(s) to be interpolated,
572 except as noted below. Note that this subsidiary documentation will not be
573 nested. All of the parameters will be documented at the same list indentation
574 level, whether or not they are defined in subsidiary rulesets.
575  
576 Documentation strings may start with one of the following special characters:
577  
578 =over 4
579  
580 =item C<<< >> >>>
581  
582 The remainder of this string, plus any strings immediately following, will
583 appear as an ordinary paragraph. You can use this feature to provide
584 commentary paragraphs separating the documented parameters into groups.
585 Any documentation strings occurring before the first parameter rule
586 definition, or following an C<allow> or C<require> rule, will always generate
587 ordinary paragraphs regardless of whether they start with this special
588 character.
589  
590 =item C<<< > >>>
591  
592 The remainder of this string, plus any strings immediately following, will
593 appear as a new paragraph of the same type as the preceding paragraph (item
594 body or ordinary paragraph).
595  
596 =item C<!>
597  
598 The preceding rule definition will be ignored by any calls to
599 L</document_params>, and all documentation for this rule will be suppressed.
600 This is equivalent to specifying the rule attribute L</undocumented>.
601  
602 =item C<^>
603  
604 Any documentation generated for the preceding rule definition will be
605 suppressed. The remainder of this string plus any strings immediately
606 following will appear as an ordinary paragraph in its place. You can use
607 this, for example, to document a subsidiary ruleset with an explanatory note
608 (i.e. a link to another documentation section or page) instead of explicitly
609 listing all of the included parameters.
610  
611 =item C<?>
612  
613 This character is ignored at the beginning of a documentation string, and the
614 next character loses any special meaning it might have had. You can use this
615 in the unlikely event that you want a documentation paragraph to actually
616 start with one of these special characters.
617  
618 =back
619  
620 Note that modifier rules such as C<at_most_one>, C<require_one>, etc. are
621 ignored when generating documentation. Any documentation strings following
622 them will be treated as if they apply to the most recently preceding parameter
623 rule or inclusion rule.
624  
625 =cut
626  
627 7 our (@EXPORT_OK, @VALIDATORS, %EXPORT_TAGS);
628  
629 BEGIN {
630  
631 7 ▶ @EXPORT_OK = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
632 define_ruleset check_params validation_settings ruleset_defined document_params
633 list_params
634 INT_VALUE POS_VALUE POS_ZERO_VALUE
635 DECI_VALUE
636 ENUM_VALUE
637 BOOLEAN_VALUE
638 MATCH_VALUE
639 FLAG_VALUE ANY_VALUE
640 );
641
642 7 @VALIDATORS = qw(INT_VALUE POS_VALUE POS_ZERO_VALUE DECI_VALUE
643 ENUM_VALUE MATCH_VALUE BOOLEAN_VALUE FLAG_VALUE ANY_VALUE);
644  
645 7 %EXPORT_TAGS = (
646 keywords => [qw(define_ruleset check_params validation_settings ruleset_defined document_params
647 list_params)],
648 validators => \@VALIDATORS,
649 );
650 };
651  
652 # The following defines a single global validator object, for use when this
653 # module is used in the non-object-oriented manner.
654  
655 7 our ($DEFAULT_INSTANCE) = bless { RULESETS => {}, SETTINGS => {} };
656  
657  
658 # Known media types are defined here
659  
660 7 my (%MEDIA_TYPE) =
661 ('html' => 'text/html',
662 'xml' => 'text/xml',
663 'txt' => 'text/plain',
664 'tsv' => 'text/tab-separated-values',
665 'csv' => 'text/csv',
666 'json' => 'application/json',
667 );
668  
669 # Default error messages
670  
671 7 my (%ERROR_MSG) =
672 ('ERR_INVALID' => "the value of parameter {param} is invalid (was {value})",
673 'ERR_BAD_VALUES' => "no valid values were specified for {param} (found {value})",
674 'ERR_MULT_NAMES' => "you may only include one of {param}",
675 'ERR_MULT_VALUES' => "you may only specify one value for {param}: found {value}",
676 'ERR_MANDATORY' => "you must specify a value for {param}",
677 'ERR_TOGETHER' => "you must specify {param} together or not at all",
678 'ERR_AT_MOST' => "you may not specify more than one of {param}",
679 'ERR_REQ_SINGLE' => "you must specify the parameter {param}",
680 'ERR_REQ_MULT' => "you must specify at least one of the parameters {param}",
681 'ERR_REQ_ONE' => "you may not include parameters from more than one of these groups: {param}",
682 'ERR_MEDIA_TYPE' => "you must specify a media type, from the following list: {value}",
683 'ERR_DEFAULT' => "parameter value error: {param}",
684 );
685  
686 =head1 INTERFACE
687  
688 This module can be used in either an object-oriented or a procedural manner.
689 To use the object-oriented interface, generate a new instance of
690 HTTP::Validate and use any of the routines listed below as methods:
691  
692 use HTTP::Validate qw(:validators);
693
694 my $validator = HTTP::Validate->new();
695
696 $validator->define_ruleset('my_params' =>
697 { param => 'foo', valid => INT_VALUE, default => '0' });
698
699 my $result = $validator->check_params('my_params', \%ARGS);
700  
701 Otherwise, you can export these routines to your module and call them
702 directly. In this case, a global ruleset namespace will be assumed:
703  
704 use HTTP::Validate qw(:keywords :validators);
705
706 define_ruleset('my_params' =>
707 { param => 'foo', valid => INT_VALUE, default => '0' });
708
709 my $validated = check_params('my_params', \%ARGS);
710  
711 Using C<:keywords> will import all of the keywords listed below, except
712 'new'. Using C<:validators> will import all of the L<validators|/VALIDATORS>
713 listed below.
714  
715 The following can be called either as subroutines or as method names,
716 depending upon which paradigm you prefer:
717  
718 =head3 new
719  
720 This can be called as a class method to generate a new validation instance
721 (see example above) with its own ruleset namespace. Any of the arguments that
722 can be passed to L</validation_settings> can also be passed to this routine.
723  
724 =cut
725  
726 sub new {
727  
728 9 ▶ my ($class, @settings) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
729
730 9 ▶ croak "You must call 'new' as a class method" unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 9 9
731
732 # Create a new object
733
734 9 my $self = bless { RULESETS => {}, SETTINGS => {} }, $class;
735
736 # Set the requested settings
737
738 9 $self->validation_settings(@settings);
739
740 # Return the new object
741
742 7 return $self;
743 }
744  
745  
746 =head3 define_ruleset
747  
748 This keyword defines a set of rules to be used for validating parameters. The
749 first argument is the ruleset's name, which must be unique within its
750 namespace. The rest of the parameters must be a list of rules (hashrefs) interspersed
751 with documentation strings. For examples, see above.
752  
753 =cut
754  
755 sub define_ruleset {
756
757 # If we were called as a method, use the object on which we were called.
758 # Otherwise, use the default instance.
759
760 77 ▶ my $self = ref $_[0] eq 'HTTP::Validate' ? shift : $DEFAULT_INSTANCE;
Subroutine define_ruleset: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTTP::Validate' ? : 14 63 77
761  
762 77 my ($ruleset_name, @rules) = @_;
763
764 # Next make sure we know where this is called from, for the purpose of
765 # generating useful error messages.
766
767 77 my ($package, $filename, $line) = caller;
768
769 # Check the arguments, then create a new ruleset object.
770
771 77 ▶ croak "The first argument to 'define_ruleset' must be a non-empty string"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ruleset_name and not ref $ruleset_name and $ruleset_name ne "" 2 74 76
Condition100%6/6
defined $ruleset_name and not ref $ruleset_name and $ruleset_name ne ""
!ll&&!rl&&r
2174
defined $ruleset_name and not ref $ruleset_name
!ll&&!rl&&r
1175
MC/DC100%3/3
defined $ruleset_name and not ref $ruleset_name and $ruleset_name ne ""
defined $ruleset_name not ref $ruleset_name $ruleset_name ne ""
Truth table100%4/4
A and not(B) and C
A= defined $ruleset_nameB= not ref $ruleset_nameC= $ruleset_name ne ""
ABCresult
0XX0
10X0
1100
1111
772 unless defined $ruleset_name && !ref $ruleset_name && $ruleset_name ne '';
773
774 74 my $rs = $self->create_ruleset($ruleset_name, $filename, $line);
775
776 # Then add the rules.
777
778 72 $self->add_rules($rs, @rules);
779
780 # If we get here without any errors, install the ruleset and return.
781
782 61 $self->{RULESETS}{$ruleset_name} = $rs;
783 61 return 1;
784 };
785  
786  
787 =head3 check_params
788  
789 my $result = check_params('my_ruleset', undef, params('query'));
790
791 if ( $result->passed )
792 {
793 # process the request using the keys and values returned by
794 # $result->values
795 }
796
797 else
798 {
799 # redisplay the form, send an error response, or otherwise handle the
800 # error condition using the error messages returned by $result->errors
801 }
802  
803 This function validates a set of parameters and values (which may be provided
804 either as one or more hashrefs or as a flattened list of keys and values or a
805 combination of the two) against the named ruleset with the specified context. It
806 returns a response object from which you can get the cleaned parameter values
807 along with any errors or warnings that may have been generated.
808  
809 The second parameter must be either a hashref or undefined. If it is defined,
810 it is passed to each of the validator functions as "context". This allows you
811 to provide attributes such as a database handle to the validator functions.
812 The third parameter must be either a hashref or a listref containing parameter
813 names and values. If it is a listref, any items at the beginning of the list
814 which are themselves hashrefs will be expanded before the list is processed
815 (this allows you, for example, to pass in a hashref plus some additional names
816 and values without having to modify the hashref in place).
817  
818 You can use the L</passed> method on the returned object to determine if the
819 validation passed or failed. In the latter case, you can return an HTTP error
820 response to the user, or perhaps redisplay a submitted form.
821  
822 Note that you can validate against multiple rulesets at once by defining a new
823 ruleset with inclusion rules referring to all of the rulesets
824 you wish to validate against.
825  
826 =cut
827  
828 sub check_params {
829
830 # If we were called as a method, use the object on which we were called.
831 # Otherwise, use the globally defined one.
832
833 60 ▶ my $self = ref $_[0] eq 'HTTP::Validate' ? shift : $DEFAULT_INSTANCE;
Subroutine check_params: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTTP::Validate' ? : 18 42 60
834
835 60 my ($ruleset_name, $context, $parameters) = @_;
836
837 # Create a new validation-execution object using the specified context
838 # and parameters.
839
840 60 my $vr = $self->new_execution($context, $parameters);
841
842 # Now execute that validation using the specified ruleset, and return the
843 # result.
844
845 60 return $self->execute_validation($vr, $ruleset_name);
846 };
847  
848  
849 =head3 validation_settings
850  
851 This function allows you to change the settings on the validation routine.
852 For example:
853  
854 validation_settings( allow_unrecognized => 1 );
855  
856 If you are using this module in an object-oriented way, then you can also pass
857 any of these settings as parameters to the constructor method. Available
858 settings include:
859  
860 =over 4
861  
862 =item allow_unrecognized
863  
864 If specified, then unrecognized parameters will generate warnings instead of errors.
865  
866 =item ignore_unrecognized
867  
868 If specified, then unrecognized parameters will be ignored entirely.
869  
870 =back
871  
872 You may also specify one or more of the following keys, each followed by a string. These
873 allow you to redefine the messages that are generated when parameter errors are detected:
874  
875 ERR_INVALID, ERR_BAD_VALUES, ERR_MULT_NAMES, ERR_MULT_VALUES, ERR_MANDATORY, ERR_TOGETHER,
876 ERR_AT_MOST, ERR_REQ_SINGLE, ERR_REQ_MULT, ERR_REQ_ONE, ERR_MEDIA_TYPE, ERR_DEFAULT
877  
878 For example:
879  
880 validation_settings( ERR_MANDATORY => 'Missing mandatory parameter {param}',
881 ERR_REQ_SINGLE => 'Found {value} for {param}: only one value is allowed' );
882  
883 =cut
884  
885 sub validation_settings {
886
887 # If we were called as a method, use the object on which we were called.
888 # Otherwise, use the globally defined one.
889
890 15 ▶ my $self = ref $_[0] eq 'HTTP::Validate' ? shift : $DEFAULT_INSTANCE;
Subroutine validation_settings: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTTP::Validate' ? : 12 3 15
891
892 15 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 34 12 46
893 {
894 34 my $key = shift;
895 34 my $value = shift;
896
897 34 ▶ if ( $key eq 'allow_unrecognized' )
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'allow_unrecognized') { } 5 29 34
elsif ($key eq 'ignore_unrecognized') { } 2 27 29
elsif ($ERROR_MSG{$key}) { } 24 3 27
898 {
899 5 ▶ $self->{SETTINGS}{permissive} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 5 0 5
900 }
901
902 elsif ( $key eq 'ignore_unrecognized' )
903 {
904 2 ▶ $self->{SETTINGS}{ignore_unrecognized} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 0 2
905 }
906
907 elsif ( $ERROR_MSG{$key} )
908 {
909 24 $self->{SETTINGS}{$key} = $value;
910 }
911
912 else
913 {
914 3 croak "unrecognized setting: '$key'";
915 }
916 }
917
918 12 return 1;
919 }
920  
921  
922 =head3 ruleset_defined
923  
924 if ( ruleset_defined($ruleset_name) ) {
925 # then do something
926 }
927  
928 This function returns true if a ruleset has been defined with the given name,
929 false otherwise.
930  
931 =cut
932  
933 sub ruleset_defined {
934  
935 # If we were called as a method, use the object on which we were called.
936 # Otherwise, use the globally defined one.
937
938 2 ▶ my $self = ref $_[0] eq 'HTTP::Validate' ? shift : $DEFAULT_INSTANCE;
Subroutine ruleset_defined: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTTP::Validate' ? : 0 2 2
939
940 2 my ($ruleset_name) = @_;
941
942 # Return the requested result
943
944 2 return defined $self->{RULESETS}{$ruleset_name};
945 }
946  
947  
948 =head3 document_params
949  
950 This function generates L<documentation|/Documentation> for the given
951 ruleset, in L<Pod|perlpod> format. This only works if you have included
952 documentation strings in your calls to L</define_ruleset>. The method returns
953 I<undef> if the specified ruleset is not found.
954  
955 $my_doc = document_params($ruleset_name);
956  
957 This capability has been included in order to simplify the process of
958 documenting web services implemented using this module. The author has
959 noticed that documentation is much easier to maintain and more likely to be
960 kept up-to-date if the documentation strings are located right next to the
961 relevant definitions.
962  
963 Any parameter rules that you wish to leave undocumented should either be given
964 the attribute 'undocumented' or be immediately followed by a string starting
965 with "!". All others will automatically generate list items in the resulting
966 documentation, even if no documentation string is provided (in this case, the
967 item body will be empty).
968  
969 =cut
970  
971 sub document_params {
972  
973 # If we were called as a method, use the object on which we were called.
974 # Otherwise, use the globally defined instance.
975
976 4 ▶ my $self = ref $_[0] eq 'HTTP::Validate' ? shift : $DEFAULT_INSTANCE;
Subroutine document_params: called CC=4 SCAR=14.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTTP::Validate' ? : 0 4 4
977
978 4 my ($ruleset_name) = @_;
979
980 # Make sure we have a valid ruleset, or else return false.
981
982 4 ▶ return unless defined $ruleset_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ruleset_name 0 4 4
983
984 4 my $rs = $self->{RULESETS}{$ruleset_name};
985 4 ▶ return unless $rs;
Branch: 1 branch
BranchTrueFalseTotal
unless $rs 0 4 4
986
987 # Now generate the requested documentation.
988
989 4 return $self->generate_docstring($rs, { in_list => 0, level => 0, processed => {} });
990 }
991  
992  
993 =head3 list_params
994  
995 This function returns a list of the names of all parameters accepted by the
996 specified ruleset, including those accepted by included rulesets.
997  
998 my @parameter_names = list_ruleset_params($ruleset_name);
999  
1000 This may be useful if your validations allow unrecognized parameters, as it
1001 enables you to determine which of the parameters in a given request are
1002 significant to that request.
1003  
1004 =cut
1005  
1006 sub list_params {
1007  
1008 # If we were called as a method, use the object on which we were called.
1009 # Otherwise, use the globally defined instance.
1010
1011 1 ▶ my $self = ref $_[0] eq 'HTTP::Validate' ? shift : $DEFAULT_INSTANCE;
Subroutine list_params: called CC=4 SCAR=14.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HTTP::Validate' ? : 0 1 1
1012
1013 1 my ($ruleset_name) = @_;
1014
1015 # Make sure we have a valid ruleset, or else return false.
1016
1017 1 ▶ return unless defined $ruleset_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ruleset_name 0 1 1
1018
1019 1 my $rs = $self->{RULESETS}{$ruleset_name};
1020 1 ▶ return unless $rs;
Branch: 1 branch
BranchTrueFalseTotal
unless $rs 0 1 1
1021
1022 # Now generate the requested list.
1023
1024 1 return $self->generate_param_list($ruleset_name);
1025 }
1026  
1027  
1028 # Here are the implementing functions:
1029 # ====================================
1030  
1031 # create_ruleset ( ruleset_name, filename, line )
1032 #
1033 # Create a new ruleset with the given name, noting that it was defined in the
1034 # given filename at the given line number.
1035  
1036 sub create_ruleset {
1037  
1038 74 ▶ my ($validator, $ruleset_name, $filename, $line_no) = @_;
Subroutine create_ruleset: called CC=3 SCAR=11.0
Pod undocumented
1039
1040 # Make sure that a non-empty name was given, and that no ruleset has
1041 # already been defined under that name.
1042
1043 74 ▶ croak "you must provide a non-empty name for the ruleset" if $ruleset_name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ruleset_name eq "" 0 74 74
1044
1045 74 ▶ if ( exists $validator->{RULESETS}{$ruleset_name} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $validator->{"RULESETS"}{$ruleset_name}) 2 72 74
1046 {
1047 2 my $filename = $validator->{RULESETS}{$ruleset_name}{filename};
1048 2 my $line_no = $validator->{RULESETS}{$ruleset_name}{line_no};
1049 2 croak "ruleset '$ruleset_name' was already defined at line $line_no of $filename\n";
1050 }
1051
1052 # Create the new ruleset.
1053
1054 72 my $rs = { name => $ruleset_name,
1055 filename => $filename,
1056 line_no => $line_no,
1057 doc_items => [],
1058 fulfill_order => [],
1059 params => {},
1060 includes => {},
1061 rules => [] };
1062
1063 72 return bless $rs, 'HTTP::Validate::Ruleset';
1064 }
1065  
1066  
1067 # List all of the keys that are allowed in rule specifications. Those whose
1068 # value is 2 indicate the rule type, and at most one of these may be included
1069 # per rule. The others are optional.
1070  
1071 7 my %DIRECTIVE = ( 'param' => 2, 'optional' => 2, 'mandatory' => 2,
1072 'together' => 2, 'at_most_one' => 2, 'ignore' => 2,
1073 'require' => 2, 'allow' => 2, 'require_one' => 2,
1074 'require_any' => 2, 'allow_one' => 2, 'content_type' => 2,
1075 'valid' => 1, 'clean' => 1,
1076 'multiple' => 1, 'split' => 1, 'list' => 1, 'bad_value' => 1,
1077 'error' => 1, 'errmsg' => 1, 'warn' => 1, 'undocumented' => 1,
1078 'alias' => 1, 'key' => 1, 'default' => 1);
1079  
1080 # Categorize the rule types
1081  
1082 7 my %CATEGORY = ( 'param' => 'param',
1083 'optional' => 'param',
1084 'mandatory' => 'param',
1085 'together' => 'modifier',
1086 'at_most_one' => 'modifier',
1087 'ignore' => 'modifier',
1088 'require' => 'include',
1089 'allow' => 'include',
1090 'require_one' => 'constraint',
1091 'allow_one' => 'constraint',
1092 'require_any' => 'constraint',
1093 'content_type' => 'content' );
1094  
1095 # List the special validators.
1096  
1097 7 my (%VALIDATOR_DEF) = ( 'FLAG_VALUE' => 1, 'ANY_VALUE' => 1 );
1098  
1099 7 my (%CLEANER_DEF) = ( 'uc' => eval 'sub { return uc $_[0] }',
1100 'lc' => eval 'sub { return lc $_[0] }',
1101 'fc' => $case_fold );
1102  
1103 # add_rules ( ruleset, rule ... )
1104 #
1105 # Add rules to the specified ruleset. The rules may be optionally
1106 # interspersed with documentation strings.
1107  
1108 sub add_rules {
1109
1110 72 ▶ my ($self) = shift;
Subroutine add_rules: called CC=98 SCAR=48.5
Pod undocumented
1111 72 my ($rs) = shift;
1112
1113 72 my @doc_lines; # collect up documentation strings until we know how to apply them
1114 my $doc_rule; # the rule to which all new documentation strings should be added
1115
1116 # Go through the items in @_, one by one.
1117
1118 RULE:
1119 72 foreach my $rule (@_)
1120 {
1121 # If the item is a scalar, then it is a documentation string.
1122
1123 200 ▶ unless ( ref $rule )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rule) { } 184 16 200
(do { builtin::reftype($rule) ne "HASH" }) 0 184 184
1124 {
1125 # If the string starts with >, !, ^, or ? then treat it specially.
1126
1127 16 ▶ if ( $rule =~ qr{ ^ ([!^?] | >>?) (.*) }xs )
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ qr/ ^ ([!^?] | >>?) (.*) /sx) { } 5 11 16
1128 {
1129 # If >>, then close the active documentation section (if any)
1130 # and start a new one that is not tied to any rule. This will
1131 # generate an ordinary paragraph starting with the remainder
1132 # of the line.
1133
1134 5 ▶ if ( $1 eq '>>' )
Branch: 4 branches
BranchTrueFalseTotal
if ($1 eq '>>') { } 1 4 5
elsif ($1 eq '>') { } 1 3 4
elsif ($1 eq '!') { } 1 2 3
elsif ($1 eq '^') { } 1 1 2
1135 {
1136 1 ▶ $self->add_doc($rs, $doc_rule, @doc_lines) if $doc_rule || @doc_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $doc_rule or @doc_lines 1 0 1
Condition33%1/3
$doc_rule or @doc_lines
l!l&&r!l&&!r
100
MC/DC0%0/2
$doc_rule or @doc_lines
$doc_rule @doc_lines
Truth table33%1/3
A or B
A= $doc_ruleB= @doc_lines
ABresult
1X1
011
000
1137 1 @doc_lines = $2;
1138 1 $doc_rule = undef;
1139 }
1140
1141 # If >, then add to the current documentation a blank line
1142 # (which will cause a new paragraph) followed by the remainder
1143 # of this line.
1144
1145 elsif ( $1 eq '>' )
1146 {
1147 1 push @doc_lines, "", $2;
1148 }
1149
1150 # If !, then discard the contents of the current documentation
1151 # section and replace them with this line (including the !
1152 # character). This will cause add_doc to later discard them.
1153
1154 elsif ( $1 eq '!' )
1155 {
1156 1 @doc_lines = $rule;
1157 }
1158
1159 # If ^, then discard the contents of the current documentation
1160 # section and replace them with the remainder of the line.
1161 # Set $doc_rule to undef, which will cause the rule currently
1162 # being documented to be forgotten and the documentation to be
1163 # added as an ordinary paragraph instead.
1164
1165 elsif ( $1 eq '^' )
1166 {
1167 1 @doc_lines = $2;
1168 1 $doc_rule = undef;
1169 }
1170
1171 # If ?, then add the remainder of the line to the current
1172 # documentation section. This will prevent the next character
1173 # from being interpreted specially.
1174
1175 else
1176 {
1177 1 push @doc_lines, $2;
1178 }
1179 }
1180
1181 # Otherwise, just add this string to the current documentation section.
1182
1183 else
1184 {
1185 11 push @doc_lines, $rule;
1186 }
1187
1188 16 next RULE;
1189 }
1190
1191 # All other items must be hashrefs, otherwise throw an exception.
1192
1193 0 elsif ( reftype $rule ne 'HASH' )
1194 {
1195 0 croak "The arguments to 'define_ruleset' must all be hashrefs and/or strings";
1196 }
1197
1198 # If we get here, assume the item represents a rule and create a new record to
1199 # represent it.
1200
1201 184 my $rr = { rs => $rs, rn => scalar(@{$rs->{rules}}) + 1 };
1202 184 push @{$rs->{rules}}, $rr;
1203
1204 184 weaken($rr->{rs});
1205
1206 # Check all of the keys in the rule definition, making sure that all
1207 # are valid, and determine the rule type.
1208
1209 184 my $type;
1210
1211 KEY:
1212 184 foreach my $key (keys %$rule)
1213 {
1214 324 ▶ croak "unknown attribute '$key' found in rule" unless $DIRECTIVE{$key} || $ERROR_MSG{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $DIRECTIVE{$key} or $ERROR_MSG{$key} 2 322 324
Condition100%3/3
$DIRECTIVE{$key} or $ERROR_MSG{$key}
l!l&&r!l&&!r
31842
MC/DC100%2/2
$DIRECTIVE{$key} or $ERROR_MSG{$key}
$DIRECTIVE{$key} $ERROR_MSG{$key}
Truth table100%3/3
A or B
A= $DIRECTIVE{$key}B= $ERROR_MSG{$key}
ABresult
1X1
011
000
1215
1216 322 ▶ if ( defined $DIRECTIVE{$key} && $DIRECTIVE{$key} == 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DIRECTIVE{$key} and $DIRECTIVE{$key} == 2) 183 139 322
Condition100%3/3
defined $DIRECTIVE{$key} and $DIRECTIVE{$key} == 2
!ll&&!rl&&r
4135183
MC/DC100%2/2
defined $DIRECTIVE{$key} and $DIRECTIVE{$key} == 2
defined $DIRECTIVE{$key} $DIRECTIVE{$key} == 2
Truth table100%3/3
A and B
A= defined $DIRECTIVE{$key}B= $DIRECTIVE{$key} == 2
ABresult
0X0
100
111
1217 {
1218 183 ▶ croak "a rule definition cannot contain the attributes '$key' and '$type' together, because they indicate different rule types"
Branch: 1 branch
BranchTrueFalseTotal
if $type 1 182 183
1219 if $type;
1220 182 $type = $key;
1221 182 $rr->{$type} = $rule->{$type};
1222 182 next KEY;
1223 }
1224 }
1225
1226 # Then process the other keys.
1227
1228 181 foreach my $key (keys %$rule)
1229 {
1230 320 my $value = $rule->{$key};
1231
1232 320 ▶ if ( $key eq 'valid' )
Branch: 7 branches
BranchTrueFalseTotal
if ($key eq 'valid') { } 95 225 320
elsif ($key eq 'alias') { } 3 222 225
elsif ($key eq 'clean') { } 4 218 222
elsif ($key eq 'default') { } 3 215 218
elsif ($key eq 'split' or $key eq 'list') { } 8 207 215
elsif ($key eq 'error' or $key eq 'errmsg') { } 7 200 207
elsif ($key ne $type) { } 19 181 200
1233 {
1234 croak "the attribute 'valid' is only allowed with parameter rules"
1235 95 ▶ unless $CATEGORY{$type} eq 'param' || $type eq 'content_type';
Branch: 1 branch
BranchTrueFalseTotal
unless $CATEGORY{$type} eq "param" or $type eq "content_type" 0 95 95
Condition66%2/3
$CATEGORY{$type} eq "param" or $type eq "content_type"
l!l&&r!l&&!r
9140
MC/DC0%0/2
$CATEGORY{$type} eq "param" or $type eq "content_type"
$CATEGORY{$type} eq "param" $type eq "content_type"
Truth table66%2/3
A or B
A= $CATEGORY{$type} eq "param"B= $type eq "content_type"
ABresult
1X1
011
000
1236 }
1237
1238 elsif ( $key eq 'alias' )
1239 {
1240 croak "the attribute 'alias' is only allowed with parameter rules"
1241 3 ▶ unless $CATEGORY{$type} eq 'param';
Branch: 1 branch
BranchTrueFalseTotal
unless $CATEGORY{$type} eq "param" 0 3 3
1242
1243 3 ▶ croak "the value of 'alias' must be a string or a list ref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $value ne "ARRAY" 0 3 3
Condition66%2/3
ref $value and ref $value ne "ARRAY"
!ll&&!rl&&r
210
MC/DC0%0/2
ref $value and ref $value ne "ARRAY"
ref $value ref $value ne "ARRAY"
Truth table66%2/3
A and B
A= ref $valueB= ref $value ne "ARRAY"
ABresult
0X0
100
111
1244 if ref $value and ref $value ne 'ARRAY';
1245
1246 3 ▶ $rr->{alias} = ref $value ? $value : [ $value ];
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 2 3
1247 }
1248
1249 elsif ( $key eq 'clean' )
1250 {
1251 croak "they attribute 'clean' is only allowed with parameter rules"
1252 4 ▶ unless $CATEGORY{$type} eq 'param';
Branch: 1 branch
BranchTrueFalseTotal
unless $CATEGORY{$type} eq "param" 0 4 4
1253
1254 4 ▶ $rr->{cleaner} = $CLEANER_DEF{$value} || $value;
Condition66%2/3
$CLEANER_DEF{$value} || $value
l!l&&r!l&&!r
310
MC/DC0%0/2
$CLEANER_DEF{$value} || $value
$CLEANER_DEF{$value} $value
Truth table66%2/3
A || B
A= $CLEANER_DEF{$value}B= $value
ABresult
1X1
011
000
1255
1256 croak "invalid value '$value' for 'clean'"
1257 4 ▶ unless ref $rr->{cleaner} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rr->{"cleaner"} eq "CODE" 0 4 4
1258 }
1259
1260 elsif ( $key eq 'default' )
1261 {
1262 croak "the attribute 'default' is only allowed with parameter rules"
1263 3 ▶ unless $CATEGORY{$type} eq 'param';
Branch: 1 branch
BranchTrueFalseTotal
unless $CATEGORY{$type} eq "param" 0 3 3
1264
1265 3 $rr->{default} = $value;
1266 }
1267
1268 ▶ elsif ( $key eq 'split' || $key eq 'list' )
Condition100%3/3
$key eq "split" or $key eq "list"
l!l&&r!l&&!r
35207
MC/DC100%2/2
$key eq "split" or $key eq "list"
$key eq "split" $key eq "list"
Truth table100%3/3
A or B
A= $key eq "split"B= $key eq "list"
ABresult
1X1
011
000
1269 {
1270 croak "the attribute '$key' is only allowed with parameter rules"
1271 8 ▶ unless $CATEGORY{$type} eq 'param';
Branch: 1 branch
BranchTrueFalseTotal
unless $CATEGORY{$type} eq "param" 0 8 8
1272
1273 8 ▶ croak "the value of '$key' must be a string or a regexp"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $value ne "Regexp" 0 8 8
Condition66%2/3
ref $value and ref $value ne "Regexp"
!ll&&!rl&&r
710
MC/DC0%0/2
ref $value and ref $value ne "Regexp"
ref $value ref $value ne "Regexp"
Truth table66%2/3
A and B
A= ref $valueB= ref $value ne "Regexp"
ABresult
0X0
100
111
1274 if ref $value and ref $value ne 'Regexp';
1275
1276 8 $rr->{multiple} = 1;
1277
1278 # Make sure that we have a proper regular expression. If 'split'
1279 # was given with a string, surround it by \s* to ignore
1280 # whitespace.
1281
1282 8 ▶ unless ( ref $value )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 7 1 8
1283 {
1284 7 $value = qr{ \s* $value \s* }oxs;
1285 }
1286
1287 8 $rr->{split} = $value;
1288 8 ▶ $rr->{warn} = 1 if $key eq 'list';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "list" 5 3 8
1289 }
1290
1291 ▶ elsif ( $key eq 'error' || $key eq 'errmsg' )
Condition66%2/3
$key eq "error" or $key eq "errmsg"
l!l&&r!l&&!r
07200
MC/DC50%1/2
$key eq "error" or $key eq "errmsg"
$key eq "error" $key eq "errmsg"
Truth table66%2/3
A or B
A= $key eq "error"B= $key eq "errmsg"
ABresult
1X1
011
000
1292 {
1293 7 $rr->{errmsg} = $value;
1294 }
1295
1296 elsif ( $key ne $type )
1297 {
1298 19 ▶ croak "the value of '$key' must be a string" if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 19 19
1299
1300 19 $rr->{$key} = $value;
1301 }
1302 }
1303
1304 181 ▶ croak "each record must include a key that specifies the rule type, e.g. 'param' or 'allow'"
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 181 181
1305 unless $type;
1306
1307 # If we have any documentation strings collected up, then they belong to the previous
1308 # rule. If the current rule is a parameter rule, then add the collected documentation to
1309 # the previous rule and set this new rule as the target for subsequent documentation.
1310
1311 181 ▶ if ( $CATEGORY{$type} ne 'modifier' )
Branch: 1 branch
BranchTrueFalseTotal
if ($CATEGORY{$type} ne "modifier") 176 5 181
1312 {
1313 176 $self->add_doc($rs, $doc_rule, @doc_lines);
1314 176 $doc_rule = $rr;
1315 176 @doc_lines = ();
1316 }
1317
1318 # If the previous rule is an 'include' or 'constraint' rule, then any subsequent
1319 # documentation should become an ordinary paragraph; so set $doc_rule to undefined. If
1320 # the previous rule is a 'modifier' rule, and if $doc_rule is not empty, then its
1321 # documentation should be added to that previously encountered parameter rule.
1322
1323 # elsif ( $CATEGORY{$type} ne 'modifier' )
1324 # {
1325 # $self->add_doc($rs, $doc_rule);
1326 # $self->add_doc($rs, undef, @doc_lines);
1327 # $doc_rule = undef;
1328 # @doc_lines = ();
1329 # }
1330
1331 # Now process the rule according to its type.
1332
1333 181 my $typevalue = $rule->{$type};
1334
1335 181 ▶ if ( $CATEGORY{$type} eq 'param' )
Branch: 5 branches
BranchTrueFalseTotal
if ($CATEGORY{$type} eq 'param') { } 131 50 181
elsif ($CATEGORY{$type} eq 'modifier') { } 5 45 50
elsif ($CATEGORY{$type} eq 'include') { } 31 14 45
elsif ($CATEGORY{$type} eq 'constraint') { } 10 4 14
elsif ($type eq 'content_type') { } 4 0 4
1336 {
1337 131 $rr->{type} = 'param';
1338 131 $rr->{param} = $typevalue;
1339
1340 # Do some basic sanity checking.
1341
1342 131 ▶ croak "the value of '$type' must be a parameter name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $typevalue and not ref $typevalue and $typevalue ne "" 1 130 131
Condition66%4/6
defined $typevalue and not ref $typevalue and $typevalue ne ""
!ll&&!rl&&r
10130
defined $typevalue and not ref $typevalue
!ll&&!rl&&r
01130
MC/DC33%1/3
defined $typevalue and not ref $typevalue and $typevalue ne ""
defined $typevalue not ref $typevalue $typevalue ne ""
Truth table50%2/4
A and not(B) and C
A= defined $typevalueB= not ref $typevalueC= $typevalue ne ""
ABCresult
0XX0
10X0
1100
1111
1343 unless defined $typevalue && !ref $typevalue && $typevalue ne '';
1344
1345 # Check the validators.
1346
1347 130 ▶ my @validators = ref $rule->{valid} eq 'ARRAY' ? @{$rule->{valid}} : $rule->{valid};
Branch: 1 branch
BranchTrueFalseTotal
ref $rule->{'valid'} eq 'ARRAY' ? : 3 127 130
1348
1349 130 foreach my $v (@validators)
1350 {
1351 132 ▶ if ( defined $v && $VALIDATOR_DEF{$v} )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $v and $VALIDATOR_DEF{$v}) { } 6 126 132
elsif (defined $v) { } 87 39 126
Condition100%3/3
defined $v and $VALIDATOR_DEF{$v}
!ll&&!rl&&r
39876
MC/DC100%2/2
defined $v and $VALIDATOR_DEF{$v}
defined $v $VALIDATOR_DEF{$v}
Truth table100%3/3
A and B
A= defined $vB= $VALIDATOR_DEF{$v}
ABresult
0X0
100
111
1352 {
1353 6 ▶ $rr->{flag} = 1 if $v eq 'FLAG_VALUE';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "FLAG_VALUE" 2 4 6
1354 6 ▶ push @{$rr->{validators}}, \&boolean_value if $v eq 'FLAG_VALUE';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "FLAG_VALUE" 2 4 6
1355 }
1356
1357 elsif ( defined $v )
1358 {
1359 87 ▶ croak "invalid validator '$v': must be a code ref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v and builtin::reftype($v) eq "CODE" 2 85 87
Condition100%3/3
ref $v and builtin::reftype($v) eq "CODE"
!ll&&!rl&&r
1185
MC/DC100%2/2
ref $v and builtin::reftype($v) eq "CODE"
ref $v builtin::reftype($v) eq "CODE"
Truth table100%3/3
A and B
A= ref $vB= builtin::reftype($v) eq "CODE"
ABresult
0X0
100
111
1360 unless ref $v && reftype $v eq 'CODE';
1361
1362 85 push @{$rr->{validators}}, $v;
1363 }
1364 }
1365
1366 128 ▶ $rr->{$type} = 1 if $type eq 'optional' || $type eq 'mandatory';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "optional" or $type eq "mandatory" 28 100 128
Condition100%3/3
$type eq "optional" or $type eq "mandatory"
l!l&&r!l&&!r
244100
MC/DC100%2/2
$type eq "optional" or $type eq "mandatory"
$type eq "optional" $type eq "mandatory"
Truth table100%3/3
A or B
A= $type eq "optional"B= $type eq "mandatory"
ABresult
1X1
011
000
1367
1368 128 ▶ if ( $type ne 'optional' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "optional") 104 24 128
1369 {
1370 104 ▶ push @{$rs->{fulfill_order}}, $typevalue unless $rs->{params}{$typevalue};
Branch: 1 branch
BranchTrueFalseTotal
unless $rs->{"params"}{$typevalue} 104 0 104
1371 }
1372
1373 128 $rs->{params}{$typevalue} = 1;
1374
1375 # If a default value was given, run it through all of the
1376 # validators in turn until it passes one of them. Store the
1377 # resulting clean value. If the default does not pass any of the
1378 # validators, throw an error.
1379
1380 128 ▶ if ( defined $rr->{default} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rr->{"default"}) 3 125 128
1381 {
1382 3 ▶ croak "default value must be a scalar\n" if ref $rr->{default};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rr->{"default"} 0 3 3
1383
1384 next RULE unless ref $rr->{validators} eq 'ARRAY' &&
1385 3 ▶ @{$rr->{validators}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rr->{"validators"} eq "ARRAY" and @{$rr->{"validators"};} 1 2 3
Condition66%2/3
ref $rr->{"validators"} eq "ARRAY" and @{$rr->{"validators"};}
!ll&&!rl&&r
102
MC/DC50%1/2
ref $rr->{"validators"} eq "ARRAY" and @{$rr->{"validators"};}
ref $rr->{"validators"} eq "ARRAY" @{$rr->{"validators"};}
Truth table66%2/3
A and B
A= ref $rr->{"validators"} eq "ARRAY"B= @{$rr->{"validators"};}
ABresult
0X0
100
111
1386
1387 2 foreach my $v ( @{$rr->{validators}} )
1388 {
1389 2 my $result = $v->($rr->{default}, {});
1390
1391 2 ▶ next RULE unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 2 2
1392
1393 2 ▶ if ( exists $result->{value} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"value"}) 1 1 2
1394 {
1395 1 $rr->{default} = $result->{value};
1396 1 ▶ croak "cleaned default value must be a scalar\n" if ref $rr->{default};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rr->{"default"} 0 1 1
1397 1 next RULE;
1398 }
1399 }
1400
1401 1 croak "the default value '$rr->{default}' failed all of the validators\n";
1402 }
1403 }
1404
1405 elsif ( $CATEGORY{$type} eq 'modifier' )
1406 {
1407 5 $rr->{type} = $type;
1408 5 $rr->{param} = [];
1409
1410 5 ▶ my @params = ref $typevalue eq 'ARRAY' ? @$typevalue : $typevalue;
Branch: 1 branch
BranchTrueFalseTotal
ref $typevalue eq 'ARRAY' ? : 4 1 5
1411
1412 5 foreach my $arg (@params)
1413 {
1414 # croak "parameter '$arg' was not defined" unless defined
1415 # $rs->{params}{$arg} || $type eq 'ignore';
1416 9 push @{$rr->{param}}, $arg;
1417 }
1418
1419 croak "a rule of type '$type' requires at least one parameter name"
1420 5 ▶ unless @{$rr->{param}} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rr->{"param"};} > 0 0 5 5
1421 }
1422
1423 elsif ( $CATEGORY{$type} eq 'include' )
1424 {
1425 31 $rr->{type} = 'include';
1426 31 ▶ $rr->{require} = 1 if $type eq 'require';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "require" 6 25 31
1427 31 $rr->{ruleset} = $typevalue;
1428
1429 31 ▶ croak "the value of '$type' must be a ruleset name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $typevalue and not ref $typevalue and $typevalue ne "" 1 29 30
Condition83%5/6
defined $typevalue and not ref $typevalue and $typevalue ne ""
!ll&&!rl&&r
2029
defined $typevalue and not ref $typevalue
!ll&&!rl&&r
1129
MC/DC66%2/3
defined $typevalue and not ref $typevalue and $typevalue ne ""
defined $typevalue not ref $typevalue $typevalue ne ""
Truth table75%3/4
A and not(B) and C
A= defined $typevalueB= not ref $typevalueC= $typevalue ne ""
ABCresult
0XX0
10X0
1100
1111
1430 unless defined $typevalue && !ref $typevalue && $typevalue ne '';
1431
1432 29 ▶ croak "ruleset '$typevalue' not found" unless defined $self->{RULESETS}{$typevalue};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"RULESETS"}{$typevalue} 1 28 29
1433
1434 28 $rs->{includes}{$typevalue} = 1;
1435 }
1436
1437 elsif ( $CATEGORY{$type} eq 'constraint' )
1438 {
1439 10 $rr->{type} = 'constraint';
1440 10 $rr->{constraint} = $type;
1441 10 $rr->{ruleset} = [];
1442
1443 10 ▶ croak "the value of '$type' must be a list of ruleset names"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $typevalue and ref $typevalue eq "ARRAY" 0 10 10
Condition33%1/3
defined $typevalue and ref $typevalue eq "ARRAY"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $typevalue and ref $typevalue eq "ARRAY"
defined $typevalue ref $typevalue eq "ARRAY"
Truth table33%1/3
A and B
A= defined $typevalueB= ref $typevalue eq "ARRAY"
ABresult
0X0
100
111
1444 unless defined $typevalue && ref $typevalue eq 'ARRAY';
1445
1446 10 foreach my $arg (@$typevalue)
1447 {
1448 20 ▶ next unless defined $arg && $arg ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg and $arg ne "" 0 20 20
Condition33%1/3
defined $arg and $arg ne ""
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $arg and $arg ne ""
defined $arg $arg ne ""
Truth table33%1/3
A and B
A= defined $argB= $arg ne ""
ABresult
0X0
100
111
1449
1450 20 ▶ croak "ruleset '$arg' was not included by any rule" unless defined $rs->{includes}{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rs->{"includes"}{$arg} 0 20 20
1451 20 push @{$rr->{ruleset}}, $arg;
1452 }
1453
1454 croak "a rule of type '$type' requires at least one ruleset name"
1455 10 ▶ unless @{$rr->{ruleset}} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rr->{"ruleset"};} > 0 0 10 10
1456 }
1457
1458 elsif ( $type eq 'content_type' )
1459 {
1460 4 $rr->{type} = 'content_type';
1461 4 $rr->{param} = $typevalue;
1462
1463 4 my %map;
1464
1465 4 ▶ croak "invalid parameter name '$typevalue'" if ref $typevalue || $typevalue !~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $typevalue or not $typevalue =~ /\w/ 0 4 4
Condition33%1/3
ref $typevalue or not $typevalue =~ /\w/
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $typevalue or not $typevalue =~ /\w/
ref $typevalue not $typevalue =~ /\w/
Truth table33%1/3
A or not(B)
A= ref $typevalueB= not $typevalue =~ /\w/
ABresult
1X1
011
000
1466
1467 4 ▶ my @types = ref $rule->{valid} eq 'ARRAY' ? @{$rule->{valid}} : $rule->{valid};
Branch: 1 branch
BranchTrueFalseTotal
ref $rule->{'valid'} eq 'ARRAY' ? : 4 0 4
1468
1469 4 foreach my $t (@types)
1470 {
1471 10 ▶ if ( $t eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "") 0 10 10
1472 {
1473 0 carp "ignored empty value '$t' for 'content_type'";
1474 0 next;
1475 }
1476
1477 10 my ($short, $long) = split /\s*=\s*/, $t;
1478 10 ▶ $long ||= $MEDIA_TYPE{$short};
Condition100%3/3
$long ||= $MEDIA_TYPE{$short}
l!l&&r!l&&!r
361
MC/DC100%2/2
$long ||= $MEDIA_TYPE{$short}
$long $MEDIA_TYPE{$short}
Truth table100%3/3
A ||= B
A= $longB= $MEDIA_TYPE{$short}
ABresult
1X1
011
000
1479
1480 10 ▶ croak "unknown content type for '$short': you must specify a full content type with '$short=some/type'"
Branch: 1 branch
BranchTrueFalseTotal
unless $long 1 9 10
1481 unless $long;
1482
1483 9 ▶ croak "type '$short' cannot be specified twice" if defined $rr->{type_map}{$short};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rr->{"type_map"}{$short} 0 9 9
1484
1485 9 $rr->{type_map}{$short} = $long;
1486 9 push @{$rr->{type_list}}, $short;
1487 }
1488
1489 3 ▶ croak "you must specify at least one value for 'content_type'" unless $rr->{type_map};
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->{"type_map"} 0 3 3
1490 }
1491
1492 else
1493 {
1494 0 croak "invalid rule type '$type'\n";
1495 }
1496 }
1497
1498 # If we have documentation strings collected up, then they belong to the
1499 # last-defined rule. Then call add_doc with a special parameter
1500 # to close any pending lists.
1501
1502 61 $self->add_doc($rs, $doc_rule, @doc_lines);
1503 }
1504  
1505  
1506 # add_doc ( ruleset, rule_record, line... )
1507 #
1508 # Add the specified documentation lines to the specified ruleset. If
1509 # $rule_record is defined, it represents the rule to which this documentation
1510 # applies. Otherwise, the documentation represents header material to be
1511 # output before the documentation for the first rule. If the beginning of the
1512 # first documentation line is '!', then return without doing anything.
1513 #
1514 # Any line starting with = is, of course, taken to indicate a Pod command
1515 # paragraph. It will be preceded and followed by a blank line.
1516 #
1517 # If $rule_record is undefined, then close any pending lists and do nothing
1518 # else.
1519  
1520 sub add_doc {
1521  
1522 238 ▶ my ($self, $rs, $rr, @lines) = @_;
Subroutine add_doc: called CC=20 SCAR=30.2
Pod undocumented
1523
1524 # Don't do anything unless we were given either a rule record or some
1525 # documentation or both.
1526
1527 238 ▶ return unless defined($rr) || @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rr or @lines 66 172 238
Condition100%3/3
defined $rr or @lines
l!l&&r!l&&!r
167566
MC/DC100%2/2
defined $rr or @lines
defined $rr @lines
Truth table100%3/3
A or B
A= defined $rrB= @lines
ABresult
1X1
011
000
1528
1529 # If the first documentation line starts with !, return without doing
1530 # anything. That character indicates that this rule should not be
1531 # documented.
1532
1533 172 ▶ return if @lines && $lines[0] =~ /^[!]/;
Branch: 1 branch
BranchTrueFalseTotal
if @lines and $lines[0] =~ /^[!]/ 1 171 172
Condition100%3/3
@lines and $lines[0] =~ /^[!]/
!ll&&!rl&&r
159121
MC/DC100%2/2
@lines and $lines[0] =~ /^[!]/
@lines $lines[0] =~ /^[!]/
Truth table100%3/3
A and B
A= @linesB= $lines[0] =~ /^[!]/
ABresult
0X0
100
111
1534
1535 # Similarly, return without doing anything if the rule contains the
1536 # 'undocumented' attribute."
1537
1538 171 ▶ return if defined $rr && $rr->{undocumented};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rr and $rr->{"undocumented"} 1 170 171
Condition100%3/3
defined $rr and $rr->{"undocumented"}
!ll&&!rl&&r
51651
MC/DC100%2/2
defined $rr and $rr->{"undocumented"}
defined $rr $rr->{"undocumented"}
Truth table100%3/3
A and B
A= defined $rrB= $rr->{"undocumented"}
ABresult
0X0
100
111
1539
1540 # Otherwise, put the documentation lines together into a single string
1541 # (which may contain a series of POD paragraphs).
1542
1543 170 my $body = '';
1544 170 my $last_pod;
1545 my $this_pod;
1546
1547 170 foreach my $line (@lines)
1548 {
1549 # If this line starts with =, then it needs extra spacing.
1550
1551 15 my $this_pod = $line =~ qr{ ^ = }x;
1552
1553 # If $body already has something in it, add a newline first. Add
1554 # two if this line starts with =, or if the previously added line
1555 # did, so that we get a new paragraph.
1556
1557 15 ▶ if ( $body ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($body ne "") 4 11 15
1558 {
1559 4 ▶ $body .= "\n" if $last_pod || $this_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $last_pod or $this_pod 0 4 4
Condition33%1/3
$last_pod or $this_pod
l!l&&r!l&&!r
004
MC/DC0%0/2
$last_pod or $this_pod
$last_pod $this_pod
Truth table33%1/3
A or B
A= $last_podB= $this_pod
ABresult
1X1
011
000
1560 4 $body .= "\n";
1561 }
1562
1563 15 $body .= $line;
1564 15 $last_pod = $this_pod;
1565 }
1566
1567 # Then add the documentation to the ruleset record:
1568
1569 # If there is no attached rule, then we add the body as an ordinary paragraph.
1570
1571 170 ▶ unless ( defined $rr )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $rr) { } 165 5 170
if (do { defined $rr and $rr->{'type'} eq 'param' }) { } 125 40 165
elsif (defined $rr and $rr->{'type'} eq 'include') { } 27 13 40
1572 {
1573 5 push @{$rs->{doc_items}}, "=ORDINARY";
1574 5 ▶ push @{$rs->{doc_items}}, process_doc($body) if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 5 0 5
1575 }
1576
1577 # If the indicated rule is a parameter rule, then add its record to the list.
1578
1579 ▶ elsif ( defined $rr and $rr->{type} eq 'param' )
Condition66%2/3
defined $rr and $rr->{"type"} eq "param"
!ll&&!rl&&r
040125
MC/DC50%1/2
defined $rr and $rr->{"type"} eq "param"
defined $rr $rr->{"type"} eq "param"
Truth table66%2/3
A and B
A= defined $rrB= $rr->{"type"} eq "param"
ABresult
0X0
100
111
1580 {
1581 125 push @{$rs->{doc_items}}, $rr;
1582 125 weaken $rs->{doc_items}[-1];
1583 125 ▶ push @{$rs->{doc_items}}, process_doc($body, 1) if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 125 0 125
1584 }
1585
1586 # If this is an include rule, then we add a special line to include the
1587 # specified ruleset(s).
1588
1589 ▶ elsif ( defined $rr and $rr->{type} eq 'include' )
Condition66%2/3
defined $rr and $rr->{"type"} eq "include"
!ll&&!rl&&r
01327
MC/DC50%1/2
defined $rr and $rr->{"type"} eq "include"
defined $rr $rr->{"type"} eq "include"
Truth table66%2/3
A and B
A= defined $rrB= $rr->{"type"} eq "include"
ABresult
0X0
100
111
1590 {
1591 27 push @{$rs->{doc_items}}, "=INCLUDE $rr->{ruleset}";
1592
1593 # If any body text was specified, then add it as an ordinary paragraph
1594 # after the inclusion.
1595
1596 27 ▶ if ( $body ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($body ne "") 1 26 27
1597 {
1598 1 push @{$rs->{doc_items}}, "=ORDINARY";
1599 1 ▶ push @{$rs->{doc_items}}, process_doc($body) if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 1 0 1
1600 }
1601 }
1602 }
1603  
1604  
1605 # process_doc ( )
1606 #
1607 # Make sure that the indicated string is valid POD. In particular, if there
1608 # are any unclosed =over sections, close them at the end. Throw an exception
1609 # if we find an =item before the first =over or a =head inside an =over.
1610  
1611 sub process_doc {
1612  
1613 131 ▶ my ($docstring, $item_body) = @_;
Subroutine process_doc: called CC=10 SCAR=42.0
Pod undocumented
1614
1615 131 my ($list_level) = 0;
1616
1617 131 ▶ while ( $docstring =~ / ^ (=[a-z]+) /gmx )
Branch: 1 branch
BranchTrueFalseTotal
if ($docstring =~ / ^ (=[a-z]+) /gmx) 0 131 131
1618 {
1619 0 ▶ if ( $1 eq '=over' )
Branch: 4 branches
BranchTrueFalseTotal
if ($1 eq '=over') { } 0 0 0
elsif ($1 eq '=back') { } 0 0 0
elsif ($1 eq '=item') { } 0 0 0
elsif ($1 eq '=head') { } 0 0 0
1620 {
1621 0 $list_level++;
1622 }
1623
1624 elsif ( $1 eq '=back' )
1625 {
1626 0 $list_level--;
1627 0 ▶ croak "invalid POD string: =back does not match any =over" if $list_level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $list_level < 0 0 0 0
1628 }
1629
1630 elsif ( $1 eq '=item' )
1631 {
1632 0 ▶ croak "invalid POD string: =item outside of =over" if $list_level == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $list_level == 0 0 0 0
1633 }
1634
1635 elsif ( $1 eq '=head' )
1636 {
1637 0 ▶ croak "invalid POD string: =head inside =over" if $list_level > 0 or $item_body;
Branch: 1 branch
BranchTrueFalseTotal
if $list_level > 0 or $item_body 0 0 0
Condition0%0/3
$list_level > 0 or $item_body
l!l&&r!l&&!r
000
MC/DC0%0/2
$list_level > 0 or $item_body
$list_level > 0 $item_body
Truth table0%0/3
A or B
A= $list_level > 0B= $item_body
ABresult
1X1
011
000
1638 }
1639 }
1640
1641 131 return $docstring, ('=back') x $list_level;
1642 }
1643  
1644  
1645 # generate_docstring ( ruleset )
1646 #
1647 # Generate the documentation string for the specified ruleset, recursively
1648 # evaluating all of the rulesets it includes. This will generate a series of
1649 # flat top-level lists describing all of the various parameters, potentially
1650 # with non-list paragraphs in between.
1651  
1652 sub generate_docstring {
1653  
1654 6 ▶ my ($self, $rs, $state) = @_;
Subroutine generate_docstring: called CC=21 SCAR=31.2
Pod undocumented
1655
1656 # Make sure that we process each ruleset only once, even if it is included
1657 # multiple times. Also keep track of our recursion level.
1658
1659 6 ▶ return '' if $state->{processed}{$rs->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"processed"}{$rs->{"name"}} 0 6 6
1660
1661 6 $state->{processed}{$rs->{name}} = 1;
1662 6 $state->{level}++;
1663
1664 # Start with an empty string. If there are no doc_items for this
1665 # ruleset, just return that.
1666
1667 6 my $doc = '';
1668
1669 6 ▶ return $doc unless ref $rs && ref $rs->{doc_items} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rs and ref $rs->{"doc_items"} eq "ARRAY" 0 6 6
Condition33%1/3
ref $rs and ref $rs->{"doc_items"} eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $rs and ref $rs->{"doc_items"} eq "ARRAY"
ref $rs ref $rs->{"doc_items"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $rsB= ref $rs->{"doc_items"} eq "ARRAY"
ABresult
0X0
100
111
1670
1671 # Go through each docstring, treating it as a POD paragraph. That means
1672 # that they will be separated from each other by a blank line.
1673
1674 6 foreach my $item ( @{$rs->{doc_items}} )
1675 {
1676 # An item record starts a list if not already in one.
1677
1678 38 ▶ if ( ref $item && defined $item->{param} )
Branch: 3 branches
BranchTrueFalseTotal
if (ref $item and defined $item->{'param'}) { } 10 28 38
elsif ($item =~ qr/ ^ =ORDINARY /x) { } 8 20 28
elsif ($item =~ qr/ ^ =INCLUDE \s* (.*) /sx) { } 2 18 20
Condition66%2/3
ref $item and defined $item->{"param"}
!ll&&!rl&&r
28010
MC/DC50%1/2
ref $item and defined $item->{"param"}
ref $item defined $item->{"param"}
Truth table66%2/3
A and B
A= ref $itemB= defined $item->{"param"}
ABresult
0X0
100
111
1679 {
1680 10 ▶ unless ( $state->{in_list} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($state->{"in_list"}) 5 5 10
1681 {
1682 5 ▶ $doc .= "\n\n" if $doc ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $doc ne "" 2 3 5
1683 5 $doc .= "=over";
1684 5 $state->{in_list} = 1;
1685 }
1686
1687 10 $doc .= "\n\n=item $item->{param}";
1688 }
1689
1690 # A string starting with =ORDINARY closes any current list.
1691
1692 elsif ( $item =~ qr{ ^ =ORDINARY }x )
1693 {
1694 8 ▶ if ( $state->{in_list} )
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"in_list"}) 3 5 8
1695 {
1696 3 ▶ $doc .= "\n\n" if $doc ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $doc ne "" 3 0 3
1697 3 $doc .= "=back";
1698 3 $state->{in_list} = 0;
1699 }
1700 }
1701
1702 # A string starting with =INCLUDE inserts the specified ruleset.
1703
1704 elsif ( $item =~ qr{ ^ =INCLUDE \s* (.*) }xs )
1705 {
1706 2 my $included_rs = $self->{RULESETS}{$1};
1707
1708 2 ▶ if ( ref $included_rs eq 'HTTP::Validate::Ruleset' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $included_rs eq "HTTP::Validate::Ruleset") 2 0 2
1709 {
1710 2 my $subdoc = $self->generate_docstring($included_rs, $state);
1711
1712 2 ▶ $doc .= "\n\n" if $doc ne '' && $subdoc ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $doc ne "" and $subdoc ne "" 2 0 2
Condition33%1/3
$doc ne "" and $subdoc ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
$doc ne "" and $subdoc ne ""
$doc ne "" $subdoc ne ""
Truth table33%1/3
A and B
A= $doc ne ""B= $subdoc ne ""
ABresult
0X0
100
111
1713 2 ▶ $doc .= $subdoc if $subdoc ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $subdoc ne "" 2 0 2
1714 }
1715 }
1716
1717 # All other strings are added as-is.
1718
1719 else
1720 {
1721 18 ▶ $doc .= "\n\n" if $doc ne '' && $item ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $doc ne "" and $item ne "" 14 4 18
Condition100%3/3
$doc ne "" and $item ne ""
!ll&&!rl&&r
3114
MC/DC100%2/2
$doc ne "" and $item ne ""
$doc ne "" $item ne ""
Truth table100%3/3
A and B
A= $doc ne ""B= $item ne ""
ABresult
0X0
100
111
1722 18 $doc .= $item;
1723 }
1724 }
1725
1726 # If we get to the end of the top-level ruleset and we are still in a
1727 # list, close it. Also make sure that our resulting documentation string
1728 # ends with a newline.
1729
1730 6 ▶ if ( --$state->{level} == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (--$state->{"level"} == 0) 4 2 6
1731 {
1732 4 ▶ $doc .= "\n\n=back" if $state->{in_list};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"in_list"} 2 2 4
1733 4 $state->{in_list} = 0;
1734 4 $doc .= "\n";
1735 }
1736
1737 6 return $doc;
1738 }
1739  
1740  
1741 # generate_param_list ( ruleset )
1742 #
1743 # Generate a list of unique parameter names for the ruleset and its included
1744 # rulesets if any.
1745  
1746 sub generate_param_list {
1747
1748 3 ▶ my ($self, $rs_name, $uniq) = @_;
Subroutine generate_param_list: called CC=6 SCAR=18.0
Pod undocumented
1749
1750 3 ▶ $uniq ||= {};
Condition100%2/2
$uniq ||= {}
l!l
21
MC/DC100%1/1
$uniq ||= {}
$uniq
Truth table100%2/2
A
A= $uniq
Aresult
11
00
1751
1752 3 ▶ return if $uniq->{$rs_name}; $uniq->{$rs_name} = 1;
Branch: 1 branch
BranchTrueFalseTotal
if $uniq->{$rs_name} 0 3 3
1753
1754 3 my @params;
1755
1756 3 foreach my $rule ( @{$self->{RULESETS}{$rs_name}{rules}} )
1757 {
1758 7 ▶ if ( $rule->{type} eq 'param' )
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'type'} eq 'param') { } 5 2 7
elsif ($rule->{'type'} eq 'include') { } 2 0 2
1759 {
1760 5 push @params, $rule->{param};
1761 }
1762
1763 elsif ( $rule->{type} eq 'include' )
1764 {
1765 2 push @params, $self->generate_param_list($rule->{ruleset}, $uniq);
1766 }
1767 }
1768
1769 3 return @params;
1770 }
1771  
1772  
1773 # new_execution ( context, params )
1774 #
1775 # Create a new validation-execution control record, using the given context
1776 # and input parameters.
1777  
1778 sub new_execution {
1779
1780 60 ▶ my ($self, $context, $input_params) = @_;
Subroutine new_execution: called CC=13 SCAR=27.7
Pod undocumented
1781
1782 # First check the types of the arguments to this function.
1783
1784 60 ▶ croak "the second parameter to check_params() must be a hashref if defined"
Branch: 1 branch
BranchTrueFalseTotal
if defined $context and !ref($context) || builtin::reftype($context) ne "HASH" 0 60 60
Condition33%2/6
defined $context and !ref($context) || builtin::reftype($context) ne "HASH"
!ll&&!rl&&r
0600
not ref $context or builtin::reftype($context) ne "HASH"
l!l&&r!l&&!r
0060
MC/DC0%0/3
defined $context and !ref($context) || builtin::reftype($context) ne "HASH"
defined $context not ref $context builtin::reftype($context) ne "HASH"
Truth table25%1/4
A and not(B) or C
A= defined $contextB= not ref $contextC= builtin::reftype($context) ne "HASH"
ABCresult
0XX0
1000
11X1
1011
1785 if defined $context && (!ref $context || reftype $context ne 'HASH');
1786
1787 60 ▶ $context = {} unless defined $context;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 0 60 60
1788
1789 60 ▶ croak "the third parameter to check_params() must be a hashref or listref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input_params 0 60 60
1790 unless ref $input_params;
1791
1792 # If the parameters were given as a hashref, just use it straight.
1793
1794 60 my $unpacked_params = {};
1795
1796 60 ▶ if ( reftype $input_params eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($input_params) eq 'HASH') { } 29 31 60
elsif (builtin::reftype($input_params) eq 'ARRAY') { } 31 0 31
1797 {
1798 29 %$unpacked_params = %$input_params;
1799 }
1800
1801 # If the parameters were given as a listref, we need to look for hashrefs
1802 # at the front.
1803
1804 elsif ( reftype $input_params eq 'ARRAY' )
1805 {
1806 # Look for hashrefs at the beginning of the list and unpack them.
1807
1808 31 ▶ while ( ref $input_params->[0] && reftype $input_params->[0] eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input_params->[0] and builtin::reftype($input_params->[0]) eq "HASH") 3 31 34
Condition66%2/3
ref $input_params->[0] and builtin::reftype($input_params->[0]) eq "HASH"
!ll&&!rl&&r
3103
MC/DC50%1/2
ref $input_params->[0] and builtin::reftype($input_params->[0]) eq "HASH"
ref $input_params->[0] builtin::reftype($input_params->[0]) eq "HASH"
Truth table66%2/3
A and B
A= ref $input_params->[0]B= builtin::reftype($input_params->[0]) eq "HASH"
ABresult
0X0
100
111
1809 {
1810 3 my $p = shift @$input_params;
1811
1812 3 foreach my $x (keys %$p)
1813 {
1814 6 add_param($unpacked_params, $x, $p->{$x});
1815 }
1816 }
1817
1818 # All other items must be name/value pairs.
1819
1820 31 ▶ while ( @$input_params )
Branch: 1 branch
BranchTrueFalseTotal
if (@$input_params) 72 31 103
1821 {
1822 72 my $p = shift @$input_params;
1823
1824 72 ▶ if ( ref $p )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p) { } 0 72 72
1825 {
1826 0 croak "invalid parameter '$p'";
1827 }
1828
1829 else
1830 {
1831 72 add_param($unpacked_params, $p, shift @$input_params);
1832 }
1833 }
1834 }
1835
1836 # Anything else is invalid.
1837
1838 else
1839 {
1840 0 croak "the third parameter to check_params() must be a hashref or listref";
1841 }
1842
1843 # Now create a new validation record
1844
1845 60 my %settings = %{$self->{SETTINGS}};
1846
1847 60 my $vr = { raw => $unpacked_params, # the raw parameters and values
1848 clean => { }, # the parameter keys and values
1849 clean_list => [ ], # the parameter keys in order of recognition
1850 context => $context, # context for the validators to use
1851 ps => { }, # the status (failed=0, passed=1, ignored=undef) of each parameter
1852 rs => { }, # the status (checked=1, fulfilled=2) of each ruleset
1853 settings => \%settings, # a copy of our current settings
1854 };
1855
1856 60 return bless $vr, 'HTTP::Validate::Progress';
1857 }
1858  
1859  
1860 sub add_param {
1861  
1862 78 ▶ my ($hash, $param, $value) = @_;
Subroutine add_param: called CC=9 SCAR=29.5
Pod undocumented
1863
1864 # If there is already more than one value for this parameter, add the new
1865 # value(s) to the array ref.
1866
1867 78 ▶ if ( ref $hash->{$param} && reftype $hash->{$param} eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$param} and builtin::reftype($hash->{$param}) eq 'ARRAY') { } 0 78 78
elsif (defined $hash->{$param} and $hash->{$param} ne '') { } 3 75 78
Condition33%1/3
ref $hash->{$param} and builtin::reftype($hash->{$param}) eq "ARRAY"
!ll&&!rl&&r
7800
MC/DC0%0/2
ref $hash->{$param} and builtin::reftype($hash->{$param}) eq "ARRAY"
ref $hash->{$param} builtin::reftype($hash->{$param}) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $hash->{$param}B= builtin::reftype($hash->{$param}) eq "ARRAY"
ABresult
0X0
100
111
1868 {
1869 0 ▶ push @{$hash->{$param}},
Branch: 1 branch
BranchTrueFalseTotal
ref $value && builtin::reftype($value) eq 'ARRAY' ? : 0 0 0
Condition0%0/3
ref $value and builtin::reftype($value) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and builtin::reftype($value) eq "ARRAY"
ref $value builtin::reftype($value) eq "ARRAY"
Truth table0%0/3
A and B
A= ref $valueB= builtin::reftype($value) eq "ARRAY"
ABresult
0X0
100
111
1870 (ref $value && reftype $value eq 'ARRAY' ? @$value : $value);
1871 }
1872
1873 # If there is already one value for this parameter, turn it into an array
1874 # ref.
1875
1876 ▶ elsif ( defined $hash->{$param} && $hash->{$param} ne '' )
Condition100%3/3
defined $hash->{$param} and $hash->{$param} ne ""
!ll&&!rl&&r
7413
MC/DC100%2/2
defined $hash->{$param} and $hash->{$param} ne ""
defined $hash->{$param} $hash->{$param} ne ""
Truth table100%3/3
A and B
A= defined $hash->{$param}B= $hash->{$param} ne ""
ABresult
0X0
100
111
1877 {
1878 3 ▶ $hash->{$param} = [$hash->{$param},
Branch: 1 branch
BranchTrueFalseTotal
ref $value && builtin::reftype($value) eq 'ARRAY' ? : 0 3 3
Condition33%1/3
ref $value and builtin::reftype($value) eq "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
ref $value and builtin::reftype($value) eq "ARRAY"
ref $value builtin::reftype($value) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $valueB= builtin::reftype($value) eq "ARRAY"
ABresult
0X0
100
111
1879 (ref $value && reftype $value eq 'ARRAY' ? @$value : $value)];
1880 }
1881
1882 # Otherwise, set the value for this parameter to be the new value (which
1883 # could be either a scalar or a reference).
1884
1885 else
1886 {
1887 75 $hash->{$param} = $value;
1888 }
1889 }
1890  
1891  
1892 # This function performs a validation using the given validation-progress
1893 # record, starting with the given ruleset, and returns a hash with the
1894 # results.
1895  
1896 sub execute_validation {
1897  
1898 60 ▶ my ($self, $vr, $ruleset_name) = @_;
Subroutine execute_validation: called CC=15 SCAR=27.5
Pod undocumented
1899
1900 60 ▶ croak "you must provide a ruleset name" unless defined $ruleset_name && $ruleset_name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ruleset_name and $ruleset_name ne "" 0 60 60
Condition33%1/3
defined $ruleset_name and $ruleset_name ne ""
!ll&&!rl&&r
0060
MC/DC0%0/2
defined $ruleset_name and $ruleset_name ne ""
defined $ruleset_name $ruleset_name ne ""
Truth table33%1/3
A and B
A= defined $ruleset_nameB= $ruleset_name ne ""
ABresult
0X0
100
111
1901 60 ▶ croak "invalid ruleset name: '$ruleset_name'" if ref $ruleset_name || $ruleset_name !~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ruleset_name or not $ruleset_name =~ /\w/ 0 60 60
Condition33%1/3
ref $ruleset_name or not $ruleset_name =~ /\w/
l!l&&r!l&&!r
0060
MC/DC0%0/2
ref $ruleset_name or not $ruleset_name =~ /\w/
ref $ruleset_name not $ruleset_name =~ /\w/
Truth table33%1/3
A or not(B)
A= ref $ruleset_nameB= not $ruleset_name =~ /\w/
ABresult
1X1
011
000
1902
1903 # First perform the specified validation against the specified ruleset.
1904 # This may trigger validations against additional rulesets if the intial
1905 # one contains 'allow' or 'require' rules.
1906
1907 60 $self->validate_ruleset($vr, $ruleset_name);
1908
1909 # Now, if this ruleset was not fulfilled, add an appropriate error
1910 # message.
1911
1912 60 ▶ if ( $vr->{rs}{$ruleset_name} != 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($vr->{"rs"}{$ruleset_name} != 2) 1 59 60
1913 {
1914 1 my @names = @{$self->{RULESETS}{$ruleset_name}{fulfill_order}};
1915 1 ▶ my $msg = @names == 1 ? 'ERR_REQ_SINGLE': 'ERR_REQ_MULT';
Branch: 1 branch
BranchTrueFalseTotal
@names == 1 ? : 1 0 1
1916 1 add_error($vr, { key => $ruleset_name }, $msg, { param => \@names });
1917 }
1918
1919 # Create an object to hold the result of this function.
1920
1921 60 my $result = bless {}, 'HTTP::Validate::Result';
1922
1923 # Add the clean-value hash and the raw-value hash
1924
1925 60 $result->{clean} = $vr->{clean};
1926 60 $result->{clean_list} = $vr->{clean_list};
1927 60 $result->{raw} = $vr->{raw};
1928
1929 # Put the clean-value hash under the old name, for backward compatibility
1930 # (it will be eventually removed).
1931
1932 60 $result->{values} = $vr->{clean};
1933
1934 # Add the content type, if one was specified.
1935
1936 $result->{content_type} = $vr->{content_type}
1937 if defined $vr->{content_type} and
1938 $vr->{content_type} ne '' and
1939 60 ▶ $vr->{content_type} ne 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
if defined $vr->{"content_type"} and $vr->{"content_type"} ne "" and $vr->{"content_type"} ne "unknown" 3 57 60
Condition83%5/6
defined $vr->{"content_type"} and $vr->{"content_type"} ne "" and $vr->{"content_type"} ne "unknown"
!ll&&!rl&&r
5613
defined $vr->{"content_type"} and $vr->{"content_type"} ne ""
!ll&&!rl&&r
5604
MC/DC66%2/3
defined $vr->{"content_type"} and $vr->{"content_type"} ne "" and $vr->{"content_type"} ne "unknown"
defined $vr->{"content_type"} $vr->{"content_type"} ne "" $vr->{"content_type"} ne "unknown"
Truth table75%3/4
A and B and C
A= defined $vr->{"content_type"}B= $vr->{"content_type"} ne ""C= $vr->{"content_type"} ne "unknown"
ABCresult
0XX0
10X0
1100
1111
1940
1941 # Add any errors that were generated.
1942
1943 60 $result->{ec} = $vr->{ec};
1944 60 $result->{er} = $vr->{er};
1945 60 $result->{wc} = $vr->{wc};
1946 60 $result->{wn} = $vr->{wn};
1947 60 $result->{ig} = $vr->{ig};
1948
1949 # Now check for unrecognized parameters, and generate errors or warnings
1950 # for them.
1951
1952 60 ▶ return $result if $self->{SETTINGS}{ignore_unrecognized};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SETTINGS"}{"ignore_unrecognized"} 8 52 60
1953
1954 52 foreach my $key (keys %{$vr->{raw}})
1955 {
1956 109 ▶ next if exists $vr->{ps}{$key} or exists $vr->{ig}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $vr->{"ps"}{$key} or exists $vr->{"ig"}{$key} 105 4 109
Condition66%2/3
exists $vr->{"ps"}{$key} or exists $vr->{"ig"}{$key}
l!l&&r!l&&!r
10504
MC/DC50%1/2
exists $vr->{"ps"}{$key} or exists $vr->{"ig"}{$key}
exists $vr->{"ps"}{$key} exists $vr->{"ig"}{$key}
Truth table66%2/3
A or B
A= exists $vr->{"ps"}{$key}B= exists $vr->{"ig"}{$key}
ABresult
1X1
011
000
1957
1958 4 ▶ if ( $self->{SETTINGS}{permissive} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SETTINGS'}{'permissive'}) { } 2 2 4
1959 {
1960 2 unshift @{$result->{wn}}, [$key, "unknown parameter '$key'"];
1961 2 $result->{wc}{$key}++;
1962 }
1963 else
1964 {
1965 2 unshift @{$result->{er}}, [$key, "unknown parameter '$key'"];
1966 2 $result->{ec}{$key}++;
1967 }
1968 }
1969
1970 # Now return the result object.
1971
1972 52 return $result;
1973 }
1974  
1975  
1976 # This function does the actual work of validating. It takes two parameters:
1977 # a validation record and a ruleset name. It sets various subfields of the
1978 # validation record according to the results of the validation.
1979  
1980 sub validate_ruleset {
1981  
1982 82 ▶ my ($self, $vr, $ruleset_name) = @_;
Subroutine validate_ruleset: called CC=91 SCAR=47.4
Pod undocumented
1983
1984 82 ▶ die "Missing ruleset" unless defined $ruleset_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ruleset_name 0 82 82
1985
1986 82 my $rs = $self->{RULESETS}{$ruleset_name};
1987
1988 # Throw an error if this ruleset does not exist.
1989
1990 82 ▶ croak "Unknown ruleset '$ruleset_name'" unless ref $rs;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rs 0 82 82
1991
1992 # Return immediately if we have already visited this ruleset. Otherwise,
1993 # mark it as visited.
1994
1995 82 ▶ return if exists $vr->{rs}{$ruleset_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $vr->{"rs"}{$ruleset_name} 0 82 82
1996 82 $vr->{rs}{$ruleset_name} = 1;
1997
1998 # Mark the ruleset as fulfilled if it has no non-optional parameters.
1999
2000 82 ▶ $vr->{rs}{$ruleset_name} = 2 unless ref $rs->{fulfill_order} && @{$rs->{fulfill_order}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rs->{"fulfill_order"} and @{$rs->{"fulfill_order"};} 35 47 82
Condition66%2/3
ref $rs->{"fulfill_order"} and @{$rs->{"fulfill_order"};}
!ll&&!rl&&r
03547
MC/DC50%1/2
ref $rs->{"fulfill_order"} and @{$rs->{"fulfill_order"};}
ref $rs->{"fulfill_order"} @{$rs->{"fulfill_order"};}
Truth table66%2/3
A and B
A= ref $rs->{"fulfill_order"}B= @{$rs->{"fulfill_order"};}
ABresult
0X0
100
111
2001
2002 # Now check all of the rules in this ruleset against the parameter values
2003 # stored in $vr->{raw}.
2004
2005 RULE:
2006 82 foreach my $rr (@{$rs->{rules}})
2007 {
2008 214 my $type = $rr->{type};
2009 214 my $param = $rr->{param};
2010 214 ▶ my $key = $rr->{key} || $param;
Condition100%3/3
$rr->{'key'} || $param
l!l&&r!l&&!r
118528
MC/DC100%2/2
$rr->{'key'} || $param
$rr->{'key'} $param
Truth table100%3/3
A || B
A= $rr->{'key'}B= $param
ABresult
1X1
011
000
2011 214 my $default_used;
2012
2013 # To evaluate a rule of type 'param' we check to see if a
2014 # corresponding parameter was specified.
2015
2016 214 ▶ if ( $type eq 'param' )
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'param') { } 170 44 214
elsif ($rr->{'type'} eq 'ignore') { } 0 44 44
elsif ($rr->{'type'} eq 'together' or $rr->{'type'} eq 'at_most_one') { } 12 32 44
elsif ($rr->{'type'} eq 'include') { } 22 10 32
elsif ($rr->{'type'} eq 'constraint') { } 6 4 10
elsif ($type eq 'content_type') { } 4 0 4
2017 {
2018 170 my (%names_found, @names_found, @raw_values);
2019
2020 # Skip this rule if a previous 'ignore' was encountered.
2021
2022 170 ▶ next RULE if $vr->{ig}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $vr->{"ig"}{$key} 0 170 170
2023
2024 # Otherwise check to see if the parameter or any of its aliases were specified. If
2025 # so, then collect up their values.
2026
2027 170 foreach my $name ( $rr->{param}, @{$rr->{alias}} )
2028 {
2029 177 ▶ next unless exists $vr->{raw}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $vr->{"raw"}{$name} 62 115 177
2030 115 $names_found{$name} = 1;
2031 115 my $v = $vr->{raw}{$name};
2032 115 ▶ push @raw_values, grep { defined $_ && $_ ne '' } ref $v eq 'ARRAY' ? @$v : $v;
Branch: 2 branches
BranchTrueFalseTotal
defined $_ and $_ ne "" 116 2 118
ref $v eq 'ARRAY' ? : 3 112 115
2033 # Make sure this parameter exists in {ps}, but don't
2034 # change its status if any.
2035 115 ▶ $vr->{ps}{$name} = undef unless exists $vr->{ps}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $vr->{"ps"}{$name} 115 0 115
2036 }
2037
2038 # If more than one of the aliases for this parameter was specified, and the 'multiple'
2039 # option was not specified, then generate an error and go on to the next rule. We
2040 # mark the parameter status as "error" (0), and we also mark the ruleset as fulfilled (2)
2041 # if this was a 'param' or 'mandatory' rule. This last is done to avoid generating a
2042 # spurious error message if the ruleset is not fulfilled by any other parameters.
2043
2044 170 ▶ if ( keys(%names_found) > 1 && ! $rr->{multiple} )
Branch: 4 branches
BranchTrueFalseTotal
if (keys %names_found > 1 and not $rr->{'multiple'}) { } 1 169 170
elsif (exists $vr->{'clean'}{$key}) { } 0 169 169
elsif (not @raw_values and exists $rr->{'default'}) { } 1 168 169
elsif (@raw_values > 1 and not $rr->{'multiple'}) { } 2 331 333
Condition66%2/3
keys %names_found > 1 and not $rr->{"multiple"}
!ll&&!rl&&r
16901
MC/DC50%1/2
keys %names_found > 1 and not $rr->{"multiple"}
keys %names_found > 1 not $rr->{"multiple"}
Truth table66%2/3
A and not(B)
A= keys %names_found > 1B= not $rr->{"multiple"}
ABresult
0X0
100
111
2045 {
2046 1 add_error($vr, $rr, 'ERR_MULT_NAMES', { param => [ sort keys %names_found ] });
2047 1 $vr->{ps}{$param} = 0;
2048 1 ▶ $vr->{rs}{$ruleset_name} = 2 unless $rr->{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->{"optional"} 1 0 1
2049 1 next RULE;
2050 }
2051
2052 # If a clean value has already been determined for this parameter, then it was already
2053 # recognized by some other rule. Consequently, this rule can be ignored.
2054
2055 elsif ( exists $vr->{clean}{$key} )
2056 {
2057 0 next RULE;
2058 }
2059
2060 # If no values were specified for this parameter, check to see if the rule includes a
2061 # default value. If so, use that instead and go on to the next rule.
2062
2063 ▶ elsif ( ! @raw_values && exists $rr->{default} )
Condition100%3/3
not @raw_values and exists $rr->{"default"}
!ll&&!rl&&r
100681
MC/DC100%2/2
not @raw_values and exists $rr->{"default"}
not @raw_values exists $rr->{"default"}
Truth table100%3/3
not(A) and B
A= not @raw_valuesB= exists $rr->{"default"}
ABresult
0X0
100
111
2064 {
2065 1 $vr->{clean}{$key} = $rr->{default};
2066 1 push @{$vr->{clean_list}}, $key;
2067 1 next RULE;
2068 }
2069
2070 # If more than one value was given and the rule does not include the 'multiple'
2071 # directive, signal an error. We mark the parameter status as "error" (0), and we
2072 # also mark the ruleset as fulfilled (2) if this was a 'param' or 'mandatory' rule.
2073 # This last is done to avoid generating a spurious error message if the ruleset is not
2074 # fulfilled by any other parameters.
2075
2076 ▶ elsif ( @raw_values > 1 && ! $rr->{multiple} )
Condition100%3/3
@raw_values > 1 and not $rr->{"multiple"}
!ll&&!rl&&r
16512
MC/DC100%2/2
@raw_values > 1 and not $rr->{"multiple"}
@raw_values > 1 not $rr->{"multiple"}
Truth table100%3/3
A and not(B)
A= @raw_values > 1B= not $rr->{"multiple"}
ABresult
0X0
100
111
2077 {
2078 2 add_error($vr, $rr, 'ERR_MULT_VALUES',
2079 { param => [ sort keys %names_found ], value => \@raw_values });
2080 2 $vr->{ps}{$param} = 0;
2081 2 ▶ $vr->{rs}{$ruleset_name} = 2 unless $rr->{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->{"optional"} 2 0 2
2082 2 next RULE;
2083 }
2084
2085 # Now we can process the rule. If the 'split' directive was
2086 # given, split the value(s) using the specified regexp.
2087
2088 166 ▶ if ( $rr->{split} )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{"split"}) 20 146 166
2089 {
2090 # Split all of the raw values, and discard empty strings.
2091
2092 22 ▶ my @new_values = grep { defined $_ && $_ ne '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ ne "" 22 0 22
2093 20 map { split $rr->{split}, $_ } @raw_values;
2094 20 @raw_values = @new_values;
2095 }
2096
2097 # If this is a 'flag' parameter and the parameter was present but
2098 # no values were given, assume the value '1'.
2099
2100 166 ▶ if ( $rr->{flag} && keys(%names_found) && ! @raw_values )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{"flag"} and keys %names_found and not @raw_values) 2 164 166
Condition83%5/6
$rr->{"flag"} and keys %names_found and not @raw_values
!ll&&!rl&&r
16402
$rr->{"flag"} and keys %names_found
!ll&&!rl&&r
16312
MC/DC66%2/3
$rr->{"flag"} and keys %names_found and not @raw_values
$rr->{"flag"} keys %names_found not @raw_values
Truth table75%3/4
A and B and not(C)
A= $rr->{"flag"}B= keys %names_foundC= not @raw_values
ABCresult
0XX0
10X0
1100
1111
2101 {
2102 2 @raw_values = (1);
2103 }
2104
2105 # At this point, if there are no values then generate an error if
2106 # the parameter is mandatory. Otherwise just skip this rule.
2107
2108 166 ▶ unless ( @raw_values )
Branch: 1 branch
BranchTrueFalseTotal
unless (@raw_values) 67 99 166
2109 {
2110 67 ▶ if ( $rr->{mandatory} )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{"mandatory"}) 2 65 67
2111 {
2112 2 add_error($vr, $rr, 'ERR_MANDATORY', { param => $rr->{param} });
2113 2 $vr->{ps}{$param} = 0;
2114 2 ▶ $vr->{rs}{$ruleset_name} = 2 unless $rr->{optional};
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->{"optional"} 2 0 2
2115 }
2116
2117 67 next RULE;
2118 }
2119
2120 # Now indicate that at least one value was found for this
2121 # parameter, even though we don't yet know if it is a good one.
2122 # This will be necessary for properly handling 'together' and
2123 # 'at_most_one' rules.
2124
2125 99 $vr->{clean}{$key} = undef;
2126
2127 # Now we process each value in turn.
2128
2129 99 my @clean_values;
2130 my $error_flag;
2131
2132 VALUE:
2133 99 foreach my $raw_val ( @raw_values )
2134 {
2135 # If no validators were defined, just pass all of the values
2136 # that are not empty.
2137
2138 111 ▶ unless ( $rr->{validators} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($rr->{"validators"}) 40 71 111
2139 {
2140 40 ▶ if ( defined $raw_val && $raw_val ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw_val and $raw_val ne "") 40 0 40
Condition33%1/3
defined $raw_val and $raw_val ne ""
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $raw_val and $raw_val ne ""
defined $raw_val $raw_val ne ""
Truth table33%1/3
A and B
A= defined $raw_valB= $raw_val ne ""
ABresult
0X0
100
111
2141 {
2142 40 ▶ $raw_val = $rr->{cleaner}($raw_val) if ref $rr->{cleaner} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rr->{"cleaner"} eq "CODE" 4 36 40
2143 40 push @clean_values, $raw_val;
2144 }
2145
2146 40 next VALUE;
2147 }
2148
2149 # Otherwise, check each value against the validators in turn until
2150 # one of them passes the value or until we have tried them
2151 # all.
2152
2153 71 my $result;
2154
2155 VALIDATOR:
2156 71 foreach my $validator ( @{$rr->{validators}} )
2157 {
2158 71 $result = $validator->($raw_val, $vr->{context});
2159
2160 # If the result is not a hash ref, then the value passes
2161 # the test.
2162
2163 71 ▶ last VALIDATOR unless ref $result && reftype $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result and builtin::reftype($result) eq "HASH" 4 67 71
Condition66%2/3
ref $result and builtin::reftype($result) eq "HASH"
!ll&&!rl&&r
4067
MC/DC50%1/2
ref $result and builtin::reftype($result) eq "HASH"
ref $result builtin::reftype($result) eq "HASH"
Truth table66%2/3
A and B
A= ref $resultB= builtin::reftype($result) eq "HASH"
ABresult
0X0
100
111
2164
2165 # If the result contains an 'error' key, then we need to
2166 # try the next validator (if any). Otherwise, the value
2167 # passes the test.
2168
2169 67 ▶ last VALIDATOR unless $result->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"error"} 42 25 67
2170 }
2171
2172 # If the last validator to be tried generated an error, then
2173 # the value is bad. We must report it and skip to the next value.
2174
2175 71 ▶ if ( ref $result and $result->{error} )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result and $result->{"error"}) 25 46 71
Condition100%3/3
ref $result and $result->{"error"}
!ll&&!rl&&r
44225
MC/DC100%2/2
ref $result and $result->{"error"}
ref $result $result->{"error"}
Truth table100%3/3
A and B
A= ref $resultB= $result->{"error"}
ABresult
0X0
100
111
2176 {
2177 # If the rule contains a 'warn' directive, then generate a
2178 # warning. But the value is still bad, and will be
2179 # ignored.
2180
2181 25 ▶ if ( $rr->{warn} )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{'warn'}) { } 8 17 25
2182 {
2183 my $msg = $rr->{warn} ne '1' ? $rr->{warn} :
2184 8 ▶ $rr->{ERR_INVALID} || $rr->{errmsg} || $result->{error};
Branch: 1 branch
BranchTrueFalseTotal
$rr->{'warn'} ne '1' ? : 0 8 8
Condition66%4/6
$rr->{"ERR_INVALID"} or $rr->{"errmsg"} or $result->{"error"}
l!l&&r!l&&!r
170
$rr->{"ERR_INVALID"} or $rr->{"errmsg"}
l!l&&r!l&&!r
107
MC/DC0%0/3
$rr->{"ERR_INVALID"} or $rr->{"errmsg"} or $result->{"error"}
$rr->{"ERR_INVALID"} $rr->{"errmsg"} $result->{"error"}
Truth table50%2/4
A or B or C
A= $rr->{"ERR_INVALID"}B= $rr->{"errmsg"}C= $result->{"error"}
ABCresult
1XX1
01X1
0011
0000
2185 8 add_warning($vr, $rr, $msg, { param => [ keys %names_found ], value => $raw_val });
2186 }
2187
2188 # Otherwise, generate an error.
2189
2190 else
2191 {
2192 17 ▶ my $msg = $rr->{ERR_INVALID} || $rr->{errmsg} || $result->{error};
Condition66%4/6
$rr->{'ERR_INVALID'} || $rr->{'errmsg'} || $result->{'error'}
l!l&&r!l&&!r
1160
$rr->{"ERR_INVALID"} or $rr->{"errmsg"}
l!l&&r!l&&!r
1016
MC/DC0%0/3
$rr->{'ERR_INVALID'} || $rr->{'errmsg'} || $result->{'error'}
$rr->{"ERR_INVALID"} $rr->{"errmsg"} $result->{'error'}
Truth table50%2/4
A or B || C
A= $rr->{"ERR_INVALID"}B= $rr->{"errmsg"}C= $result->{'error'}
ABCresult
1XX1
01X1
0011
0000
2193 17 add_error($vr, $rr, $msg, { param => [ sort keys %names_found ], value => $raw_val });
2194 }
2195
2196 25 $error_flag = 1;
2197 25 next VALUE;
2198 }
2199
2200 # If the result contains a 'warn' field, then generate a
2201 # warning. In this case, the value is still assumed to be
2202 # good.
2203
2204 46 ▶ if ( ref $result and $result->{warn} )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result and $result->{"warn"}) 1 45 46
Condition100%3/3
ref $result and $result->{"warn"}
!ll&&!rl&&r
4411
MC/DC100%2/2
ref $result and $result->{"warn"}
ref $result $result->{"warn"}
Truth table100%3/3
A and B
A= ref $resultB= $result->{"warn"}
ABresult
0X0
100
111
2205 {
2206 1 add_warning($vr, $rr, $result->{warn}, { param => [ sort keys %names_found ], value => $raw_val });
2207 }
2208
2209 # If we get here, then the value is good. If the result was a
2210 # hash ref with a 'value' field, we use that for the clean
2211 # value. Otherwise, we use the raw value.
2212
2213 46 ▶ my $value = ref $result && exists $result->{value} ? $result->{value} : $raw_val;
Branch: 1 branch
BranchTrueFalseTotal
ref $result && exists $result->{'value'} ? : 41 5 46
Condition100%3/3
ref $result and exists $result->{"value"}
!ll&&!rl&&r
4141
MC/DC100%2/2
ref $result and exists $result->{"value"}
ref $result exists $result->{"value"}
Truth table100%3/3
A and B
A= ref $resultB= exists $result->{"value"}
ABresult
0X0
100
111
2214
2215 # If a cleaning subroutine was defined, pass the value through
2216 # it and save the cleaned value.
2217
2218 46 ▶ $value = $rr->{cleaner}($value) if ref $rr->{cleaner} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rr->{"cleaner"} eq "CODE" 0 46 46
2219
2220 46 push @clean_values, $value;
2221 }
2222
2223 # If clean values were found, store them. If multiple values are
2224 # allowed, then we store them as a list. Otherwise, there should
2225 # only be one clean value and so we just store it as a scalar.
2226
2227 99 ▶ if ( @clean_values )
Branch: 1 branch
BranchTrueFalseTotal
if (@clean_values) { } 78 21 99
2228 {
2229 78 push @{$vr->{clean_list}}, $key;
2230
2231 78 ▶ if ( $rr->{multiple} )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{'multiple'}) { } 7 71 78
2232 {
2233 7 $vr->{clean}{$key} = \@clean_values;
2234 }
2235
2236 else
2237 {
2238 71 $vr->{clean}{$key} = $clean_values[0];
2239 }
2240 }
2241
2242 # If raw values were found for this parameter, but none of them
2243 # pass the validators, then we need to indicate this condition.
2244
2245 else
2246 {
2247 21 push @{$vr->{clean_list}}, $key;
2248
2249 21 ▶ if ( defined $rr->{bad_value} && $rr->{bad_value} eq 'ERROR' )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rr->{'bad_value'} and $rr->{'bad_value'} eq 'ERROR') { } 2 19 21
elsif (defined $rr->{'bad_value'}) { } 1 18 19
Condition100%3/3
defined $rr->{"bad_value"} and $rr->{"bad_value"} eq "ERROR"
!ll&&!rl&&r
1812
MC/DC100%2/2
defined $rr->{"bad_value"} and $rr->{"bad_value"} eq "ERROR"
defined $rr->{"bad_value"} $rr->{"bad_value"} eq "ERROR"
Truth table100%3/3
A and B
A= defined $rr->{"bad_value"}B= $rr->{"bad_value"} eq "ERROR"
ABresult
0X0
100
111
2250 {
2251 2 add_error($vr, $rr, 'ERR_BAD_VALUES',
2252 { param => [ sort keys %names_found ], value => \@raw_values });
2253 2 $vr->{clean}{$key} = undef;
2254 2 $error_flag = 1;
2255 }
2256
2257 elsif ( defined $rr->{bad_value} )
2258 {
2259 1 ▶ $vr->{clean}{$key} = $rr->{multiple} ? [ $rr->{bad_value} ] : $rr->{bad_value};
Branch: 1 branch
BranchTrueFalseTotal
$rr->{'multiple'} ? : 1 0 1
2260 }
2261
2262 else
2263 {
2264 18 $vr->{clean}{$key} = undef;
2265 }
2266 }
2267
2268 # Set the status of this parameter to 1 (passed) unless an error
2269 # was generated, 0 (failed) otherwise.
2270
2271 99 ▶ $vr->{ps}{$param} = $error_flag ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$error_flag ? : 22 77 99
2272
2273 # If this rule is not 'optional', then set the status of this
2274 # ruleset to 'fulfilled' (2). That does not mean that the validation
2275 # passes, because the parameter value may still have generated an
2276 # error.
2277
2278 99 ▶ unless ( $rr->{optional} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($rr->{"optional"}) 77 22 99
2279 {
2280 77 $vr->{rs}{$ruleset_name} = 2;
2281 }
2282 }
2283
2284 # An 'ignore' directive causes the parameter to be recognized, but no
2285 # cleaned value is generated and the containing ruleset is not
2286 # triggered. No error messages will be generated for this parameter,
2287 # either.
2288
2289 elsif ( $rr->{type} eq 'ignore' )
2290 {
2291 # Make sure that the parameter is counted as having been
2292 # recognized.
2293
2294 0 foreach my $param ( @{$rr->{param}} )
2295 {
2296 0 $vr->{ps}{$param} = undef;
2297
2298 # Make sure that errors, warnings, and cleaned values for this key
2299 # are ignored.
2300
2301 0 ▶ my $key = $rr->{key} || $param;
Condition0%0/3
$rr->{'key'} || $param
l!l&&r!l&&!r
000
MC/DC0%0/2
$rr->{'key'} || $param
$rr->{'key'} $param
Truth table0%0/3
A || B
A= $rr->{'key'}B= $param
ABresult
1X1
011
000
2302 0 $vr->{ig}{$key} = 1;
2303 0 delete $vr->{clean}{$param};
2304 }
2305 }
2306
2307 # A 'together' or 'at_most_one' rule requires checking the presence
2308 # of each of the specified parameters. This kind of rule does not
2309 # affect the status of any parameters or rulesets, but if violated
2310 # will generate an error message and cause the entire validation to
2311 # fail.
2312
2313 ▶ elsif ( $rr->{type} eq 'together' or $rr->{type} eq 'at_most_one' )
Condition100%3/3
$rr->{"type"} eq "together" or $rr->{"type"} eq "at_most_one"
l!l&&r!l&&!r
5732
MC/DC100%2/2
$rr->{"type"} eq "together" or $rr->{"type"} eq "at_most_one"
$rr->{"type"} eq "together" $rr->{"type"} eq "at_most_one"
Truth table100%3/3
A or B
A= $rr->{"type"} eq "together"B= $rr->{"type"} eq "at_most_one"
ABresult
1X1
011
000
2314 {
2315 # We start by listing those that are present in the parameter set.
2316
2317 12 my @present = grep exists $vr->{clean}{$_}, @{$rr->{param}};
2318
2319 # For a 'together' rule, the count must equal the number of
2320 # arguments to this rule, or must be zero. In other words, there
2321 # must be none present or all present.
2322
2323 12 ▶ if ( $rr->{type} eq 'together' and @present > 0 and @present < @{$rr->{param}} )
Branch: 2 branches
BranchTrueFalseTotal
if ($rr->{'type'} eq 'together' and @present > 0 and @present < @{$rr->{'param'};}) { } 1 11 12
elsif ($rr->{'type'} eq 'at_most_one' and @present > 1) { } 2 13 15
Condition83%5/6
$rr->{"type"} eq "together" and @present > 0 and @present < @{$rr->{"param"};}
!ll&&!rl&&r
1101
$rr->{"type"} eq "together" and @present > 0
!ll&&!rl&&r
741
MC/DC66%2/3
$rr->{"type"} eq "together" and @present > 0 and @present < @{$rr->{"param"};}
$rr->{"type"} eq "together" @present > 0 @present < @{$rr->{"param"};}
Truth table75%3/4
A and B and C
A= $rr->{"type"} eq "together"B= @present > 0C= @present < @{$rr->{"param"};}
ABCresult
0XX0
10X0
1100
1111
2324 {
2325 1 add_error_warn($vr, $rr, 'ERR_TOGETHER', { param => $rr->{param} });
2326 }
2327
2328 # For a 'at_most_one' rule, the count must be less than or equal
2329 # to one (i.e. not more than one must have been specified).
2330
2331 ▶ elsif ( $rr->{type} eq 'at_most_one' and @present > 1 )
Condition100%3/3
$rr->{"type"} eq "at_most_one" and @present > 1
!ll&&!rl&&r
452
MC/DC100%2/2
$rr->{"type"} eq "at_most_one" and @present > 1
$rr->{"type"} eq "at_most_one" @present > 1
Truth table100%3/3
A and B
A= $rr->{"type"} eq "at_most_one"B= @present > 1
ABresult
0X0
100
111
2332 {
2333 2 add_error_warn($vr, $rr, 'ERR_AT_MOST', { param => \@present });
2334 }
2335 }
2336
2337 # For an 'include' rule, we immediately check the given ruleset
2338 # (unless it has already been checked). This statement essentially
2339 # includes one ruleset within another. It is very powerful, because
2340 # it allows different route handlers to to validate their parameters
2341 # using common rulesets.
2342
2343 elsif ( $rr->{type} eq 'include' )
2344 {
2345 22 my $rs_name = $rr->{ruleset};
2346
2347 # First try to validate the given ruleset.
2348
2349 22 $self->validate_ruleset($vr, $rs_name);
2350
2351 # If it was a 'require' rule, check to see if the ruleset was
2352 # fulfilled.
2353
2354 22 ▶ if ( $rr->{require} and not $vr->{rs}{$rs_name} == 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{"require"} and not $vr->{"rs"}{$rs_name} == 2) 1 21 22
Condition100%3/3
$rr->{"require"} and not $vr->{"rs"}{$rs_name} == 2
!ll&&!rl&&r
1651
MC/DC100%2/2
$rr->{"require"} and not $vr->{"rs"}{$rs_name} == 2
$rr->{"require"} not $vr->{"rs"}{$rs_name} == 2
Truth table100%3/3
A and not(B)
A= $rr->{"require"}B= not $vr->{"rs"}{$rs_name} == 2
ABresult
0X0
100
111
2355 {
2356 1 my (@missing, %found);
2357
2358 1 @missing = grep { unique($_, \%found) } @{$self->{RULESETS}{$rs_name}{fulfill_order}};
2359
2360 1 ▶ my $msg = @missing == 1 ? 'ERR_REQ_SINGLE' : 'ERR_REQ_MULT';
Branch: 1 branch
BranchTrueFalseTotal
@missing == 1 ? : 0 1 1
2361 1 add_error_warn($vr, $rr, $msg, { param => \@missing });
2362 }
2363 }
2364
2365 elsif ( $rr->{type} eq 'constraint' )
2366 {
2367 # From the list of rulesets specified in this rule, check how many
2368 # were and were not fulfilled.
2369
2370 6 my @fulfilled = grep { $vr->{rs}{$_} == 2 } @{$rr->{ruleset}};
2371 6 my @not_fulfilled = grep { $vr->{rs}{$_} != 2 } @{$rr->{ruleset}};
2372
2373 # For a 'require_one' or 'require_any' rule, generate an error if
2374 # not enough of the rulesets are fulfilled. List all of the
2375 # parameters which could be given in order to fulfill these
2376 # rulesets.
2377
2378 6 ▶ if ( @fulfilled == 0 and ( $rr->{constraint} eq 'require_one' or
Branch: 2 branches
BranchTrueFalseTotal
if (@fulfilled == 0 and $rr->{'constraint'} eq 'require_one' || $rr->{'constraint'} eq 'require_any') { } 4 2 6
elsif (@fulfilled > 1 and $rr->{'constraint'} eq 'allow_one' || $rr->{'constraint'} eq 'require_one') { } 2 0 2
Condition66%4/6
@fulfilled == 0 and $rr->{"constraint"} eq "require_one" || $rr->{"constraint"} eq "require_any"
!ll&&!rl&&r
204
$rr->{"constraint"} eq "require_one" or $rr->{"constraint"} eq "require_any"
l!l&&r!l&&!r
310
MC/DC33%1/3
@fulfilled == 0 and $rr->{"constraint"} eq "require_one" || $rr->{"constraint"} eq "require_any"
@fulfilled == 0 $rr->{"constraint"} eq "require_one" $rr->{"constraint"} eq "require_any"
Truth table75%3/4
A and B or C
A= @fulfilled == 0B= $rr->{"constraint"} eq "require_one"C= $rr->{"constraint"} eq "require_any"
ABCresult
0XX0
1000
11X1
1011
2379 $rr->{constraint} eq 'require_any' ) )
2380 {
2381 4 my (@missing, %found);
2382
2383 12 @missing = grep { unique($_, \%found) }
2384 4 map { @{$self->{RULESETS}{$_}{fulfill_order}} } @not_fulfilled;
2385
2386 4 ▶ my $msg = @missing == 1 ? 'ERR_REQ_SINGLE' : 'ERR_REQ_MULT';
Branch: 1 branch
BranchTrueFalseTotal
@missing == 1 ? : 0 4 4
2387 4 add_error_warn($vr, $rr, $msg, { param => \@missing });
2388 }
2389
2390 # For an 'allow_one' or 'require_one' rule, generate an error if
2391 # more than one of the rulesets was fulfilled.
2392
2393 ▶ elsif ( @fulfilled > 1 and ($rr->{constraint} eq 'allow_one' or
Condition33%2/6
@fulfilled > 1 and $rr->{"constraint"} eq "allow_one" || $rr->{"constraint"} eq "require_one"
!ll&&!rl&&r
002
$rr->{"constraint"} eq "allow_one" or $rr->{"constraint"} eq "require_one"
l!l&&r!l&&!r
020
MC/DC0%0/3
@fulfilled > 1 and $rr->{"constraint"} eq "allow_one" || $rr->{"constraint"} eq "require_one"
@fulfilled > 1 $rr->{"constraint"} eq "allow_one" $rr->{"constraint"} eq "require_one"
Truth table25%1/4
A and B or C
A= @fulfilled > 1B= $rr->{"constraint"} eq "allow_one"C= $rr->{"constraint"} eq "require_one"
ABCresult
0XX0
1000
11X1
1011
2394 $rr->{constraint} eq 'require_one') )
2395 {
2396 2 my @params;
2397 2 my ($label) = "A";
2398
2399 2 foreach my $rs ( @fulfilled )
2400 {
2401 4 push @params, "($label)"; $label++;
2402 4 push @params, @{$self->{RULESETS}{$rs}{fulfill_order}}
2403 4 ▶ if ref $self->{RULESETS}{$rs}{fulfill_order} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"RULESETS"}{$rs}{"fulfill_order"} eq "ARRAY" 4 0 4
2404 }
2405
2406 2 my $message = 'ERR_REQ_ONE';
2407
2408 2 add_error_warn($vr, $rr, 'ERR_REQ_ONE', { param => \@params });
2409 }
2410 }
2411
2412 # For a 'content_type' rule, we set the content type of the response
2413 # according to the given parameter.
2414
2415 elsif ( $type eq 'content_type' )
2416 {
2417 4 my $param = $rr->{param};
2418 4 ▶ my $value = $vr->{raw}{$param} || '';
Condition100%2/2
$vr->{'raw'}{$param} || ''
l!l
22
MC/DC100%1/1
$vr->{'raw'}{$param} || ''
$vr->{'raw'}{$param}
Truth table100%2/2
A
A= $vr->{'raw'}{$param}
Aresult
11
00
2419 4 ▶ my $clean_name = $rr->{key} || $rr->{param};
Condition33%1/3
$rr->{'key'} || $rr->{'param'}
l!l&&r!l&&!r
040
MC/DC0%0/2
$rr->{'key'} || $rr->{'param'}
$rr->{'key'} $rr->{'param'}
Truth table33%1/3
A || B
A= $rr->{'key'}B= $rr->{'param'}
ABresult
1X1
011
000
2420 4 my ($selected, $selected_type);
2421
2422 4 push @{$vr->{clean_list}}, $key;
2423
2424 4 ▶ if ( $rr->{type_map}{$value} )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{'type_map'}{$value}) { } 3 1 4
2425 {
2426 3 $vr->{content_type} = $rr->{type_map}{$value};
2427 3 $vr->{clean}{$clean_name} = $value;
2428 3 $vr->{ps}{$param} = 1;
2429 }
2430
2431 else
2432 {
2433 1 $vr->{content_type} = 'unknown';
2434 1 $vr->{clean}{$clean_name} = undef;
2435 1 $vr->{ps}{$param} = 1;
2436 1 ▶ $rr->{key} ||= '_content_type';
Condition50%1/2
$rr->{"key"} ||= "_content_type"
l!l
01
MC/DC0%0/1
$rr->{"key"} ||= "_content_type"
$rr->{"key"}
Truth table50%1/2
A
A= $rr->{"key"}
Aresult
11
00
2437 1 add_error_warn($vr, $rr, 'ERR_MEDIA_TYPE', { param => $param, value => $rr->{type_list} });
2438 }
2439 }
2440 }
2441 };
2442  
2443  
2444 # Helper function - given a hashref to use as a scratchpad, returns true the
2445 # first time a given argument is encountered and false each subsequent time.
2446 # This can be reset by calling it with a newly emptied scratchpad.
2447  
2448 sub unique {
2449
2450 14 ▶ my ($arg, $scratch) = @_;
Subroutine unique: called CC=2 SCAR=7.1
Pod undocumented
2451
2452 14 ▶ return if exists $scratch->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $scratch->{$arg} 0 14 14
2453 14 $scratch->{$arg} = 1;
2454 }
2455  
2456  
2457 # Add an error message to the current validation.
2458  
2459 sub add_error {
2460  
2461 34 ▶ my ($vr, $rr, $msg, $subst) = @_;
Subroutine add_error: called CC=9 SCAR=22.5
Pod undocumented
2462
2463 # If no message was given, use a default one. It's not a very good
2464 # message, but what can we do?
2465
2466 34 ▶ $msg ||= 'ERR_DEFAULT';
Condition50%1/2
$msg ||= "ERR_DEFAULT"
l!l
340
MC/DC0%0/1
$msg ||= "ERR_DEFAULT"
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
2467
2468 # If the given message starts with 'ERR_', assume it is an error code. If
2469 # the code is present as an attribute of the rule record, use the
2470 # corresponding value as the message. Otherwise, use the global value.
2471
2472 34 ▶ if ( $msg =~ qr{^ERR_} )
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ qr/^ERR_/) 17 17 34
2473 {
2474 17 ▶ $msg = $rr->{$msg} || $vr->{settings}{$msg} || $ERROR_MSG{$msg} || $ERROR_MSG{ERR_DEFAULT};
Condition66%6/9
$rr->{$msg} || $vr->{'settings'}{$msg} || $ERROR_MSG{$msg} || $ERROR_MSG{'ERR_DEFAULT'}
l!l&&r!l&&!r
1700
$rr->{$msg} or $vr->{"settings"}{$msg} or $ERROR_MSG{$msg}
l!l&&r!l&&!r
1340
$rr->{$msg} or $vr->{"settings"}{$msg}
l!l&&r!l&&!r
2114
MC/DC0%0/4
$rr->{$msg} || $vr->{'settings'}{$msg} || $ERROR_MSG{$msg} || $ERROR_MSG{'ERR_DEFAULT'}
$rr->{$msg} $vr->{"settings"}{$msg} $ERROR_MSG{$msg} $ERROR_MSG{'ERR_DEFAULT'}
Truth table60%3/5
A or B or C || D
A= $rr->{$msg}B= $vr->{"settings"}{$msg}C= $ERROR_MSG{$msg}D= $ERROR_MSG{'ERR_DEFAULT'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2475 }
2476
2477 # Next, figure out the error key. If the rule has a 'key' directive, use
2478 # that. Otherwise determine it according to the rule type, ruleset name,
2479 # and rule number.
2480
2481 my $err_key = $rr->{key} ? $rr->{key}
2482 : $rr->{type} eq 'param' ? $rr->{param}
2483 34 ▶ : $rr->{type} eq 'content_type' ? '_content_type'
Branch: 3 branches
BranchTrueFalseTotal
$rr->{'key'} ? : 2 32 34
$rr->{'type'} eq 'param' ? : 24 8 32
$rr->{'type'} eq 'content_type' ? : 0 8 8
2484 : "_$rr->{rs}{name}_$rr->{rn}";
2485
2486 # Record the error message under the key, and add the key to the error
2487 # list. Other rules might later remove or alter the error
2488 # message.
2489
2490 34 push @{$vr->{er}}, [$err_key, subst_error($msg, $subst)];
2491 34 $vr->{ec}{$err_key}++;
2492 }
2493  
2494  
2495 # Add a warning message to the current validation. The $subst hash if
2496 # given specifies placeholder substitutions.
2497  
2498 sub add_warning {
2499  
2500 11 ▶ my ($vr, $rr, $msg, $subst) = @_;
Subroutine add_warning: called CC=9 SCAR=24.8
Pod undocumented
2501
2502 # If no message was given, use a default one. It's not a very good
2503 # message, but what can we do?
2504
2505 11 ▶ $msg ||= 'ERR_DEFAULT';
Condition50%1/2
$msg ||= "ERR_DEFAULT"
l!l
110
MC/DC0%0/1
$msg ||= "ERR_DEFAULT"
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
2506
2507 # If the given message starts with 'ERR_', assume it is an error code. If
2508 # the code is present as an attribute of the rule record, use the
2509 # corresponding value as the message. Otherwise, use the global value.
2510
2511 11 ▶ if ( $msg =~ qr{^ERR_} )
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ qr/^ERR_/) 1 10 11
2512 {
2513 1 ▶ $msg = $rr->{$msg} || $vr->{settings}{$msg} || $ERROR_MSG{$msg} || $ERROR_MSG{ERR_DEFAULT};
Condition33%3/9
$rr->{$msg} || $vr->{'settings'}{$msg} || $ERROR_MSG{$msg} || $ERROR_MSG{'ERR_DEFAULT'}
l!l&&r!l&&!r
100
$rr->{$msg} or $vr->{"settings"}{$msg} or $ERROR_MSG{$msg}
l!l&&r!l&&!r
100
$rr->{$msg} or $vr->{"settings"}{$msg}
l!l&&r!l&&!r
010
MC/DC0%0/4
$rr->{$msg} || $vr->{'settings'}{$msg} || $ERROR_MSG{$msg} || $ERROR_MSG{'ERR_DEFAULT'}
$rr->{$msg} $vr->{"settings"}{$msg} $ERROR_MSG{$msg} $ERROR_MSG{'ERR_DEFAULT'}
Truth table20%1/5
A or B or C || D
A= $rr->{$msg}B= $vr->{"settings"}{$msg}C= $ERROR_MSG{$msg}D= $ERROR_MSG{'ERR_DEFAULT'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2514 }
2515
2516 # Next, figure out the warning key. If the rule has a 'key' directive, use
2517 # that. Otherwise determine it according to the rule type, ruleset name,
2518 # and rule number.
2519
2520 my $warn_key = $rr->{key} ? $rr->{key}
2521 : $rr->{type} eq 'param' ? $rr->{param}
2522 11 ▶ : $rr->{type} eq 'content_type' ? '_content_type'
Branch: 3 branches
BranchTrueFalseTotal
$rr->{'key'} ? : 0 11 11
$rr->{'type'} eq 'param' ? : 9 2 11
$rr->{'type'} eq 'content_type' ? : 0 2 2
2523 : "_$rr->{rs}{name}_$rr->{rn}";
2524
2525 # Record the warning message under the key. Other rules might later
2526 # alter the warning message if they use the same key.
2527
2528 11 push @{$vr->{wn}}, [$warn_key, subst_error($msg, $subst)];
2529 11 $vr->{wc}{$warn_key}++;
2530 }
2531  
2532  
2533 # Add an error or warning message to the current validation. If the rule has
2534 # a 'warn' attribute, add a warning. Otherwise, add an error. If the rule
2535 # has an 'errmsg' attribute, use its value instead of the error message given.
2536  
2537 sub add_error_warn {
2538
2539 11 ▶ my ($vr, $rr, $msg, $subst) = @_;
Subroutine add_error_warn: called CC=4 SCAR=13.9
Pod undocumented
2540
2541 11 ▶ $msg = $rr->{errmsg} if $rr->{errmsg};
Branch: 1 branch
BranchTrueFalseTotal
if $rr->{"errmsg"} 0 11 11
2542
2543 11 ▶ if ( $rr->{warn} )
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->{'warn'}) { } 2 9 11
2544 {
2545 2 ▶ $msg = $rr->{warn} if $rr->{warn} ne '1';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->{"warn"} ne 1 1 1 2
2546 2 return add_warning($vr, $rr, $msg, $subst);
2547 }
2548
2549 else
2550 {
2551 9 return add_error($vr, $rr, $msg, $subst);
2552 }
2553 }
2554  
2555  
2556 # Substitute placeholders in an error or warning message.
2557  
2558 sub subst_error {
2559  
2560 45 ▶ my ($message, $subst) = @_;
Subroutine subst_error: called CC=9 SCAR=26.0
Pod undocumented
2561
2562 45 ▶ while ( $message =~ /^(.*)\{(\w+)\}(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^(.*)\{(\w+)\}(.*)$/) 46 45 91
2563 {
2564 46 my $value = $subst->{$2};
2565
2566 46 ▶ if ( ref $value )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value) { } 42 4 46
elsif (defined $value and not $value =~ /^'/) { } 4 0 4
2567 {
2568 42 ▶ if ( reftype $value eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($value) eq 'ARRAY') { } 42 0 42
elsif (builtin::reftype($value) eq 'HASH') { } 0 0 0
2569 {
2570 42 $value = name_list(@$value);
2571 }
2572 elsif ( reftype $value eq 'HASH' )
2573 {
2574 0 $value = name_list(sort keys %$value);
2575 }
2576 }
2577
2578 ▶ elsif ( defined $value && $value !~ /^'/ )
Condition33%1/3
defined $value and not $value =~ /^'/
!ll&&!rl&&r
004
MC/DC0%0/2
defined $value and not $value =~ /^'/
defined $value not $value =~ /^'/
Truth table33%1/3
A and not(B)
A= defined $valueB= not $value =~ /^'/
ABresult
0X0
100
111
2579 {
2580 4 $value = "'$value'";
2581 }
2582
2583 else
2584 {
2585 0 $value = "''";
2586 }
2587
2588 46 ▶ $message = "$1$value$3" if defined $value and $value ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value ne "" 46 0 46
Condition33%1/3
defined $value and $value ne ""
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
2589 }
2590
2591 45 return $message;
2592 }
2593  
2594  
2595 # Generate a list of quoted strings from the specified values.
2596  
2597 sub name_list {
2598
2599 42 ▶ my @names = @_;
Subroutine name_list: called CC=2 SCAR=7.1
Pod undocumented
2600
2601 42 ▶ return unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 42 42
2602 42 return "'" . join("', '", @names) . "'";
2603 };
2604  
2605  
2606 package HTTP::Validate::Result;
2607  
2608 =head1 OTHER METHODS
2609  
2610 The result object returned by L</check_params> provides the following
2611 methods:
2612  
2613 =head3 passed
2614  
2615 Returns true if the validation passed, false otherwise.
2616  
2617 =cut
2618  
2619 sub passed {
2620
2621 7 ▶ my ($self) = @_;
Subroutine passed: called CC=3 SCAR=11.0
2622
2623 # If any errors occurred, then the validation failed.
2624
2625 7 ▶ return if ref $self->{er} eq 'ARRAY' && @{$self->{er}};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"er"} eq "ARRAY" and @{$self->{"er"};} 2 5 7
Condition66%2/3
ref $self->{"er"} eq "ARRAY" and @{$self->{"er"};}
!ll&&!rl&&r
502
MC/DC50%1/2
ref $self->{"er"} eq "ARRAY" and @{$self->{"er"};}
ref $self->{"er"} eq "ARRAY" @{$self->{"er"};}
Truth table66%2/3
A and B
A= ref $self->{"er"} eq "ARRAY"B= @{$self->{"er"};}
ABresult
0X0
100
111
2626
2627 # Otherwise, it passed.
2628
2629 5 return 1;
2630 }
2631  
2632  
2633 =head3 errors
2634  
2635 In a scalar context, this returns the number of errors generated by this
2636 validation. In a list context, it returns a list of error messages. If an
2637 argument is given, only messages whose key equals the argument are returned.
2638  
2639 =cut
2640  
2641 sub errors {
2642  
2643 50 ▶ my ($self, $key) = @_;
Subroutine errors: called CC=8 SCAR=20.8
2644
2645 # In scalar context, just return the count.
2646
2647 50 ▶ if ( ! wantarray )
Branch: 2 branches
BranchTrueFalseTotal
if (not wantarray) { } 20 30 50
elsif (defined $key) { } 4 26 30
2648 {
2649 20 ▶ return 0 unless defined $key ? ref $self->{ec} : ref $self->{er};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $key ? ref $self->{"ec"} : ref $self->{"er"} 12 8 20
defined $key ? : 4 16 20
2650 8 ▶ return defined $key ? ($self->{ec}{$key} || 0) : scalar @{$self->{er}};
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 3 5 8
Condition50%1/2
$self->{"ec"}{$key} or 0
l!l
30
MC/DC0%0/1
$self->{"ec"}{$key} or 0
$self->{"ec"}{$key}
Truth table50%1/2
A
A= $self->{"ec"}{$key}
Aresult
11
00
2651 }
2652
2653 # In list context, if a key is given then return just the matching error
2654 # messages or an empty list if there are none.
2655
2656 elsif ( defined $key )
2657 {
2658 4 ▶ return unless ref $self->{ec};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"ec"} 1 3 4
2659 3 return map { $_->[1] } grep { $_->[0] eq $key } @{$self->{er}};
2660 }
2661
2662 # If no key is given, just return all of the messages.
2663
2664 else
2665 {
2666 26 return map { $_->[1] } @{$self->{er}};
2667 }
2668 }
2669  
2670 =head3 error_keys
2671  
2672 Returns the list of keys for which error messages were generated.
2673  
2674 =cut
2675  
2676 sub error_keys {
2677
2678 6 ▶ my ($self) = @_;
Subroutine error_keys: called CC=1 SCAR=0.0
2679 6 return keys %{$self->{ec}};
2680 }
2681  
2682  
2683 =head3 warnings
2684  
2685 In a scalar context, this returns the number of warnings generated by the
2686 validation. In a list context, it returns a list of warning messages. If an
2687 argument is given, only messages whose key equals the argument are returned.
2688  
2689 =cut
2690  
2691 sub warnings {
2692  
2693 29 ▶ my ($self, $key) = @_;
Subroutine warnings: called CC=8 SCAR=20.8
2694
2695 # In scalar context, just return the count.
2696
2697 29 ▶ if ( ! wantarray )
Branch: 2 branches
BranchTrueFalseTotal
if (not wantarray) { } 18 11 29
elsif (defined $key) { } 2 9 11
2698 {
2699 18 ▶ return 0 unless defined $key ? ref $self->{wc} : ref $self->{wn};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $key ? ref $self->{"wc"} : ref $self->{"wn"} 12 6 18
defined $key ? : 2 16 18
2700 6 ▶ return defined $key ? ($self->{wc}{$key} || 0) : scalar @{$self->{wn}};
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 2 4 6
Condition50%1/2
$self->{"wc"}{$key} or 0
l!l
20
MC/DC0%0/1
$self->{"wc"}{$key} or 0
$self->{"wc"}{$key}
Truth table50%1/2
A
A= $self->{"wc"}{$key}
Aresult
11
00
2701 }
2702
2703 # In list context, if a key is given then return just the matching warning
2704 # messages or an empty list if there are none.
2705
2706 elsif ( defined $key )
2707 {
2708 2 ▶ return unless ref $self->{wn};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"wn"} 0 2 2
2709 2 return map { $_->[1] } grep { $_->[0] eq $key } @{$self->{wn}};
2710 }
2711
2712 # If no key is given, just return all of the messages.
2713
2714 else
2715 {
2716 9 return map { $_->[1] } @{$self->{wn}};
2717 }
2718 }
2719  
2720  
2721 =head3 warning_keys
2722  
2723 Returns the list of keys for which warning messages were generated.
2724  
2725 =cut
2726  
2727 sub warning_keys {
2728
2729 1 ▶ my ($self) = @_;
Subroutine warning_keys: called CC=1 SCAR=0.0
2730 1 return keys %{$self->{wc}};
2731 }
2732  
2733  
2734 =head3 keys
2735  
2736 In a scalar context, this returns the number of parameters that had valid values. In a list
2737 context, it returns a list of parameter names in the order they were recognized. Individual
2738 parameter values can be gotten by using either L</values> or L</value>.
2739  
2740 =cut
2741  
2742 sub keys {
2743  
2744 5 ▶ my ($self) = @_;
Subroutine keys: called CC=1 SCAR=0.0
2745
2746 # Return the list of parameter keys in the order they were recognized.
2747
2748 5 return @{$self->{clean_list}};
2749 }
2750  
2751  
2752 =head3 values
2753  
2754 Returns the hash of clean parameter values. This is not a copy, so any
2755 modifications you make to it will be reflected in subsequent calls to L</value>.
2756  
2757 =cut
2758  
2759 sub values {
2760
2761 2 ▶ my ($self) = @_;
Subroutine values: called CC=1 SCAR=0.0
2762
2763 # Return the clean value hash.
2764
2765 2 return $self->{clean};
2766 }
2767  
2768 =head3 value
2769  
2770 Returns the value of the specified parameter, or undef if that parameter was
2771 not specified in the request or if its value was invalid.
2772  
2773 =cut
2774  
2775 sub value {
2776  
2777 54 ▶ my ($self, $param) = @_;
Subroutine value: called CC=1 SCAR=0.0
2778
2779 54 return $self->{clean}{$param};
2780 }
2781  
2782  
2783 =head3 specified
2784  
2785 Returns true if the specified parameter was specified in the request with at least
2786 one value, whether or not that value was valid. Returns false otherwise.
2787  
2788 =cut
2789  
2790 sub specified {
2791
2792 5 ▶ my ($self, $param) = @_;
Subroutine specified: called CC=1 SCAR=0.0
2793
2794 5 return exists $self->{clean}{$param};
2795 }
2796  
2797  
2798 =head3 raw
2799  
2800 Returns a hash of the raw parameter values as originally provided to
2801 L</check_params>. Multiple values are represented by array refs. The
2802 result of this method can be used, for example, to redisplay a web form if the
2803 submission resulted in errors.
2804  
2805 =cut
2806  
2807 sub raw {
2808
2809 1 ▶ my ($self, $param) = @_;
Subroutine raw: called CC=1 SCAR=0.0
2810
2811 1 return $self->{raw};
2812 }
2813  
2814  
2815 =head3 content_type
2816  
2817 This returns the content type specified by the request parameters. If none
2818 was specified, or if no content_type rule was included in the validation, it
2819 returns undef.
2820  
2821 =cut
2822  
2823 sub content_type {
2824  
2825 3 ▶ my ($self) = @_;
Subroutine content_type: called CC=1 SCAR=0.0
2826
2827 3 return $self->{content_type};
2828 }
2829  
2830  
2831 package HTTP::Validate;
2832  
2833 # At the very end, we have the validator functions
2834 # ================================================
2835  
2836 =head1 VALIDATORS
2837  
2838 Parameter rules can each include one or more validator functions under the key
2839 C<valid>. The job of these functions is two-fold: first to check for good
2840 parameter values, and second to generate cleaned values.
2841  
2842 There are a number of validators provided by this module, or you can specify a
2843 reference to a function of your own.
2844  
2845 =head2 Predefined validators
2846  
2847 =head3 INT_VALUE
2848  
2849 This validator accepts any integer, and rejects all other values. It
2850 returns a numeric value, generated by adding 0 to the raw parameter value.
2851  
2852 =head3 INT_VALUE(min,max)
2853  
2854 This validator accepts any integer between C<min> and C<max> (inclusive). If either C<min>
2855 or C<max> is undefined, that bound will not be tested.
2856  
2857 =head3 POS_VALUE
2858  
2859 This is an alias for C<INT_VALUE(1)>.
2860  
2861 =head3 POS_ZERO_VALUE
2862  
2863 This is an alias for C<INT_VALUE(0)>.
2864  
2865 =cut
2866  
2867 sub int_value {
2868  
2869 43 ▶ my ($value, $context, $min, $max) = @_;
Subroutine int_value: called CC=10 SCAR=23.1
Pod undocumented
2870
2871 43 ▶ unless ( $value =~ /^([+-]?\d+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^([+-]?\d+)$/) 9 34 43
2872 {
2873 9 return { error => "bad value '$value' for {param}: must be an integer" };
2874 }
2875
2876 34 ▶ if ( defined $min and $value < $min )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min and $value < $min) 7 27 34
Condition100%3/3
defined $min and $value < $min
!ll&&!rl&&r
4237
MC/DC100%2/2
defined $min and $value < $min
defined $min $value < $min
Truth table100%3/3
A and B
A= defined $minB= $value < $min
ABresult
0X0
100
111
2877 {
2878 7 ▶ my $criterion = defined $max ? "between $min and $max"
Branch: 3 branches
BranchTrueFalseTotal
defined $max ? : 1 6 7
$min == 0 ? : 1 5 6
$min == 1 ? : 5 0 5
2879 : $min == 0 ? "nonnegative"
2880 : $min == 1 ? "positive"
2881 : "at least $min";
2882
2883 7 return { error => "bad value '$value' for {param}: must be $criterion" };
2884 }
2885
2886 27 ▶ if ( defined $max and $value > $max )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and $value > $max) 1 26 27
Condition100%3/3
defined $max and $value > $max
!ll&&!rl&&r
2331
MC/DC100%2/2
defined $max and $value > $max
defined $max $value > $max
Truth table100%3/3
A and B
A= defined $maxB= $value > $max
ABresult
0X0
100
111
2887 {
2888 1 ▶ my $criterion = defined $min ? "between $min and $max" : "at most $max";
Branch: 1 branch
BranchTrueFalseTotal
defined $min ? : 1 0 1
2889
2890 1 return { error => "bad value '$value' for {param} must be $criterion" };
2891 }
2892
2893 26 return { value => $value + 0 };
2894 }
2895  
2896 sub INT_VALUE {
2897
2898 15 ▶ my ($min, $max) = @_;
Subroutine INT_VALUE: called CC=7 SCAR=19.7
Pod documented
2899
2900 15 ▶ croak "lower bound must be an integer (was '$min')" unless !defined $min || $min =~ /^[+-]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $min or $min =~ /^[+-]?\d+$/ 1 14 15
Condition100%3/3
not defined $min or $min =~ /^[+-]?\d+$/
l!l&&r!l&&!r
861
MC/DC100%2/2
not defined $min or $min =~ /^[+-]?\d+$/
not defined $min $min =~ /^[+-]?\d+$/
Truth table100%3/3
not(A) or B
A= not defined $minB= $min =~ /^[+-]?\d+$/
ABresult
1X1
011
000
2901 14 ▶ croak "upper bound must be an integer (was '$max')" unless !defined $max || $max =~ /^[+-]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $max or $max =~ /^[+-]?\d+$/ 0 14 14
Condition66%2/3
not defined $max or $max =~ /^[+-]?\d+$/
l!l&&r!l&&!r
950
MC/DC0%0/2
not defined $max or $max =~ /^[+-]?\d+$/
not defined $max $max =~ /^[+-]?\d+$/
Truth table66%2/3
not(A) or B
A= not defined $maxB= $max =~ /^[+-]?\d+$/
ABresult
1X1
011
000
2902
2903 14 ▶ return \&int_value unless defined $min or defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min or defined $max 8 6 14
Condition66%2/3
defined $min or defined $max
l!l&&r!l&&!r
608
MC/DC50%1/2
defined $min or defined $max
defined $min defined $max
Truth table66%2/3
A or B
A= defined $minB= defined $max
ABresult
1X1
011
000
2904 6 ▶ return sub { return int_value(shift, shift, $min, $max) };
Subroutine __ANON__: called CC=1 SCAR=0.0
2905 };
2906  
2907 sub POS_VALUE {
2908
2909 17 ▶ return sub { return int_value(shift, shift, 1) };
Subroutine POS_VALUE: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Pod documented
2910 };
2911  
2912 sub POS_ZERO_VALUE {
2913  
2914 4 ▶ return sub { return int_value(shift, shift, 0) };
Subroutine POS_ZERO_VALUE: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Pod documented
2915 };
2916  
2917  
2918 =head3 DECI_VALUE
2919  
2920 This validator accepts any decimal number, including exponential notation, and
2921 rejects all other values. It returns a numeric value, generated by adding 0
2922 to the parameter value.
2923  
2924 =head3 DECI_VALUE(min,max)
2925  
2926 This validator accepts any real number between C<min> and C<max> (inclusive).
2927 Specify these bounds in quotes (i.e. as string arguments) if non-zero so that
2928 they will appear properly in error messages. If either C<min> or C<max> is
2929 undefined, that bound will not be tested.
2930  
2931 =cut
2932  
2933 sub deci_value {
2934
2935 14 ▶ my ($value, $context, $min, $max) = @_;
Subroutine deci_value: called CC=10 SCAR=23.9
Pod undocumented
2936
2937 14 ▶ unless ( $value =~ /^[+-]?(?:\d+\.\d*|\d*\.\d+|\d+)(?:[eE][+-]?\d+)?$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^[+-]?(?:\d+\.\d*|\d*\.\d+|\d+)(?:[eE][+-]?\d+)?$/) 1 13 14
2938 {
2939 1 return { error => "bad value '$value' for {param}: must be a decimal number" };
2940 }
2941
2942 13 ▶ if ( defined $min and defined $max and ($value < $min or $value > $max) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min and defined $max and $value < $min || $value > $max) 4 9 13
Condition88%8/9
defined $min and defined $max and $value < $min || $value > $max
!ll&&!rl&&r
571
defined $min and defined $max
!ll&&!rl&&r
508
$value < $min or $value > $max
l!l&&r!l&&!r
314
MC/DC50%2/4
defined $min and defined $max and $value < $min || $value > $max
defined $min defined $max $value < $min $value > $max
Truth table60%3/5
A and B and C or D
A= defined $minB= defined $maxC= $value < $minD= $value > $max
ABCDresult
0XXX0
10XX0
11000
111X1
11011
2943 {
2944 4 return { error => "bad value '$value' for {param}: must be between $min and $max" };
2945 }
2946
2947 9 ▶ if ( defined $min and $value < $min )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min and $value < $min) 0 9 9
Condition66%2/3
defined $min and $value < $min
!ll&&!rl&&r
540
MC/DC0%0/2
defined $min and $value < $min
defined $min $value < $min
Truth table66%2/3
A and B
A= defined $minB= $value < $min
ABresult
0X0
100
111
2948 {
2949 0 return { error => "bad value '$value' for {param}: must be at least $min" };
2950 }
2951
2952 9 ▶ if ( defined $max and $value > $max )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and $value > $max) 0 9 9
Condition66%2/3
defined $max and $value > $max
!ll&&!rl&&r
540
MC/DC0%0/2
defined $max and $value > $max
defined $max $value > $max
Truth table66%2/3
A and B
A= defined $maxB= $value > $max
ABresult
0X0
100
111
2953 {
2954 0 return { error => "bad value '$value' for {param}: must be at most $max" };
2955 }
2956
2957 9 return { value => $value + 0 };
2958 }
2959  
2960 sub DECI_VALUE {
2961
2962 15 ▶ my ($min, $max) = @_;
Subroutine DECI_VALUE: called CC=7 SCAR=19.7
Pod documented
2963
2964 15 ▶ croak "lower bound must be numeric" if defined $min && !looks_like_number($min);
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and not &looks_like_number($min) 1 14 15
Condition100%3/3
defined $min and not &looks_like_number($min)
!ll&&!rl&&r
861
MC/DC100%2/2
defined $min and not &looks_like_number($min)
defined $min not &looks_like_number($min)
Truth table100%3/3
A and not(B)
A= defined $minB= not &looks_like_number($min)
ABresult
0X0
100
111
2965 14 ▶ croak "upper bound must be numeric" if defined $max && !looks_like_number($max);
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and not &looks_like_number($max) 0 14 14
Condition66%2/3
defined $max and not &looks_like_number($max)
!ll&&!rl&&r
860
MC/DC0%0/2
defined $max and not &looks_like_number($max)
defined $max not &looks_like_number($max)
Truth table66%2/3
A and not(B)
A= defined $maxB= not &looks_like_number($max)
ABresult
0X0
100
111
2966
2967 14 ▶ return \&deci_value unless defined $min or defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min or defined $max 8 6 14
Condition66%2/3
defined $min or defined $max
l!l&&r!l&&!r
608
MC/DC50%1/2
defined $min or defined $max
defined $min defined $max
Truth table66%2/3
A or B
A= defined $minB= defined $max
ABresult
1X1
011
000
2968 6 ▶ return sub { return deci_value(shift, shift, $min, $max) };
Subroutine __ANON__: called CC=1 SCAR=0.0
2969 };
2970  
2971  
2972 =head3 MATCH_VALUE
2973  
2974 This validator accepts any string that matches the specified pattern, and
2975 rejects any that does not. If you specify the pattern as a string, it will be
2976 converted into a regexp and will have ^ prepended and $ appended, and also the
2977 modifier "i". If you specify the pattern using C<qr>, then it is used unchanged.
2978 Any rule that uses this validator should be provided with an error directive, since the
2979 default error message is by necessity not very informative. The value is not
2980 cleaned in any way.
2981  
2982 =cut
2983  
2984 sub match_value {
2985  
2986 7 ▶ my ($value, $context, $pattern) = @_;
Subroutine match_value: called CC=2 SCAR=6.9
Pod undocumented
2987
2988 7 ▶ return if $value =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$pattern/ 4 3 7
2989 3 return { error => "bad value '$value' for {param}: did not match the proper pattern" };
2990 }
2991  
2992 sub MATCH_VALUE {
2993  
2994 10 ▶ my ($pattern) = @_;
Subroutine MATCH_VALUE: called CC=5 SCAR=16.1
Pod documented
2995
2996 10 ▶ croak "MATCH_VALUE requires a regular expression" unless
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern and !ref($pattern) || ref $pattern eq "Regexp" 2 8 10
Condition100%6/6
defined $pattern and !ref($pattern) || ref $pattern eq "Regexp"
!ll&&!rl&&r
127
not ref $pattern or ref $pattern eq "Regexp"
l!l&&r!l&&!r
441
MC/DC66%2/3
defined $pattern and !ref($pattern) || ref $pattern eq "Regexp"
defined $pattern not ref $pattern ref $pattern eq "Regexp"
Truth table75%3/4
A and not(B) or C
A= defined $patternB= not ref $patternC= ref $pattern eq "Regexp"
ABCresult
0XX0
1000
11X1
1011
2997 defined $pattern && (!ref $pattern || ref $pattern eq 'Regexp');
2998
2999 8 ▶ my $re = ref $pattern ? $pattern : qr{^$pattern$}oi;
Branch: 1 branch
BranchTrueFalseTotal
ref $pattern ? : 4 4 8
3000
3001 8 ▶ return sub { return match_value(shift, shift, $re) };
Subroutine __ANON__: called CC=1 SCAR=0.0
3002 };
3003  
3004  
3005 =head3 ENUM_VALUE(string,...)
3006  
3007 This validator accepts any of the specified string values, and rejects all
3008 others. Comparisons are case insensitive. If the version of Perl is 5.016 or
3009 greater, or if the module C<Unicode::Casefold> is available and has been
3010 required, then the C<fc> function will be used instead of the usual C<lc> when
3011 comparing values. The cleaned value will be the matching string value from
3012 this call.
3013  
3014 If any of the strings is '#', then subsequent values will be accepted but not
3015 reported in the standard error message as allowable values. This allows for
3016 undocumented values to be accepted.
3017  
3018 =cut
3019  
3020 sub enum_value {
3021
3022 5 ▶ my ($value, $context, $accepted, $good_list) = @_;
Subroutine enum_value: called CC=2 SCAR=6.9
Pod undocumented
3023
3024 5 my $folded = $case_fold->($value);
3025
3026 # If the value is found in the $accepted hash, then we're good. Return
3027 # the value as originally given, not the case-folded version.
3028
3029 5 ▶ return { value => $accepted->{$folded} } if exists $accepted->{$folded};
Branch: 1 branch
BranchTrueFalseTotal
if exists $accepted->{$folded} 4 1 5
3030
3031 # Otherwise, then we have an error.
3032
3033 1 return { error => "bad value '$value' for {param}: must be one of $good_list" };
3034 }
3035  
3036 sub ENUM_VALUE {
3037
3038 5 ▶ my (%accepted, @documented, $undoc);
Subroutine ENUM_VALUE: called CC=7 SCAR=21.3
Pod documented
3039
3040 5 foreach my $k ( @_ )
3041 {
3042 9 ▶ next unless defined $k && $k ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and $k ne "" 0 9 9
Condition33%1/3
defined $k and $k ne ""
!ll&&!rl&&r
009
MC/DC0%0/2
defined $k and $k ne ""
defined $k $k ne ""
Truth table33%1/3
A and B
A= defined $kB= $k ne ""
ABresult
0X0
100
111
3043
3044 9 ▶ if ( $k eq '#' )
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "#") 0 9 9
3045 {
3046 0 $undoc = 1;
3047 0 next;
3048 }
3049
3050 9 $accepted{ $case_fold->($k) } = $k;
3051 9 ▶ push @documented, $k unless $undoc;
Branch: 1 branch
BranchTrueFalseTotal
unless $undoc 9 0 9
3052 }
3053
3054 #my @non_empty = grep { defined $_ && $_ ne '' } @_;
3055 5 ▶ croak "ENUM_VALUE requires at least one value" unless keys %accepted;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %accepted 1 4 5
3056
3057 # my %accepted = map { $case_fold->($_) => $_ } @non_empty;
3058 4 my $good_list = "'" . join("', '", @documented) . "'";
3059
3060 4 ▶ return sub { return enum_value(shift, shift, \%accepted, $good_list) };
Subroutine __ANON__: called CC=1 SCAR=0.0
3061 };
3062  
3063  
3064 =head3 BOOLEAN_VALUE
3065  
3066 This validator is used for parameters that take a true/false value. It
3067 accepts any of the following values: "yes", "no", "true", "false", "on",
3068 "off", "1", "0", compared case insensitively. It returns an error if any
3069 other value is specified. The cleaned value will be 1 or 0.
3070  
3071 =cut
3072  
3073 sub boolean_value {
3074  
3075 2 ▶ my ($value, $context) = @_;
Subroutine boolean_value: called CC=5 SCAR=21.7
Pod undocumented
3076
3077 2 ▶ if ( ref($value) =~ /boolean/i )
Branch: 2 branches
BranchTrueFalseTotal
if (ref($value) =~ /boolean/i) { } 0 2 2
elsif (not ref $value) { } 2 0 2
3078 {
3079 0 return { value => $value };
3080 }
3081
3082 elsif ( ! ref $value )
3083 {
3084 2 ▶ if ( $value =~ /^(?:1|yes|true|on)$/i )
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^(?:1|yes|true|on)$/i) { } 2 0 2
elsif ($value =~ /^(?:0|no|false|off)$/i) { } 0 0 0
3085 {
3086 2 return { value => 1 };
3087 }
3088
3089 elsif ( $value =~ /^(?:0|no|false|off)$/i )
3090 {
3091 0 return { value => 0 };
3092 }
3093 }
3094
3095 0 return { error => "the value of {param} must be one of: yes, no, true, false, on, off, 1, 0" };
3096 }
3097  
3098 1 ▶sub BOOLEAN_VALUE { return \&boolean_value; };
Subroutine BOOLEAN_VALUE: called CC=1 SCAR=0.0
Pod documented
3099  
3100  
3101 =head3 FLAG_VALUE
3102  
3103 This validator should be used for parameters that are considered to be "true"
3104 if present with an empty value. The validator returns a value of 1 in this case,
3105 and behaves like 'BOOLEAN_VALUE' otherwise.
3106  
3107 =cut
3108  
3109 2 ▶sub FLAG_VALUE { return 'FLAG_VALUE'; };
Subroutine FLAG_VALUE: called CC=1 SCAR=0.0
Pod documented
3110  
3111  
3112 # =head3 EMPTY_VALUE
3113  
3114 # This validator accepts only the empty value. You can use this when you want a
3115 # ruleset to be fulfilled even if the specified parameter is given an empty
3116 # value. This will typically be used along with at least one other validator for the
3117 # same parameter. For example:
3118  
3119 # define_ruleset foo =>
3120 # { param => 'bar', valid => [EMPTY_VALUE, POS_VALUE] };
3121  
3122 # This rule would be satisfied if the parameter 'bar' is given either an empty
3123 # value or a value that is a positive integer. The ruleset will be fulfilled in
3124 # either case, but will not be fulfilled if 'bar' is not mentioned at all. For
3125 # best results EMPTY_VALUE should not be the last validator in the list, because
3126 # if a value fails all of the validators then the last error message is reported
3127 # and its error message is by necessity not very helpful.
3128  
3129 # =cut
3130  
3131 # sub empty_value {
3132
3133 # my ($value, $context) = @_;
3134
3135 # return if !defined $value || $value eq '';
3136 # return { error => "parameter {param} must be empty unless it is given a valid value" };
3137 # }
3138  
3139 # sub EMPTY_VALUE {
3140  
3141 # return 'EMPTY_VALUE';
3142 # };
3143  
3144  
3145 =head3 ANY_VALUE
3146  
3147 This validator accepts any non-empty value. Using this validator
3148 is equivalent to not specifying any validator at all.
3149  
3150 =cut
3151  
3152 sub ANY_VALUE {
3153
3154 4 ▶ return 'ANY_VALUE';
Subroutine ANY_VALUE: called CC=1 SCAR=0.0
Pod documented
3155 };
3156  
3157  
3158 =head2 Reusing validators
3159  
3160 Every time you use a parametrized validator such as C<INT_VALUE(0,10)>, a new
3161 closure is generated. If you are repeating a particular set of parameters
3162 many times, to save space you may want to instantiate the validator just once:
3163  
3164 my $zero_to_ten = INT_VALUE(0,10);
3165
3166 define_ruleset( 'foo' =>
3167 { param => 'bar', valid => $zero_to_ten },
3168 { param => 'baz', valid => $zero_to_ten });
3169  
3170 =head2 Writing your own validator functions
3171  
3172 If you wish to validate parameters which do not match any of the validators
3173 described above, you can write your own validator function. Validator
3174 functions are called with two arguments:
3175  
3176 ($value, $context)
3177  
3178 Where $value is the raw parameter value and $context is a hash ref provided
3179 when the validation process is initiated (or an empty hashref if none is
3180 provided). This allows the passing of information such as database handles to
3181 the validator functions.
3182  
3183 If your function decides that the parameter value is valid and does not need
3184 to be cleaned, it can indicate this by returning an empty result.
3185  
3186 Otherwise, it must return a hash reference with one or more of the following
3187 keys:
3188  
3189 =over 4
3190  
3191 =item error
3192  
3193 If the parameter value is not valid, the value of this key should be an error
3194 message that states I<what a good value should look like>. This message should
3195 contain the placeholder {param}, which will be substituted with the parameter
3196 name. Use this placeholder, and do not hard-code the parameter name.
3197  
3198 Here is an example of a good message:
3199  
3200 "the value of {param} must be a positive integer (was {value})".
3201  
3202 Here is an example of a bad message:
3203  
3204 "bad value for 'foo'".
3205  
3206 =item warn
3207  
3208 If the parameter value is acceptable but questionable in some way, the value
3209 of this key should be a message that states what a good value should look
3210 like. All such messages will be made available through the result object that
3211 is returned by the validation routine. The code that handles the request may
3212 then choose to display these messages as part of the response. Your code may
3213 also make use of this information during the process of responding to the
3214 request.
3215  
3216 =item value
3217  
3218 If the parameter value represents anything other than a simple string (i.e. a
3219 number, list, or more complicated data structure), then the value of this key
3220 should be the converted or "cleaned" form of the parameter value. For
3221 example, a numeric parameter might be converted into an actual number by
3222 adding zero to it, or a pair of values might be split apart and converted into
3223 an array ref. The value of this key will be returned as the "cleaned" value
3224 of the parameter, in place of the raw parameter value provided in the request.
3225  
3226 =back
3227  
3228 =head3 Parametrized validators
3229  
3230 If you want to write your own parametrized validator, write a function that
3231 generates and returns a closure. For example:
3232  
3233 sub integer_multiple {
3234  
3235 my ($value, $context, $base) = @_;
3236
3237 return { value => $value + 0 } if $value % $base == 0;
3238 return { error => "the value of {param} must be a multiple of $base (was {value})" };
3239 }
3240
3241 sub INTEGER_MULTIPLE {
3242  
3243 my ($base) = $_[0] + 0;
3244
3245 croak "INTEGER_MULTIPLE requires a numeric parameter greater than zero"
3246 unless defined $base and $base > 0;
3247
3248 return sub { return integer_multiple(shift, shift, $base) };
3249 }
3250
3251 define_ruleset( 'foo' =>
3252 { param => foo, valid => INTEGER_MULTIPLE(3) });
3253  
3254 =cut
3255  
3256  
3257  
3258 =head1 AUTHOR
3259  
3260 Michael McClennen, C<< <mmcclenn at geology.wisc.edu> >>
3261  
3262 =head1 SUPPORT
3263  
3264 Please report any bugs or feature requests to C<bug-http-validate at rt.cpan.org>, or through
3265 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTTP-Validate>. I will be notified, and then you'll
3266 automatically be notified of progress on your bug as I make changes.
3267  
3268 =head1 LICENSE AND COPYRIGHT
3269  
3270 Copyright 2014 Michael McClennen.
3271  
3272 This program is free software; you can redistribute it and/or modify it
3273 under the terms of either: the GNU General Public License as published
3274 by the Free Software Foundation; or the Artistic License.
3275  
3276 See http://dev.perl.org/licenses/ for more information.
3277  
3278  
3279 =cut
3280  
3281 7 1; # End of HTTP::Validate
« blib/lib/HTTP/Validate.pm ↑ Summary Build »