blib/lib/Data/FormValidator/Constraints.pm

filter: Statementstmt 79.0% 279 / 353 Branchbran 50.6% 77 / 152 Conditioncond 43.2% 67 / 155 MC/DCmcdc 12.8% 10 / 78 Subroutinesub 74.4% 64 / 86 Podpod 59.3% 19 / 32 totaltotal 60.2% 516 / 856 CC 131 SCAR 23.2
CC
131
Cov
64%
match_cc_number
41.0
match_cc_exp
13.4
match_ip_address
13.4
CRAP 10.2
« blib/lib/Data/FormValidator.pm ↑ Summary blib/lib/Data/FormValidator/ConstraintsFactory.pm »
1 #
2 # Constraints.pm - Standard constraints for use in Data::FormValidator.
3 #
4 # This file is part of Data::FormValidator.
5 #
6 # Author: Francis J. Lacoste
7 # Maintainer: Mark Stosberg <mark@summersault.com>
8 #
9 # Copyright (C) 1999,2000 iNsu Innovations Inc.
10 # Copyright (C) 2001 Francis J. Lacoste
11 # Parts Copyright 1996-1999 by Michael J. Heins
12 # Parts Copyright 1996-1999 by Bruce Albrecht
13 #
14 # Parts of this module are based on work by
15 # Bruce Albrecht, contributed to MiniVend.
16 #
17 # Parts also based on work by Michael J. Heins
18 #
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms same terms as perl itself.
21 #
22 package Data::FormValidator::Constraints;
23 59 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 59 our $AUTOLOAD;
26  
27 59 our $VERSION = 4.88;
28  
29 59 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=20.2
30 59 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 59 my @closures = (qw/
32 american_phone
33 cc_exp
34 cc_number
35 cc_type
36 email
37 ip_address
38 phone
39 postcode
40 province
41 state
42 state_or_province
43 zip
44 zip_or_postcode/);
45  
46 # This be optimized with some of the voodoo that CGI.pm
47 # uses to AUTOLOAD dynamic functions.
48 59 for my $func (@closures) {
49 # cc_number is defined statically
50 767 ▶ unless ($func eq 'cc_number') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func eq "cc_number") 708 59 767
51 # Notice we have to escape some characters
52 # in the subroutine, which is really a string here.
53  
54 708 local $SIG{__DIE__} = \&confess;
55 708 my $code = qq!
56 sub $func {
57 return sub {
58 my \$dfv = shift;
59 use Scalar::Util ();
60 die "first arg to $func was not an object. Must be called as a constraint_method."
61 unless ( Scalar::Util::blessed(\$dfv) && \$dfv->can('name_this') );
62  
63 \$dfv->name_this('$func') unless \$dfv->get_current_constraint_name();
64 no strict 'refs';
65 return &{"match_$func"}(\@_);
66 }
67 }
68 !;
69  
70 0 ▶ eval "package Data::FormValidator::Constraints; $code";
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine __ANON__: not called CC=4 SCAR=21.1
Subroutine american_phone: not called CC=1 SCAR=2.3
Subroutine cc_exp: not called CC=1 SCAR=2.3
Subroutine cc_type: not called CC=1 SCAR=2.3
Subroutine email: called CC=1 SCAR=2.3
Subroutine ip_address: not called CC=1 SCAR=2.3
Subroutine phone: not called CC=1 SCAR=2.3
Subroutine postcode: not called CC=1 SCAR=2.3
Subroutine province: not called CC=1 SCAR=2.3
Subroutine state: not called CC=1 SCAR=2.3
Subroutine state_or_province: not called CC=1 SCAR=2.3
Subroutine zip: not called CC=1 SCAR=2.3
Subroutine zip_or_postcode: not called CC=1 SCAR=2.3
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 24 branches
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 1 1
unless $dfv->get_current_constraint_name 1 0 1
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
Condition2%1/36
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
001
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
71 708 ▶ die "couldn't create $func: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 708 708
72 }
73 }
74  
75 59 my @FVs = (qw/
76 FV_length_between
77 FV_min_length
78 FV_max_length
79 FV_eq_with
80 FV_num_values
81 FV_num_values_between
82 /);
83  
84 59 our @EXPORT_OK = (
85 @closures,
86 @FVs,
87 qw(
88 valid_american_phone
89 valid_cc_exp
90 valid_cc_number
91 valid_cc_type
92 valid_email
93 valid_ip_address
94 valid_phone
95 valid_postcode
96 valid_province
97 valid_state
98 valid_state_or_province
99 valid_zip
100 valid_zip_or_postcode
101 match_american_phone
102 match_cc_exp
103 match_cc_number
104 match_cc_type
105 match_email
106 match_ip_address
107 match_phone
108 match_postcode
109 match_province
110 match_state
111 match_state_or_province
112 match_zip
113 match_zip_or_postcode)
114 );
115  
116 59 our %EXPORT_TAGS = (
117 # regexp common is correctly empty here, because we handle the case on the fly with the import function below.
118 regexp_common => [],
119 closures => [ @closures, @FVs ],
120 validators => [qw/
121 valid_american_phone
122 valid_cc_exp
123 valid_cc_number
124 valid_cc_type
125 valid_email
126 valid_ip_address
127 valid_phone
128 valid_postcode
129 valid_province
130 valid_state
131 valid_state_or_province
132 valid_zip
133 valid_zip_or_postcode
134 /],
135 matchers => [qw/
136 match_american_phone
137 match_cc_exp
138 match_cc_number
139 match_cc_type
140 match_email
141 match_ip_address
142 match_phone
143 match_postcode
144 match_province
145 match_state
146 match_state_or_province
147 match_zip
148 match_zip_or_postcode
149 /],
150 );
151  
152 126 ▶ sub import {
Subroutine import: called CC=3 SCAR=11.0
153 # This is Regexp::Common support.
154 # Here we are handling cases that look like this:
155 #
156 # my_field => FV_foo_bar(-zoo=>'queue'),
157 126 ▶ if (grep { m/^:regexp_common$/ } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^:regexp_common$/;} @_) 1 125 126
158 1 require Regexp::Common;
159 1 import Regexp::Common 'RE_ALL';
160  
161 1 for my $sub (grep { m/^RE_/} keys %Data::FormValidator::Constraints:: ) {
162 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 173 my $new_name = $sub;
164 173 $new_name =~ s/^RE_/FV_/;
165 173 ▶ *{caller() . "::$new_name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
166 6 my @params = @_;
167 9 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
168 9 my $dfv = shift;
169 9 ▶ $dfv->name_this($new_name) unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 9 0 9
170  
171 59 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
172 9 my $re = &$sub(-keep=>1,@params);
173 9 my ($match) = ($dfv->get_current_constraint_value =~ qr/^($re)$/);
174 9 return $dfv->untainted_constraint_value($match);
175 }
176 6 }
177 173 }
178 }
179  
180 126 Data::FormValidator::Constraints->export_to_level(1,@_);
181 }
182  
183 }
184  
185  
186 # sub DESTROY {}
187  
188 =pod
189  
190 =head1 NAME
191  
192 Data::FormValidator::Constraints - Basic sets of constraints on input profile.
193  
194 =head1 SYNOPSIS
195  
196 use Data::FormValidator::Constraints qw(:closures);
197  
198 In an Data::FormValidator profile:
199  
200 constraint_methods => {
201 email => email(),
202 phone => american_phone(),
203 first_names => {
204 constraint_method => FV_max_length(3),
205 name => 'my_custom_name',
206 },
207 },
208 msgs => {
209 constraints => {
210 my_custom_name => 'My message',
211 },
212 },
213  
214  
215  
216 =head1 DESCRIPTION
217  
218 These are the builtin constraints that can be specified by name in the input
219 profiles.
220  
221 Be sure to check out the SEE ALSO section for even more pre-packaged
222 constraints you can use.
223  
224 =cut
225  
226 34 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.3
227 34 my $name = $AUTOLOAD;
228  
229 59 ▶ no strict qw/refs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 34 $name =~ m/^(.*::)(valid_|RE_)(.*)/;
232  
233 34 my ($pkg,$prefix,$sub) = ($1,$2,$3);
234  
235 #warn "hello! my ($pkg,$prefix,$sub) = ($1,$2,$3);";
236  
237 # Since all the valid_* routines are essentially identical we're
238 # going to generate them dynamically from match_ routines with the same names.
239 34 ▶ if ((defined $prefix) and ($prefix eq 'valid_')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix eq "valid_") 34 0 34
Condition33%1/3
defined $prefix and $prefix eq "valid_"
!ll&&!rl&&r
0034
MC/DC0%0/2
defined $prefix and $prefix eq "valid_"
defined $prefix $prefix eq "valid_"
Truth table33%1/3
A and B
A= defined $prefixB= $prefix eq "valid_"
ABresult
0X0
100
111
240 34 return defined &{$pkg.'match_' . $sub}(@_);
241 }
242 }
243  
244 =head2 FV_length_between(1,23)
245  
246 =head2 FV_max_length(23)
247  
248 =head2 FV_min_length(1)
249  
250 use Data::FormValidator::Constraints qw(
251 FV_length_between
252 FV_min_length
253 FV_max_length
254 );
255  
256 constraint_methods => {
257  
258 # specify a min and max, inclusive
259 last_name => FV_length_between(1,23),
260  
261 }
262  
263 Specify a length constraint for a field.
264  
265 These constraints have a different naming convention because they are higher-order
266 functions. They take input and return a code reference to a standard constraint
267 method. A constraint name of C<length_between>, C<min_length>, or C<max_length> will be set,
268 corresponding to the function name you choose.
269  
270 The checks are all inclusive, so a max length of '100' will allow the length 100.
271  
272 Length is measured in perl characters as opposed to bytes or anything else.
273  
274 This constraint I<will> untaint your data if you have untainting turned on. However,
275 a length check alone may not be enough to insure the safety of the data you are receiving.
276 Using additional constraints to check the data is encouraged.
277  
278 =cut
279  
280 8 ▶sub FV_length_between {
Subroutine FV_length_between: called CC=3 SCAR=11.2
Pod documented
281 8 my ($min,$max) = @_;
282 8 ▶ if (not (defined $min and defined $max)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min and defined $max) 0 8 8
Condition33%1/3
defined $min and defined $max
!ll&&!rl&&r
008
MC/DC0%0/2
defined $min and defined $max
defined $min defined $max
Truth table33%1/3
A and B
A= defined $minB= defined $max
ABresult
0X0
100
111
283 0 croak "min and max are required";
284 }
285 8 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
286 8 my ($dfv,$value) = @_;
287 8 ▶ $dfv->name_this('length_between') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 7 1 8
288 8 ▶ return undef if ( ( length($value) > $max ) || ( length($value) < $min) );
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max or length $value < $min 5 3 8
Condition100%3/3
length $value > $max or length $value < $min
l!l&&r!l&&!r
233
MC/DC100%2/2
length $value > $max or length $value < $min
length $value > $max length $value < $min
Truth table100%3/3
A or B
A= length $value > $maxB= length $value < $min
ABresult
1X1
011
000
289 # Use a regexp to untaint
290 3 $value=~/(.*)/s;
291 3 return $dfv->untainted_constraint_value($1);
292 }
293 8 }
294  
295 6 ▶sub FV_max_length {
Subroutine FV_max_length: called CC=2 SCAR=6.9
Pod documented
296 6 my ($max) = @_;
297 6 ▶ croak "max is required" unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 6 6
298 6 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
299 6 my ($dfv,$value) = @_;
300 6 ▶ $dfv->name_this('max_length') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 5 1 6
301 6 ▶ return undef if ( length($value) > $max );
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max 4 2 6
302 # Use a regexp to untaint
303 2 $value=~/(.*)/s;
304 2 return $dfv->untainted_constraint_value($1);
305 }
306 6 }
307  
308 5 ▶sub FV_min_length {
Subroutine FV_min_length: called CC=2 SCAR=7.0
Pod documented
309 5 my ($min) = @_;
310 5 ▶ croak "min is required" unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 5 5
311 4 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
312 4 my ($dfv,$value) = @_;
313 4 ▶ $dfv->name_this('min_length') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 4 0 4
314 4 ▶ return undef if ( length($value) < $min );
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $min 2 2 4
315 # Use a regexp to untaint
316 2 $value=~/(.*)/s;
317 2 return $dfv->untainted_constraint_value($1);
318 }
319 5 }
320  
321 =head2 FV_eq_with
322  
323 use Data::FormValidator::Constraints qw( FV_eq_with );
324  
325 constraint_methods => {
326 password => FV_eq_with('password_confirm'),
327 }
328  
329 Compares the current field to another field.
330 A constraint name of C<eq_with> will be set.
331  
332 =cut
333  
334 2 ▶sub FV_eq_with {
Subroutine FV_eq_with: called CC=1 SCAR=0.0
Pod documented
335 2 my ($other_field) = @_;
336 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
337 2 my $dfv = shift;
338 2 ▶ $dfv->name_this('eq_with') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 2 0 2
339  
340 2 my $curr_val = $dfv->get_current_constraint_value;
341  
342 2 my $data = $dfv->get_filtered_data;
343 # Sometimes the data comes through both ways...
344 2 ▶ my $other_val = (ref $data->{$other_field}) ? $data->{$other_field}[0] : $data->{$other_field};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$other_field} ? : 0 2 2
345  
346 2 return ($curr_val eq $other_val);
347 }
348  
349 2 }
350  
351 =head2 FV_num_values
352  
353 use Data::FormValidator::Constraints qw ( FV_num_values );
354  
355 constraint_methods => {
356 attachments => FV_num_values(4),
357 }
358  
359 Checks the number of values in the array named by this param.
360 Note that this is useful for making sure that only one value was passed for a
361 given param (by supplying a size argument of 1).
362 A constraint name of C<num_values> will be set.
363  
364 =cut
365  
366 2 ▶sub FV_num_values {
Subroutine FV_num_values: called CC=2 SCAR=8.1
Pod documented
367 2 ▶ my $size = shift || croak 'size argument is required';
Condition50%1/2
shift() || croak('size argument is required')
l!l
20
MC/DC0%0/1
shift() || croak('size argument is required')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
368 3 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=16.8
369 3 my $dfv = shift;
370 3 $dfv->name_this('num_values');
371 3 my $param = $dfv->get_current_constraint_field();
372 3 my $value = $dfv->get_filtered_data()->{$param};
373  
374 # If there's an arrayref of values provided, test the number of them found
375 # against the number of them of required
376 3 ▶ if (defined $value and ref $value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and ref $value eq 'ARRAY') { } 3 0 3
elsif ($size == 1) { } 0 0 0
Condition33%1/3
defined $value and ref $value eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and ref $value eq "ARRAY"
defined $value ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= defined $valueB= ref $value eq "ARRAY"
ABresult
0X0
100
111
377 3 my $num_values_found = scalar @$value;
378 3 return ($num_values_found == $size);
379 }
380 # If a size of 1 was requested, there was not an arrayref of values,
381 # there must be exactly one value.
382 elsif ($size == 1) {
383 0 return 1;
384 }
385 # Any other case is failure.
386 else {
387 0 return 0;
388 }
389 }
390 2 }
391  
392 =head2 FV_num_values_between
393  
394 use Data::FormValidator::Constraints qw ( FV_num_values_between );
395  
396 constraint_methods => {
397 attachments => FV_num_values_between(1,4),
398 }
399  
400 Checks that the number of values in the array named by this param is between
401 the supplied bounds (inclusively).
402 A constraint name of C<num_values_between> will be set.
403  
404 =cut
405  
406 2 ▶sub FV_num_values_between {
Subroutine FV_num_values_between: called CC=3 SCAR=13.2
Pod documented
407 2 my ($min, $max) = @_;
408 2 ▶ croak 'min and max arguments are required' unless $min && $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $min and $max 0 2 2
Condition33%1/3
$min and $max
!ll&&!rl&&r
002
MC/DC0%0/2
$min and $max
$min $max
Truth table33%1/3
A and B
A= $minB= $max
ABresult
0X0
100
111
409 2 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=22.4
410 2 my $dfv = shift;
411 2 $dfv->name_this('num_values_between');
412 2 my $param = $dfv->get_current_constraint_field();
413 2 my $value = $dfv->get_filtered_data()->{$param};
414  
415 2 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 2 0 2
416 2 my $num_values = scalar @$value;
417  
418 return(
419 (
420 2 ▶ $num_values >= $min
Branch: 1 branch
BranchTrueFalseTotal
$num_values >= $min && $num_values <= $max ? : 1 1 2
Condition66%2/3
$num_values >= $min and $num_values <= $max
!ll&&!rl&&r
101
MC/DC50%1/2
$num_values >= $min and $num_values <= $max
$num_values >= $min $num_values <= $max
Truth table66%2/3
A and B
A= $num_values >= $minB= $num_values <= $max
ABresult
0X0
100
111
421 && $num_values <= $max
422 ) ? 1 : 0
423 );
424 } else {
425 0 ▶ if ($min <= 1 && $max >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min <= 1 and $max >= 1) { } 0 0 0
Condition0%0/3
$min <= 1 and $max >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$min <= 1 and $max >= 1
$min <= 1 $max >= 1
Truth table0%0/3
A and B
A= $min <= 1B= $max >= 1
ABresult
0X0
100
111
426 # Single value is allowed
427 0 return 1;
428 } else {
429 0 return 0;
430 }
431 }
432 }
433 2 }
434  
435 =head2 email
436  
437 Checks if the email LOOKS LIKE an email address. This should be sufficient
438 99% of the time.
439  
440 Look elsewhere if you want something super fancy that matches every possible variation
441 that is valid in the RFC, or runs out and checks some MX records.
442  
443 =cut
444  
445 # Many of the following validators are taken from
446 # MiniVend 3.14. (http://www.minivend.com)
447 # Copyright 1996-1999 by Michael J. Heins <mike@heins.net>
448  
449 36 ▶sub match_email {
Subroutine match_email: called CC=3 SCAR=11.0
Pod undocumented
450 36 my $in_email = shift;
451  
452 36 require Email::Valid;
453 36 my $valid_email;
454  
455 # The extra check that the result matches the input prevents
456 # an address like this from being considered valid: Joe Smith <joe@smith.com>
457 36 ▶ if ( ($valid_email = Email::Valid->address($in_email) )
Branch: 1 branch
BranchTrueFalseTotal
if ($valid_email = 'Email::Valid'->address($in_email) and $valid_email eq $in_email) { } 10 26 36
Condition100%3/3
$valid_email = "Email::Valid"->address($in_email) and $valid_email eq $in_email
!ll&&!rl&&r
25110
MC/DC100%2/2
$valid_email = "Email::Valid"->address($in_email) and $valid_email eq $in_email
$valid_email = "Email::Valid"->address($in_email) $valid_email eq $in_email
Truth table100%3/3
A and B
A= $valid_email = "Email::Valid"->address($in_email)B= $valid_email eq $in_email
ABresult
0X0
100
111
458 and ($valid_email eq $in_email)) {
459 10 return $valid_email;
460 }
461 else {
462 26 return undef;
463 }
464 }
465  
466 59 my $state = <<EOF;
467 AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD
468 MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI
469 SC SD TN TX UT VT VA WA WV WI WY DC AP FP FPO APO GU VI
470 EOF
471  
472 59 my $province = <<EOF;
473 AB BC MB NB NF NL NS NT NU ON PE QC SK YT YK
474 EOF
475  
476 =head2 state_or_province
477  
478 This one checks if the input correspond to an american state or a canadian
479 province.
480  
481 =cut
482  
483 4 ▶sub match_state_or_province {
Subroutine match_state_or_province: called CC=2 SCAR=7.6
Pod undocumented
484 4 my $match;
485 4 ▶ if ($match = match_state(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match = match_state(@_)) { } 0 4 4
486 0 return $match;
487 }
488 else {
489 4 return match_province(@_);
490 }
491 }
492  
493 =head2 state
494  
495 This one checks if the input is a valid two letter abbreviation of an
496 American state.
497  
498 =cut
499  
500 11 ▶sub match_state {
Subroutine match_state: called CC=2 SCAR=6.9
Pod undocumented
501 11 my $val = shift;
502 11 ▶ if ($state =~ /\b($val)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /\b($val)\b/i) { } 2 9 11
503 2 return $1;
504 }
505 9 else { return undef; }
506 }
507  
508 =head2 province
509  
510 This checks if the input is a two letter Canadian province
511 abbreviation.
512  
513 =cut
514  
515 10 ▶sub match_province {
Subroutine match_province: called CC=2 SCAR=6.9
Pod undocumented
516 10 my $val = shift;
517 10 ▶ if ($province =~ /\b($val)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($province =~ /\b($val)\b/i) { } 4 6 10
518 4 return $1;
519 }
520 6 else { return undef; }
521 }
522  
523 =head2 zip_or_postcode
524  
525 This constraints checks if the input is an American zipcode or a
526 Canadian postal code.
527  
528 =cut
529  
530 4 ▶sub match_zip_or_postcode {
Subroutine match_zip_or_postcode: called CC=2 SCAR=6.9
Pod undocumented
531 4 my $match;
532 4 ▶ if ($match = match_zip(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match = match_zip(@_)) { } 2 2 4
533 2 return $match;
534 }
535 else {
536 2 return match_postcode(@_)
537 };
538 }
539 =pod
540  
541 =head2 postcode
542  
543 This constraints checks if the input is a valid Canadian postal code.
544  
545 =cut
546  
547 6 ▶sub match_postcode {
Subroutine match_postcode: called CC=2 SCAR=6.9
Pod undocumented
548 6 my $val = shift;
549 #$val =~ s/[_\W]+//g;
550 6 ▶ if ($val =~ /^([ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy][_\W]*\d[_\W]*[A-Za-z][_\W]*[- ]?[_\W]*\d[_\W]*[A-Za-z][_\W]*\d[_\W]*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy][_\W]*\d[_\W]*[A-Za-z][_\W]*[- ]?[_\W]*\d[_\W]*[A-Za-z][_\W]*\d[_\W]*)$/) { } 2 4 6
551 2 return $1;
552 }
553 4 else { return undef; }
554 }
555  
556 =head2 zip
557  
558 This input validator checks if the input is a valid american zipcode :
559 5 digits followed by an optional mailbox number.
560  
561 =cut
562  
563 12 ▶sub match_zip {
Subroutine match_zip: called CC=2 SCAR=6.9
Pod undocumented
564 12 my $val = shift;
565 12 ▶ if ($val =~ /^(\s*\d{5}(?:[-]\d{4})?\s*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\s*\d{5}(?:[-]\d{4})?\s*)$/) { } 6 6 12
566 6 return $1;
567 }
568 6 else { return undef; }
569 }
570  
571 =head2 phone
572  
573 This one checks if the input looks like a phone number, (if it
574 contains at least 6 digits.)
575  
576 =cut
577  
578 6 ▶sub match_phone {
Subroutine match_phone: called CC=2 SCAR=6.9
Pod undocumented
579 6 my $val = shift;
580  
581 6 ▶ if ($val =~ /^((?:\D*\d\D*){6,})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^((?:\D*\d\D*){6,})$/) { } 4 2 6
582 4 return $1;
583 }
584 2 else { return undef; }
585 }
586  
587 =head2 american_phone
588  
589 This constraints checks if the number is a possible North American style
590 of phone number : (XXX) XXX-XXXX. It has to contains 7 or more digits.
591  
592 =cut
593  
594 5 ▶sub match_american_phone {
Subroutine match_american_phone: called CC=2 SCAR=6.9
Pod undocumented
595 5 my $val = shift;
596  
597 5 ▶ if ($val =~ /^((?:\D*\d\D*){7,})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^((?:\D*\d\D*){7,})$/) { } 2 3 5
598 2 return $1;
599 }
600 3 else { return undef; }
601 }
602  
603  
604 =head2 cc_number
605  
606 This constraint references the value of a credit card type field.
607  
608 constraint_methods => {
609 cc_no => cc_number({fields => ['cc_type']}),
610 }
611  
612  
613 The number is checked only for plausibility, it checks if the number could
614 be valid for a type of card by checking the checksum and looking at the number
615 of digits and the number of digits of the number.
616  
617 This functions is only good at catching typos. IT DOESN'T
618 CHECK IF THERE IS AN ACCOUNT ASSOCIATED WITH THE NUMBER.
619  
620 =cut
621  
622 # This one is taken from the contributed program to
623 # MiniVend by Bruce Albrecht
624  
625 # XXX raise exception on bad/missing params?
626 1 ▶sub cc_number {
Subroutine cc_number: called CC=6 SCAR=20.5
Pod documented
627 1 my $attrs = shift;
628 return undef unless $attrs && ref($attrs) eq 'HASH'
629 1 ▶ && exists $attrs->{fields} && ref($attrs->{fields}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"} and ref $attrs->{"fields"} eq "ARRAY" 0 1 1
Condition33%3/9
$attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"} and ref $attrs->{"fields"} eq "ARRAY"
!ll&&!rl&&r
001
$attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"}
!ll&&!rl&&r
001
$attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/4
$attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"} and ref $attrs->{"fields"} eq "ARRAY"
$attrs ref $attrs eq "HASH" exists $attrs->{"fields"} ref $attrs->{"fields"} eq "ARRAY"
Truth table20%1/5
A and B and C and D
A= $attrsB= ref $attrs eq "HASH"C= exists $attrs->{"fields"}D= ref $attrs->{"fields"} eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
630  
631 1 my ($cc_type_field) = @{ $attrs->{fields} };
632 1 ▶ return undef unless $cc_type_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $cc_type_field 0 1 1
633  
634 12 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
635 12 my $dfv = shift;
636 12 my $data = $dfv->get_filtered_data;
637  
638 return match_cc_number(
639 $dfv->get_current_constraint_value,
640 12 $data->{$cc_type_field}
641 );
642 1 };
643 }
644  
645 28 ▶sub match_cc_number {
Subroutine match_cc_number: called CC=29 SCAR=37.1
Pod undocumented
646 28 my ( $the_card, $card_type ) = @_;
647 28 my $orig_card = $the_card; #used for return match at bottom
648 28 my ($index, $digit, $product);
649 28 my $multiplier = 2; # multiplier is either 1 or 2
650 28 my $the_sum = 0;
651  
652 28 ▶ return undef if length($the_card) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $the_card == 0 0 28 28
653  
654 # check card type
655 28 ▶ return undef unless $card_type =~ /^[admv]/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $card_type =~ /^[admv]/i 0 28 28
656  
657 28 ▶ return undef if ($card_type =~ /^v/i && substr($the_card, 0, 1) ne "4") ||
Branch: 1 branch
BranchTrueFalseTotal
if $card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 and substr($the_card, 0, 1) ne 2 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011 or $card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37 4 24 28
Condition77%21/27
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 and substr($the_card, 0, 1) ne 2 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011 or $card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37
l!l&&r!l&&!r
4024
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 and substr($the_card, 0, 1) ne 2 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011
l!l&&r!l&&!r
2224
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 and substr($the_card, 0, 1) ne 2
l!l&&r!l&&!r
0226
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4
!ll&&!rl&&r
2080
$card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 and substr($the_card, 0, 1) ne 2
!ll&&!rl&&r
2602
$card_type =~ /^m/i and substr($the_card, 0, 1) ne 5
!ll&&!rl&&r
2062
$card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011
!ll&&!rl&&r
2042
$card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37
!ll&&!rl&&r
1860
$card_type =~ /^a/i and substr($the_card, 0, 2) ne 34
!ll&&!rl&&r
1806
MC/DC40%4/10
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 and substr($the_card, 0, 1) ne 2 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011 or $card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37
$card_type =~ /^v/i substr($the_card, 0, 1) ne 4 $card_type =~ /^m/i substr($the_card, 0, 1) ne 5 substr($the_card, 0, 1) ne 2 $card_type =~ /^d/i substr($the_card, 0, 4) ne 6011 $card_type =~ /^a/i substr($the_card, 0, 2) ne 34 substr($the_card, 0, 2) ne 37
Truth table10%6/57
A and B or C and D and E or F and G or H and I and J
A= $card_type =~ /^v/iB= substr($the_card, 0, 1) ne 4C= $card_type =~ /^m/iD= substr($the_card, 0, 1) ne 5E= substr($the_card, 0, 1) ne 2F= $card_type =~ /^d/iG= substr($the_card, 0, 4) ne 6011H= $card_type =~ /^a/iI= substr($the_card, 0, 2) ne 34J= substr($the_card, 0, 2) ne 37
ABCDEFGHIJresult
11XXXXXXXX1
0X111XXXXX1
10111XXXXX1
0X0XX11XXX1
0X10X11XXX1
0X11011XXX1
100XX11XXX1
1010X11XXX1
1011011XXX1
0X0XX0X1111
0X0XX101111
0X10X0X1111
0X10X101111
0X1100X1111
0X110101111
100XX0X1111
100XX101111
1010X0X1111
1010X101111
101100X1111
10110101111
0X0XX0X0XX0
0X0XX0X10X0
0X0XX0X1100
0X0XX100XX0
0X0XX1010X0
0X0XX101100
0X10X0X0XX0
0X10X0X10X0
0X10X0X1100
0X10X100XX0
0X10X1010X0
0X10X101100
0X1100X0XX0
0X1100X10X0
0X1100X1100
0X110100XX0
0X1101010X0
0X110101100
100XX0X0XX0
100XX0X10X0
100XX0X1100
100XX100XX0
100XX1010X0
100XX101100
1010X0X0XX0
1010X0X10X0
1010X0X1100
1010X100XX0
1010X1010X0
1010X101100
101100X0XX0
101100X10X0
101100X1100
10110100XX0
101101010X0
10110101100
658 ($card_type =~ /^m/i && substr($the_card, 0, 1) ne "5" &&
659 substr($the_card, 0, 1) ne "2") ||
660 ($card_type =~ /^d/i && substr($the_card, 0, 4) ne "6011") ||
661 ($card_type =~ /^a/i && substr($the_card, 0, 2) ne "34" &&
662 substr($the_card, 0, 2) ne "37");
663  
664 # check for valid number of digits.
665 24 $the_card =~ s/\s//g; # strip out spaces
666 24 ▶ return undef if $the_card !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $the_card =~ /^\d+$/ 0 24 24
667  
668 24 $digit = substr($the_card, 0, 1);
669 24 $index = length($the_card)-1;
670 24 ▶ return undef if ($digit == 3 && $index != 14) ||
Branch: 1 branch
BranchTrueFalseTotal
if $digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15 or $digit == 6 and $index != 13 and $index != 15 0 24 24
Condition55%15/27
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15 or $digit == 6 and $index != 13 and $index != 15
l!l&&r!l&&!r
0024
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15
l!l&&r!l&&!r
0024
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15
l!l&&r!l&&!r
0024
$digit == 3 and $index != 14
!ll&&!rl&&r
1860
$digit == 4 and $index != 12 and $index != 15
!ll&&!rl&&r
1680
$digit == 4 and $index != 12
!ll&&!rl&&r
1608
$digit == 5 and $index != 15
!ll&&!rl&&r
1860
$digit == 6 and $index != 13 and $index != 15
!ll&&!rl&&r
2040
$digit == 6 and $index != 13
!ll&&!rl&&r
2004
MC/DC0%0/10
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15 or $digit == 6 and $index != 13 and $index != 15
$digit == 3 $index != 14 $digit == 4 $index != 12 $index != 15 $digit == 5 $index != 15 $digit == 6 $index != 13 $index != 15
Truth table7%4/57
A and B or C and D and E or F and G or H and I and J
A= $digit == 3B= $index != 14C= $digit == 4D= $index != 12E= $index != 15F= $digit == 5G= $index != 15H= $digit == 6I= $index != 13J= $index != 15
ABCDEFGHIJresult
11XXXXXXXX1
0X111XXXXX1
10111XXXXX1
0X0XX11XXX1
0X10X11XXX1
0X11011XXX1
100XX11XXX1
1010X11XXX1
1011011XXX1
0X0XX0X1111
0X0XX101111
0X10X0X1111
0X10X101111
0X1100X1111
0X110101111
100XX0X1111
100XX101111
1010X0X1111
1010X101111
101100X1111
10110101111
0X0XX0X0XX0
0X0XX0X10X0
0X0XX0X1100
0X0XX100XX0
0X0XX1010X0
0X0XX101100
0X10X0X0XX0
0X10X0X10X0
0X10X0X1100
0X10X100XX0
0X10X1010X0
0X10X101100
0X1100X0XX0
0X1100X10X0
0X1100X1100
0X110100XX0
0X1101010X0
0X110101100
100XX0X0XX0
100XX0X10X0
100XX0X1100
100XX100XX0
100XX1010X0
100XX101100
1010X0X0XX0
1010X0X10X0
1010X0X1100
1010X100XX0
1010X1010X0
1010X101100
101100X0XX0
101100X10X0
101100X1100
10110100XX0
101101010X0
10110101100
671 ($digit == 4 && $index != 12 && $index != 15) ||
672 ($digit == 5 && $index != 15) ||
673 ($digit == 6 && $index != 13 && $index != 15);
674  
675  
676 # calculate checksum.
677 24 ▶ for ($index--; $index >= 0; $index --)
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 354 24 378
678 {
679 354 $digit=substr($the_card, $index, 1);
680 354 $product = $multiplier * $digit;
681 354 ▶ $the_sum += $product > 9 ? $product - 9 : $product;
Branch: 1 branch
BranchTrueFalseTotal
$product > 9 ? : 50 304 354
682 354 $multiplier = 3 - $multiplier;
683 }
684 24 $the_sum %= 10;
685 24 ▶ $the_sum = 10 - $the_sum if $the_sum;
Branch: 1 branch
BranchTrueFalseTotal
if $the_sum 22 2 24
686  
687 # return whether checksum matched.
688 24 ▶ if ($the_sum == substr($the_card, -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($the_sum == substr($the_card, -1)) { } 18 6 24
689 18 ▶ if ($orig_card =~ /^([\d\s]*)$/) { return $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_card =~ /^([\d\s]*)$/) { } 18 0 18
690 0 else { return undef; }
691 }
692 else {
693 6 return undef;
694 }
695 }
696  
697 =head2 cc_exp
698  
699 This one checks if the input is in the format MM/YY or MM/YYYY and if
700 the MM part is a valid month (1-12) and if that date is not in the past.
701  
702 =cut
703  
704 4 ▶sub match_cc_exp {
Subroutine match_cc_exp: called CC=10 SCAR=25.9
Pod undocumented
705 4 my $val = shift;
706 4 my ($matched_month, $matched_year);
707  
708 4 my ($month, $year) = split('/', $val);
709 4 ▶ return undef if $month !~ /^(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $month =~ /^(\d+)$/ 2 2 4
710 2 $matched_month = $1;
711  
712 2 ▶ return undef if $year !~ /^(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $year =~ /^(\d+)$/ 0 2 2
713 2 $matched_year = $1;
714  
715 2 ▶ return undef if $month <1 || $month > 12;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 0 2 2
Condition33%1/3
$month < 1 or $month > 12
l!l&&r!l&&!r
002
MC/DC0%0/2
$month < 1 or $month > 12
$month < 1 $month > 12
Truth table33%1/3
A or B
A= $month < 1B= $month > 12
ABresult
1X1
011
000
716 2 ▶ $year += ($year < 70) ? 2000 : 1900 if $year < 1900;
Branch: 2 branches
BranchTrueFalseTotal
if $year < 1900 2 0 2
$year < 70 ? : 2 0 2
717 2 my @now=localtime();
718 2 $now[5] += 1900;
719 2 ▶ return undef if ($year < $now[5]) || ($year == $now[5] && $month <= $now[4]);
Branch: 1 branch
BranchTrueFalseTotal
if $year < $now[5] or $year == $now[5] and $month <= $now[4] 0 2 2
Condition33%2/6
$year < $now[5] or $year == $now[5] and $month <= $now[4]
l!l&&r!l&&!r
002
$year == $now[5] and $month <= $now[4]
!ll&&!rl&&r
200
MC/DC0%0/3
$year < $now[5] or $year == $now[5] and $month <= $now[4]
$year < $now[5] $year == $now[5] $month <= $now[4]
Truth table25%1/4
A or B and C
A= $year < $now[5]B= $year == $now[5]C= $month <= $now[4]
ABCresult
1XX1
0111
00X0
0100
720  
721 2 return "$matched_month/$matched_year";
722 }
723  
724 =head2 cc_type
725  
726 This one checks if the input field starts by M(asterCard), V(isa),
727 A(merican express) or D(iscovery).
728  
729 =cut
730  
731 4 ▶sub match_cc_type {
Subroutine match_cc_type: called CC=2 SCAR=6.9
Pod undocumented
732 4 my $val = shift;
733 4 ▶ if ($val =~ /^([MVAD].*)$/i) { return $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([MVAD].*)$/i) { } 2 2 4
734 2 else { return undef; }
735 }
736  
737 =head2 ip_address
738  
739 This checks if the input is formatted like a dotted decimal IP address (v4).
740 For other kinds of IP address method, See L<Regexp::Common::net> which provides
741 several more options. L<REGEXP::COMMON SUPPORT> explains how we easily integrate
742 with Regexp::Common.
743  
744 =cut
745  
746 # contributed by Juan Jose Natera Abreu <jnatera@net-uno.net>
747  
748 6 ▶sub match_ip_address {
Subroutine match_ip_address: called CC=10 SCAR=25.9
Pod undocumented
749 6 my $val = shift;
750 6 ▶ if ($val =~ m/^((\d+)\.(\d+)\.(\d+)\.(\d+))$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^((\d+)\.(\d+)\.(\d+)\.(\d+))$/) { } 4 2 6
751 4 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if ($2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255) and ($5 >= 0 and $5 <= 255)) { } 3 1 4
Condition52%11/21
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255) and ($5 >= 0 and $5 <= 255)
!ll&&!rl&&r
103
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255)
!ll&&!rl&&r
103
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255)
!ll&&!rl&&r
103
$2 >= 0 and $2 <= 255
!ll&&!rl&&r
013
$3 >= 0 and $3 <= 255
!ll&&!rl&&r
003
$4 >= 0 and $4 <= 255
!ll&&!rl&&r
003
$5 >= 0 and $5 <= 255
!ll&&!rl&&r
003
MC/DC12%1/8
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255) and ($5 >= 0 and $5 <= 255)
$2 >= 0 $2 <= 255 $3 >= 0 $3 <= 255 $4 >= 0 $4 <= 255 $5 >= 0 $5 <= 255
Truth table22%2/9
A and B and C and D and E and F and G and H
A= $2 >= 0B= $2 <= 255C= $3 >= 0D= $3 <= 255E= $4 >= 0F= $4 <= 255G= $5 >= 0H= $5 <= 255
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
752 (($2 >= 0 && $2 <= 255) && ($3 >= 0 && $3 <= 255) && ($4 >= 0 && $4 <= 255) && ($5 >= 0 && $5 <= 255)) {
753 3 return $1;
754 }
755 1 else { return undef; }
756 }
757 2 else { return undef; }
758 }
759  
760  
761 59 1;
762  
763 __END__
« blib/lib/Data/FormValidator.pm ↑ Summary blib/lib/Data/FormValidator/ConstraintsFactory.pm »