Ex/Validate.pm

filter: Statementstmt 73.7% 505 / 685 Branchbran 57.7% 386 / 668 Conditioncond 43.8% 142 / 324 MC/DCmcdc 26.9% 48 / 178 Subroutinesub 91.3% 21 / 23 Podpod 58.3% 7 / 12 totaltotal 58.6% 1109 / 1890 CC 490 SCAR 65.9
CC
490
Cov
62%
generate_form
3660.0
check_type
745.5
validate_buddy
579.2
CRAP 731.0
↑ Summary Ex/Die.pm »
1 package CGI::Ex::Validate;
2  
3 ###---------------------###
4 # See the perldoc in CGI/Ex/Validate.pod
5 # Copyright - Paul Seamons
6 # Distributed under the Perl Artistic License without warranty
7  
8 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $VERSION = '2.54'; # VERSION
13 9 our $QR_EXTRA = qr/^(\w+_error|as_(array|string|hash)_\w+|no_\w+)/;
14 9 our @UNSUPPORTED_BROWSERS = (qr/MSIE\s+5.0\d/i);
15 9 our $JS_URI_PATH;
16 9 our $JS_URI_PATH_VALIDATE;
17  
18 sub new {
19 226 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
20 226 ▶ return bless ref($_[0]) ? shift : {@_}, $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 199 27 226
21 }
22  
23 2 ▶sub cgix { shift->{'cgix'} ||= do { require CGI::Ex; CGI::Ex->new } }
Subroutine cgix: called CC=2 SCAR=7.6
Pod documented
Condition33%1/3
+(shift())->{"cgix"} ||= do { require CGI::Ex; "CGI::Ex"->new }
l!l&&r!l&&!r
020
MC/DC0%0/2
+(shift())->{"cgix"} ||= do { require CGI::Ex; "CGI::Ex"->new }
+(shift())->{"cgix"} do { require CGI::Ex; "CGI::Ex"->new }
Truth table33%1/3
A ||= B
A= +(shift())->{"cgix"}B= do { require CGI::Ex; "CGI::Ex"->new }
ABresult
1X1
011
000
24  
25 sub validate {
26 221 ▶ my $self = (! ref($_[0])) ? shift->new # $class->validate
Subroutine validate: called CC=38 SCAR=39.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
!ref($_[0]) ? : 0 221 221
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 194 27 221
27 : UNIVERSAL::isa($_[0], __PACKAGE__) ? shift # $self->validate
28 : __PACKAGE__->new; # CGI::Ex::Validate::validate
29 221 my ($form, $val_hash, $what_was_validated) = @_;
30  
31 221 ▶ die "Invalid form hash or cgi object" if ! $form || ! ref $form;
Branch: 1 branch
BranchTrueFalseTotal
unless $form and ref $form 0 221 221
Condition33%1/3
$form and ref $form
!ll&&!rl&&r
00221
MC/DC0%0/2
$form and ref $form
$form ref $form
Truth table33%1/3
A and B
A= $formB= ref $form
ABresult
0X0
100
111
32 221 ▶ $form = $self->cgix->get_form($form) if ref $form ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $form ne "HASH" 2 219 221
33  
34 221 my ($fields, $ARGS) = $self->get_ordered_fields($val_hash);
35 220 ▶ return if ! @$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @$fields 1 219 220
36  
37 219 ▶ return if $ARGS->{'validate_if'} && ! $self->check_conditional($form, $ARGS->{'validate_if'});
Branch: 1 branch
BranchTrueFalseTotal
if $ARGS->{"validate_if"} and not $self->check_conditional($form, $ARGS->{"validate_if"}) 4 215 219
Condition100%3/3
$ARGS->{"validate_if"} and not $self->check_conditional($form, $ARGS->{"validate_if"})
!ll&&!rl&&r
21144
MC/DC100%2/2
$ARGS->{"validate_if"} and not $self->check_conditional($form, $ARGS->{"validate_if"})
$ARGS->{"validate_if"} not $self->check_conditional($form, $ARGS->{"validate_if"})
Truth table100%3/3
A and not(B)
A= $ARGS->{"validate_if"}B= not $self->check_conditional($form, $ARGS->{"validate_if"})
ABresult
0X0
100
111
38  
39 # Finally we have our arrayref of hashrefs that each have their 'field' key
40 # now lets do the validation
41 215 $self->{'was_checked'} = {};
42 215 $self->{'was_valid'} = {};
43 215 $self->{'had_error'} = {};
44 215 my $found = 1;
45 215 my @errors;
46 my $hold_error; # hold the error for a moment - to allow for an "OR" operation
47 215 my %checked;
48 215 ▶ foreach (my $i = 0; $i < @$fields; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$fields) 237 215 452
49 237 my $ref = $fields->[$i];
50 237 ▶ if (! ref($ref) && $ref eq 'OR') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $ref and $ref eq "OR") 0 237 237
Condition33%1/3
not ref $ref and $ref eq "OR"
!ll&&!rl&&r
23700
MC/DC0%0/2
not ref $ref and $ref eq "OR"
not ref $ref $ref eq "OR"
Truth table33%1/3
not(A) and B
A= not ref $refB= $ref eq "OR"
ABresult
0X0
100
111
51 0 ▶ $i++ if $found; # if found skip the OR altogether
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
52 0 $found = 1; # reset
53 0 next;
54 }
55 237 $found = 1;
56 237 ▶ my $key = $ref->{'field'} || die "Missing field key during normal validation";
Condition50%1/2
$ref->{'field'} || die('Missing field key during normal validation')
l!l
2370
MC/DC0%0/1
$ref->{'field'} || die('Missing field key during normal validation')
$ref->{'field'}
Truth table50%1/2
A
A= $ref->{'field'}
Aresult
11
00
57  
58 # allow for field names that contain regular expressions
59 237 my @keys;
60 237 ▶ if ($key =~ m/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/s) { } 9 228 237
61 9 my ($not,$pat,$opt) = ($1,$3,$4);
62 9 $opt =~ tr/g//d;
63 9 ▶ die "The e option cannot be used on validation keys on field $key" if $opt =~ /e/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /e/ 0 9 9
64 9 foreach my $_key (sort keys %$form) {
65 15 ▶ next if ($not && $_key =~ m/(?$opt:$pat)/) || (! $not && $_key !~ m/(?$opt:$pat)/);
Branch: 1 branch
BranchTrueFalseTotal
if $not and $_key =~ /(?$opt:$pat)/ or not $not || $_key =~ /(?$opt:$pat)/ 6 9 15
Condition55%5/9
$not and $_key =~ /(?$opt:$pat)/ or not $not || $_key =~ /(?$opt:$pat)/
l!l&&r!l&&!r
069
$not and $_key =~ /(?$opt:$pat)/
!ll&&!rl&&r
1500
$not || $_key =~ /(?$opt:$pat)/
l!l&&r!l&&!r
096
MC/DC25%1/4
$not and $_key =~ /(?$opt:$pat)/ or not $not || $_key =~ /(?$opt:$pat)/
$not $_key =~ /(?$opt:$pat)/ $not $_key =~ /(?$opt:$pat)/
Truth table28%2/7
A and B or not(C || D)
A= $notB= $_key =~ /(?$opt:$pat)/C= $notD= $_key =~ /(?$opt:$pat)/
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
66 9 push @keys, [$_key, [undef, $1, $2, $3, $4, $5]];
67 }
68 } else {
69 228 @keys = ([$key]);
70 }
71  
72 237 foreach my $r (@keys) {
73 237 my ($field, $ifs_match) = @$r;
74 237 ▶ if (! $checked{$field}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checked{$field}++) 237 0 237
75 237 $self->{'was_checked'}->{$field} = 1;
76 237 $self->{'was_valid'}->{$field} = 1;
77 237 $self->{'had_error'}->{$field} = 0;
78 }
79 237 local $ref->{'was_validated'} = 1;
80 237 my $err = $self->validate_buddy($form, $field, $ref, $ifs_match);
81 237 ▶ if ($ref->{'was_validated'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{'was_validated'}) { } 221 16 237
82 221 ▶ push @$what_was_validated, $ref if $what_was_validated;
Branch: 1 branch
BranchTrueFalseTotal
if $what_was_validated 7 214 221
83 } else {
84 16 $self->{'was_valid'}->{$field} = 0;
85 }
86  
87 # test the error - if errors occur allow for OR - if OR fails use errors from first fail
88 237 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 104 133 237
89 104 $self->{'was_valid'}->{$field} = 0;
90 104 $self->{'had_error'}->{$field} = 0;
91 104 ▶ if ($i < $#$fields && ! ref($fields->[$i + 1]) && $fields->[$i + 1] eq 'OR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $fields->$#* and not ref $$fields[$i + 1] and $$fields[$i + 1] eq 'OR') { } 0 104 104
Condition50%3/6
$i < $fields->$#* and not ref $$fields[$i + 1] and $$fields[$i + 1] eq "OR"
!ll&&!rl&&r
10400
$i < $fields->$#* and not ref $$fields[$i + 1]
!ll&&!rl&&r
10130
MC/DC0%0/3
$i < $fields->$#* and not ref $$fields[$i + 1] and $$fields[$i + 1] eq "OR"
$i < $fields->$#* not ref $$fields[$i + 1] $$fields[$i + 1] eq "OR"
Truth table50%2/4
A and not(B) and C
A= $i < $fields->$#*B= not ref $$fields[$i + 1]C= $$fields[$i + 1] eq "OR"
ABCresult
0XX0
10X0
1100
1111
92 0 $hold_error = $err;
93 } else {
94 104 ▶ push @errors, $hold_error ? @$hold_error : @$err;
Branch: 1 branch
BranchTrueFalseTotal
$hold_error ? : 0 104 104
95 104 $hold_error = undef;
96 }
97 } else {
98 133 $hold_error = undef;
99 }
100 }
101 }
102 215 ▶ push(@errors, @$hold_error) if $hold_error; # allow for final OR to work
Branch: 1 branch
BranchTrueFalseTotal
if $hold_error 0 215 215
103  
104 # optionally check for unused keys in the form
105 215 ▶ if ($ARGS->{no_extra_fields} || $self->{no_extra_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS->{"no_extra_fields"} or $self->{"no_extra_fields"}) 8 207 215
Condition66%2/3
$ARGS->{"no_extra_fields"} or $self->{"no_extra_fields"}
l!l&&r!l&&!r
80207
MC/DC50%1/2
$ARGS->{"no_extra_fields"} or $self->{"no_extra_fields"}
$ARGS->{"no_extra_fields"} $self->{"no_extra_fields"}
Truth table66%2/3
A or B
A= $ARGS->{"no_extra_fields"}B= $self->{"no_extra_fields"}
ABresult
1X1
011
000
106 8 my %keys = map { ($_->{'field'} => 1) } @$fields;
107 8 foreach my $key (sort keys %$form) {
108 12 ▶ next if $keys{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $keys{$key} 8 4 12
109 4 push @errors, [$key, 'no_extra_fields', {}, undef];
110 }
111 }
112  
113 215 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 107 108 215
114 107 my @copy = grep {/$QR_EXTRA/o} keys %$self;
115 107 @{ $ARGS }{@copy} = @{ $self }{@copy};
116 107 ▶ unshift @errors, $ARGS->{'title'} if $ARGS->{'title'};
Branch: 1 branch
BranchTrueFalseTotal
if $ARGS->{"title"} 0 107 107
117 107 my $err_obj = $self->new_error(\@errors, $ARGS);
118 107 ▶ die $err_obj if $ARGS->{'raise_error'};
Branch: 1 branch
BranchTrueFalseTotal
if $ARGS->{"raise_error"} 0 107 107
119 107 return $err_obj;
120 }
121  
122 108 return; # success
123 }
124  
125 sub get_ordered_fields {
126 221 ▶ my ($self, $val_hash) = @_;
Subroutine get_ordered_fields: called CC=25 SCAR=40.5
Pod undocumented
127  
128 221 ▶ die "Missing validation hash" if ! $val_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $val_hash 1 220 221
129 220 ▶ if (ref $val_hash ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val_hash ne "HASH") 0 220 220
130 0 ▶ $val_hash = $self->get_validation($val_hash) if ref $val_hash ne 'SCALAR' || ! ref $val_hash;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val_hash ne "SCALAR" or not ref $val_hash 0 0 0
Condition0%0/3
ref $val_hash ne "SCALAR" or not ref $val_hash
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $val_hash ne "SCALAR" or not ref $val_hash
ref $val_hash ne "SCALAR" not ref $val_hash
Truth table0%0/3
A or not(B)
A= ref $val_hash ne "SCALAR"B= not ref $val_hash
ABresult
1X1
011
000
131 0 ▶ die "Validation groups must be a hashref" if ref $val_hash ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val_hash ne "HASH" 0 0 0
132 }
133  
134 220 my %ARGS;
135 220 my @field_keys = grep { /^(?:group|general)\s+(\w+)/
136 262 ▶ ? do {$ARGS{$1} = $val_hash->{$_} ; 0}
Branch: 1 branch
BranchTrueFalseTotal
/^(?:group|general)\s+(\w+)/ ? : 21 241 262
137 : 1 } sort keys %$val_hash;
138  
139 # Look first for items in 'group fields' or 'group order'
140 220 my $fields;
141 220 ▶ if (my $ref = $ARGS{'fields'} || $ARGS{'order'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $ARGS{"fields"} || $ARGS{"order"}) 1 219 220
Condition66%2/3
$ARGS{'fields'} || $ARGS{'order'}
l!l&&r!l&&!r
01219
MC/DC50%1/2
$ARGS{'fields'} || $ARGS{'order'}
$ARGS{'fields'} $ARGS{'order'}
Truth table66%2/3
A || B
A= $ARGS{'fields'}B= $ARGS{'order'}
ABresult
1X1
011
000
142 1 ▶ my $type = $ARGS{'fields'} ? 'group fields' : 'group order';
Branch: 1 branch
BranchTrueFalseTotal
$ARGS{'fields'} ? : 0 1 1
143 1 ▶ die "Validation '$type' must be an arrayref when passed" if ! UNIVERSAL::isa($ref, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($ref, "ARRAY") 0 1 1
144 1 foreach my $field (@$ref) {
145 1 ▶ die "Non-defined value in '$type'" if ! defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 1 1
146 1 ▶ if (ref $field) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $field) { } 0 1 1
elsif ($field eq 'OR') { } 0 1 1
147 0 ▶ die "Found nonhashref value in '$type'" if ref($field) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $field ne "HASH" 0 0 0
148 0 ▶ die "Element missing \"field\" key/value in '$type'" if ! defined $field->{'field'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->{"field"} 0 0 0
149 0 push @$fields, $field;
150 } elsif ($field eq 'OR') {
151 0 push @$fields, 'OR';
152 } else {
153 1 ▶ die "No element found in '$type' for $field" if ! exists $val_hash->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $val_hash->{$field} 0 1 1
154 1 ▶ die "Found nonhashref value in '$type'" if ref($val_hash->{$field}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val_hash->{$field} ne "HASH" 0 1 1
155 1 my $val = $val_hash->{$field};
156 1 ▶ $val = {%$val, field => $field} if ! $val->{'field'}; # copy the values to add the key
Branch: 1 branch
BranchTrueFalseTotal
unless $val->{"field"} 1 0 1
157 1 push @$fields, $val;
158 }
159 }
160  
161 # limit the keys that need to be searched to those not in fields or order
162 1 ▶ my %found = map { ref($_) ? ($_->{'field'} => 1) : () } @$fields;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 0 1
163 1 @field_keys = grep { ! $found{$_} } @field_keys;
164 }
165  
166 # add any remaining field_vals from our original hash
167 # this is necessary for items that weren't in group fields or group order
168 220 foreach my $field (@field_keys) {
169 240 ▶ die "Found nonhashref value for field $field" if ref($val_hash->{$field}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val_hash->{$field} ne "HASH" 0 240 240
170 240 ▶ if (defined $val_hash->{$field}->{'field'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val_hash->{$field}{'field'}) { } 0 240 240
171 0 push @$fields, $val_hash->{$field};
172 } else {
173 240 push @$fields, { %{$val_hash->{$field}}, field => $field };
174 }
175 }
176  
177 220 ▶ return ($fields || [], \%ARGS);
Condition100%2/2
$fields || []
l!l
2191
MC/DC100%1/1
$fields || []
$fields
Truth table100%2/2
A
A= $fields
Aresult
11
00
178 }
179  
180 sub new_error {
181 107 ▶ my $self = shift;
Subroutine new_error: called CC=1 SCAR=0.0
Pod undocumented
182 107 return CGI::Ex::Validate::Error->new(@_);
183 }
184  
185 ### allow for optional validation on groups and on individual items
186 sub check_conditional {
187 34 ▶ my ($self, $form, $ifs, $ifs_match) = @_;
Subroutine check_conditional: called CC=16 SCAR=30.1
Pod undocumented
188 34 ▶ die "Need reference passed to check_conditional" if ! $ifs;
Branch: 1 branch
BranchTrueFalseTotal
unless $ifs 0 34 34
189 34 ▶ $ifs = [$ifs] if ! ref($ifs) || UNIVERSAL::isa($ifs,'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if not ref $ifs or UNIVERSAL::isa($ifs, "HASH") 34 0 34
Condition66%2/3
not ref $ifs or UNIVERSAL::isa($ifs, "HASH")
l!l&&r!l&&!r
3220
MC/DC0%0/2
not ref $ifs or UNIVERSAL::isa($ifs, "HASH")
not ref $ifs UNIVERSAL::isa($ifs, "HASH")
Truth table66%2/3
not(A) or B
A= not ref $ifsB= UNIVERSAL::isa($ifs, "HASH")
ABresult
1X1
011
000
190  
191 34 local $self->{'_check_conditional'} = 1;
192  
193 # run the if options here
194 # multiple items can be passed - all are required unless OR is used to separate
195 34 my $found = 1;
196 34 ▶ foreach (my $i = 0; $i <= $#$ifs; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $ifs->$#*) 34 34 68
197 34 my $ref = $ifs->[$i];
198 34 ▶ if (! ref $ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref) 32 2 34
199 32 ▶ if ($ref eq 'OR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'OR') { } 0 32 32
200 0 ▶ $i++ if $found; # if found skip the OR altogether
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
201 0 $found = 1; # reset
202 0 next;
203 } else {
204 32 ▶ if ($ref =~ /^function\s*\(/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref =~ /^function\s*\(/) { } 0 32 32
elsif ($ref =~ /^(.*?)\s+(was_valid|had_error|was_checked)$/) { } 11 21 32
elsif ($ref =~ s/^\s*!\s*//) { } 2 19 21
205 0 next;
206 } elsif ($ref =~ /^(.*?)\s+(was_valid|had_error|was_checked)$/) {
207 11 $ref = {field => $1, $2 => 1};
208 } elsif ($ref =~ s/^\s*!\s*//) {
209 2 $ref = {field => $ref, max_in_set => "0 of $ref"};
210 } else {
211 19 $ref = {field => $ref, required => 1};
212 }
213 }
214 }
215 34 ▶ last if ! $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 34 34
216  
217 # get the field - allow for custom variables based upon a match
218 34 ▶ my $field = $ref->{'field'} || die "Missing field key during validate_if (possibly used a reference to a main hash *foo -> &foo)";
Condition50%1/2
$ref->{'field'} || die('Missing field key during validate_if (possibly used a reference to a main hash *foo -> &foo)')
l!l
340
MC/DC0%0/1
$ref->{'field'} || die('Missing field key during validate_if (possibly used a reference to a main hash *foo -> &foo)')
$ref->{'field'}
Truth table50%1/2
A
A= $ref->{'field'}
Aresult
11
00
219 34 ▶ $field =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 7 27 34
defined $ifs_match->[$1] ? : 7 0 7
220  
221 34 my $errs = $self->validate_buddy($form, $field, $ref);
222  
223 34 ▶ $found = 0 if $errs;
Branch: 1 branch
BranchTrueFalseTotal
if $errs 21 13 34
224 }
225 34 return $found;
226 }
227  
228  
229 ### this is where the main checking goes on
230 sub validate_buddy {
231 271 ▶ my ($self, $form, $field, $field_val, $ifs_match) = @_;
Subroutine validate_buddy: called CC=187 SCAR=63.6
Pod undocumented
232 271 ▶ local $self->{'_recurse'} = ($self->{'_recurse'} || 0) + 1;
Condition100%2/2
$self->{'_recurse'} || 0
l!l
26245
MC/DC100%1/1
$self->{'_recurse'} || 0
$self->{'_recurse'}
Truth table100%2/2
A
A= $self->{'_recurse'}
Aresult
11
00
233 271 ▶ die "Max dependency level reached 10" if $self->{'_recurse'} > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_recurse"} > 10 0 271 271
234 271 my @errors;
235  
236 271 ▶ if ($field_val->{'exclude_cgi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"exclude_cgi"}) 0 271 271
237 0 delete $field_val->{'was_validated'};
238 0 return 0;
239 }
240  
241 # allow for field names that contain regular expressions
242 271 ▶ if ($field =~ m/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/s) 0 271 271
243 0 my ($not,$pat,$opt) = ($1,$3,$4);
244 0 $opt =~ tr/g//d;
245 0 ▶ die "The e option cannot be used on validation keys on field $field" if $opt =~ /e/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /e/ 0 0 0
246 0 foreach my $_field (sort keys %$form) {
247 0 ▶ next if ($not && $_field =~ m/(?$opt:$pat)/) || (! $not && $_field !~ m/(?$opt:$pat)/);
Branch: 1 branch
BranchTrueFalseTotal
if $not and $_field =~ /(?$opt:$pat)/ or not $not || $_field =~ /(?$opt:$pat)/ 0 0 0
Condition0%0/9
$not and $_field =~ /(?$opt:$pat)/ or not $not || $_field =~ /(?$opt:$pat)/
l!l&&r!l&&!r
000
$not and $_field =~ /(?$opt:$pat)/
!ll&&!rl&&r
000
$not || $_field =~ /(?$opt:$pat)/
l!l&&r!l&&!r
000
MC/DC0%0/4
$not and $_field =~ /(?$opt:$pat)/ or not $not || $_field =~ /(?$opt:$pat)/
$not $_field =~ /(?$opt:$pat)/ $not $_field =~ /(?$opt:$pat)/
Truth table0%0/7
A and B or not(C || D)
A= $notB= $_field =~ /(?$opt:$pat)/C= $notD= $_field =~ /(?$opt:$pat)/
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
248 0 my $errs = $self->validate_buddy($form, $_field, $field_val, [undef, $1, $2, $3, $4, $5]);
249 0 ▶ push @errors, @$errs if $errs;
Branch: 1 branch
BranchTrueFalseTotal
if $errs 0 0 0
250 }
251 0 ▶ return @errors ? \@errors : 0;
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 0 0 0
252 }
253  
254 271 ▶ if ($field_val->{'was_valid'} && ! $self->{'was_valid'}->{$field}) { return [[$field, 'was_valid', $field_val, $ifs_match]]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"was_valid"} and not $self->{"was_valid"}{$field}) 7 264 271
Condition100%3/3
$field_val->{"was_valid"} and not $self->{"was_valid"}{$field}
!ll&&!rl&&r
26317
MC/DC100%2/2
$field_val->{"was_valid"} and not $self->{"was_valid"}{$field}
$field_val->{"was_valid"} not $self->{"was_valid"}{$field}
Truth table100%3/3
A and not(B)
A= $field_val->{"was_valid"}B= not $self->{"was_valid"}{$field}
ABresult
0X0
100
111
255 264 ▶ if ($field_val->{'had_error'} && ! $self->{'had_error'}->{$field}) { return [[$field, 'had_error', $field_val, $ifs_match]]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"had_error"} and not $self->{"had_error"}{$field}) 3 261 264
Condition66%2/3
$field_val->{"had_error"} and not $self->{"had_error"}{$field}
!ll&&!rl&&r
26103
MC/DC50%1/2
$field_val->{"had_error"} and not $self->{"had_error"}{$field}
$field_val->{"had_error"} not $self->{"had_error"}{$field}
Truth table66%2/3
A and not(B)
A= $field_val->{"had_error"}B= not $self->{"had_error"}{$field}
ABresult
0X0
100
111
256 261 ▶ if ($field_val->{'was_checked'} && ! $self->{'was_checked'}->{$field}) { return [[$field, 'was_checked', $field_val, $ifs_match]]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"was_checked"} and not $self->{"was_checked"}{$field}) 0 261 261
Condition33%1/3
$field_val->{"was_checked"} and not $self->{"was_checked"}{$field}
!ll&&!rl&&r
26100
MC/DC0%0/2
$field_val->{"was_checked"} and not $self->{"was_checked"}{$field}
$field_val->{"was_checked"} not $self->{"was_checked"}{$field}
Truth table33%1/3
A and not(B)
A= $field_val->{"was_checked"}B= not $self->{"was_checked"}{$field}
ABresult
0X0
100
111
257  
258  
259 # allow for default value
260 261 ▶ if (defined($field_val->{'default'})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $field_val->{"default"} and !defined($form->{$field}) || (UNIVERSAL::isa($form->{$field}, "ARRAY") ? !@{$form->{$field};} : !length($form->{$field}))) 1 260 261
UNIVERSAL::isa($form->{$field}, 'ARRAY') ? : 0 0 0
Condition50%3/6
defined $field_val->{"default"} and !defined($form->{$field}) || (UNIVERSAL::isa($form->{$field}, "ARRAY") ? !@{$form->{$field};} : !length($form->{$field}))
!ll&&!rl&&r
26010
not defined $form->{$field} or UNIVERSAL::isa($form->{$field}, "ARRAY") ? !@{$form->{$field};} : !length($form->{$field})
l!l&&r!l&&!r
100
MC/DC0%0/3
defined $field_val->{"default"} and !defined($form->{$field}) || (UNIVERSAL::isa($form->{$field}, "ARRAY") ? !@{$form->{$field};} : !length($form->{$field}))
defined $field_val->{"default"} not defined $form->{$field} UNIVERSAL::isa($form->{$field}, "ARRAY") ? !@{$form->{$field};} : !length($form->{$field})
Truth table25%1/4
A and not(B) or C
A= defined $field_val->{"default"}B= not defined $form->{$field}C= UNIVERSAL::isa($form->{$field}, "ARRAY") ? !@{$form->{$field};} : !length($form->{$field})
ABCresult
0XX0
1000
11X1
1011
261 && (!defined($form->{$field})
262 0 || (UNIVERSAL::isa($form->{$field},'ARRAY') ? !@{ $form->{$field} } : !length($form->{$field})))) {
263 1 $form->{$field} = $field_val->{'default'};
264 }
265  
266 261 ▶ my $values = UNIVERSAL::isa($form->{$field},'ARRAY') ? $form->{$field} : [$form->{$field}];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($form->{$field}, 'ARRAY') ? : 11 250 261
267 261 my $n_values = @$values;
268  
269 # allow for a few form modifiers
270 261 my $modified = 0;
271 261 foreach my $value (@$values) {
272 275 ▶ next if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 60 215 275
273 215 ▶ if (! $field_val->{'do_not_trim'}) { # whitespace
Branch: 1 branch
BranchTrueFalseTotal
unless ($field_val->{"do_not_trim"}) 215 0 215
274 215 ▶ $modified = 1 if $value =~ s/( ^\s+ | \s+$ )//xg;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ s/( ^\s+ | \s+$ )//gx 0 215 215
275 }
276 215 ▶ if ($field_val->{'trim_control_chars'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"trim_control_chars"}) 0 215 215
277 0 ▶ $modified = 1 if $value =~ y/\t/ /;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ tr/\t/ / 0 0 0
278 0 ▶ $modified = 1 if $value =~ y/\x00-\x1F//d;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ tr/\000-\037//d 0 0 0
279 }
280 215 ▶ if ($field_val->{'to_upper_case'}) { # uppercase
Branch: 2 branches
BranchTrueFalseTotal
if ($field_val->{'to_upper_case'}) { } 0 215 215
elsif ($field_val->{'to_lower_case'}) { } 0 215 215
281 0 $value = uc $value;
282 0 $modified = 1;
283 } elsif ($field_val->{'to_lower_case'}) { # lowercase
284 0 $value = lc $value;
285 0 $modified = 1;
286 }
287 }
288  
289 261 my %types;
290 261 foreach (sort keys %$field_val) {
291 811 ▶ push @{$types{$1}}, $_ if /^ (compare|custom|equals|match|max_in_set|min_in_set|replace|required_if|sql|type|validate_if) _?\d* $/x;
Branch: 1 branch
BranchTrueFalseTotal
if /^ (compare|custom|equals|match|max_in_set|min_in_set|replace|required_if|sql|type|validate_if) _?\d* $/x 176 635 811
292 }
293  
294 # allow for inline specified modifications (ie s/foo/bar/)
295 261 ▶ if ($types{'replace'}) { foreach my $type (@{ $types{'replace'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"replace"}) 6 255 261
296 my $ref = UNIVERSAL::isa($field_val->{$type},'ARRAY') ? $field_val->{$type}
297 6 ▶ : [split(/\s*\|\|\s*/,$field_val->{$type})];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($field_val->{$type}, 'ARRAY') ? : 0 6 6
298 6 foreach my $rx (@$ref) {
299 6 ▶ if ($rx !~ m/^\s*s([^\s\w])(.+)\1(.*)\1([eigsmx]*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rx =~ /^\s*s([^\s\w])(.+)\1(.*)\1([eigsmx]*)$/s) 0 6 6
300 0 die "Not sure how to parse that replace ($rx)";
301 }
302 6 my ($pat, $swap, $opt) = ($2, $3, $4);
303 6 ▶ die "The e option cannot be used in swap on field $field" if $opt =~ /e/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /e/ 0 6 6
304 6 my $global = $opt =~ s/g//g;
305 6 $swap =~ s/\\n/\n/g;
306 my $expand = sub { # code similar to Template::Alloy::VMethod::vmethod_replace
307 9 ▶ my ($text, $start, $end) = @_;
Subroutine __ANON__: called CC=4 SCAR=15.0
308 9 my $copy = $swap;
309 9 $copy =~ s{ \\(\\|\$) | \$ (\d+) }{
310 3 ▶ $1 ? $1
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 0 3 3
$2 > $start->$#* || $2 == 0 ? : 0 3 3
Condition33%1/3
$2 > $start->$#* or $2 == 0
l!l&&r!l&&!r
003
MC/DC0%0/2
$2 > $start->$#* or $2 == 0
$2 > $start->$#* $2 == 0
Truth table33%1/3
A or B
A= $2 > $start->$#*B= $2 == 0
ABresult
1X1
011
000
311 : ($2 > $#$start || $2 == 0) ? ''
312 : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
313 }exg;
314 9 $modified = 1;
315 9 $copy;
316 6 };
317 6 foreach my $value (@$values) {
318 6 ▶ next if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 5 6
319 5 ▶ if ($global) { $value =~ s{(?$opt:$pat)}{ $expand->($value, [@-], [@+]) }eg }
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 4 1 5
320 1 else { $value =~ s{(?$opt:$pat)}{ $expand->($value, [@-], [@+]) }e }
321 }
322 }
323 } }
324 261 ▶ $form->{$field} = $values->[0] if $modified && $n_values == 1; # put them back into the form if we have modified it
Branch: 1 branch
BranchTrueFalseTotal
if $modified and $n_values == 1 5 256 261
Condition66%2/3
$modified and $n_values == 1
!ll&&!rl&&r
25605
MC/DC50%1/2
$modified and $n_values == 1
$modified $n_values == 1
Truth table66%2/3
A and B
A= $modifiedB= $n_values == 1
ABresult
0X0
100
111
325  
326 # only continue if a validate_if is not present or passes test
327 261 my $needs_val = 0;
328 261 my $n_vif = 0;
329 261 ▶ if ($types{'validate_if'}) { foreach my $type (@{ $types{'validate_if'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"validate_if"}) 24 237 261
330 24 $n_vif++;
331 24 my $ifs = $field_val->{$type};
332 24 my $ret = $self->check_conditional($form, $ifs, $ifs_match);
333 24 ▶ $needs_val++ if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 8 16 24
334 } }
335 261 ▶ if (! $needs_val && $n_vif) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $needs_val and $n_vif) 16 245 261
Condition100%3/3
not $needs_val and $n_vif
!ll&&!rl&&r
823716
MC/DC100%2/2
not $needs_val and $n_vif
not $needs_val $n_vif
Truth table100%3/3
not(A) and B
A= not $needs_valB= $n_vif
ABresult
0X0
100
111
336 16 delete $field_val->{'was_validated'};
337 16 return 0;
338 }
339  
340 # check for simple existence
341 # optionally check only if another condition is met
342 245 ▶ my $is_required = $field_val->{'required'} ? 'required' : '';
Branch: 1 branch
BranchTrueFalseTotal
$field_val->{'required'} ? : 54 191 245
343 245 ▶ if (! $is_required) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_required) 191 54 245
344 191 ▶ if ($types{'required_if'}) { foreach my $type (@{ $types{'required_if'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"required_if"}) 2 189 191
345 2 my $ifs = $field_val->{$type};
346 2 ▶ next if ! $self->check_conditional($form, $ifs, $ifs_match);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_conditional($form, $ifs, $ifs_match) 1 1 2
347 1 $is_required = $type;
348 1 last;
349 } }
350 }
351 245 ▶ if ($is_required
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required and $n_values == 0 || $n_values == 1 && !(defined $values->[0] && length $values->[0])) 28 217 245
Condition91%11/12
$is_required and $n_values == 0 || $n_values == 1 && !(defined $values->[0] && length $values->[0])
!ll&&!rl&&r
1902629
$n_values == 0 or $n_values == 1 and not defined $values->[0] && length $values->[0]
l!l&&r!l&&!r
02926
$n_values == 1 and not defined $values->[0] && length $values->[0]
!ll&&!rl&&r
12628
defined $values->[0] && length $values->[0]
!ll&&!rl&&r
24426
MC/DC60%3/5
$is_required and $n_values == 0 || $n_values == 1 && !(defined $values->[0] && length $values->[0])
$is_required $n_values == 0 $n_values == 1 defined $values->[0] length $values->[0]
Truth table66%4/6
A and B or C and not(D && E)
A= $is_requiredB= $n_values == 0C= $n_values == 1D= defined $values->[0]E= length $values->[0]
ABCDEresult
0XXXX0
100XX0
101110
11XXX1
1010X1
101101
352 && ($n_values == 0 || ($n_values == 1 && (! defined($values->[0]) || ! length $values->[0])))) {
353 28 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 10 18 28
354 18 return [[$field, $is_required, $field_val, $ifs_match]];
355 }
356  
357 217 ▶ my $n = exists($field_val->{'min_values'}) ? $field_val->{'min_values'} || 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $field_val->{'min_values'} ? : 5 212 217
Condition50%1/2
$field_val->{"min_values"} or 0
l!l
50
MC/DC0%0/1
$field_val->{"min_values"} or 0
$field_val->{"min_values"}
Truth table50%1/2
A
A= $field_val->{"min_values"}
Aresult
11
00
358 217 ▶ if ($n_values < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_values < $n) 3 214 217
359 3 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 3 3
360 3 return [[$field, 'min_values', $field_val, $ifs_match]];
361 }
362  
363 214 ▶ $field_val->{'max_values'} = 1 if ! exists $field_val->{'max_values'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $field_val->{"max_values"} 195 19 214
364 214 ▶ $n = $field_val->{'max_values'} || 0;
Condition50%1/2
$field_val->{'max_values'} || 0
l!l
2140
MC/DC0%0/1
$field_val->{'max_values'} || 0
$field_val->{'max_values'}
Truth table50%1/2
A
A= $field_val->{'max_values'}
Aresult
11
00
365 214 ▶ if ($n_values > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_values > $n) 2 212 214
366 2 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 2 2
367 2 return [[$field, 'max_values', $field_val, $ifs_match]];
368 }
369  
370 212 foreach ([min => $types{'min_in_set'}],
371 [max => $types{'max_in_set'}]) {
372 421 ▶ my $keys = $_->[1] || next;
Condition100%2/2
$_->[1] || (next)
l!l
14407
MC/DC100%1/1
$_->[1] || (next)
$_->[1]
Truth table100%2/2
A
A= $_->[1]
Aresult
11
00
373 14 my $minmax = $_->[0];
374 14 foreach my $type (@$keys) {
375 14 ▶ $field_val->{$type} =~ m/^\s*(\d+)(?i:\s*of)?\s+(.+)\s*$/
Branch: 1 branch
BranchTrueFalseTotal
$field_val->{$type} =~ /^\s*(\d+)(?i:\s*of)?\s+(.+)\s*$/ or die "Invalid ${minmax}_in_set check $field_val->{$type}" 0 14 14
376 || die "Invalid ${minmax}_in_set check $field_val->{$type}";
377 14 my $n = $1;
378 14 foreach my $_field (split /[\s,]+/, $2) {
379 38 ▶ my $ref = UNIVERSAL::isa($form->{$_field},'ARRAY') ? $form->{$_field} : [$form->{$_field}];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($form->{$_field}, 'ARRAY') ? : 4 34 38
380 38 foreach my $_value (@$ref) {
381 42 ▶ $n -- if defined($_value) && length($_value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_value and length $_value 23 19 42
Condition100%3/3
defined $_value and length $_value
!ll&&!rl&&r
18123
MC/DC100%2/2
defined $_value and length $_value
defined $_value length $_value
Truth table100%3/3
A and B
A= defined $_valueB= length $_value
ABresult
0X0
100
111
382 }
383 }
384 14 ▶ if ( ($minmax eq 'min' && $n > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax eq "min" and $n > 0 or $minmax eq "max" and $n < 0) 6 8 14
Condition100%9/9
$minmax eq "min" and $n > 0 or $minmax eq "max" and $n < 0
l!l&&r!l&&!r
338
$minmax eq "min" and $n > 0
!ll&&!rl&&r
743
$minmax eq "max" and $n < 0
!ll&&!rl&&r
443
MC/DC50%2/4
$minmax eq "min" and $n > 0 or $minmax eq "max" and $n < 0
$minmax eq "min" $n > 0 $minmax eq "max" $n < 0
Truth table42%3/7
A and B or C and D
A= $minmax eq "min"B= $n > 0C= $minmax eq "max"D= $n < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
385 || ($minmax eq 'max' && $n < 0)) {
386 6 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 1 5 6
387 5 return [[$field, $type, $field_val, $ifs_match]];
388 }
389 }
390 }
391  
392 # at this point @errors should still be empty
393 206 my $content_checked; # allow later for possible untainting (only happens if content was checked)
394  
395 206 OUTER: foreach my $value (@$values) {
396  
397 214 ▶ if (exists $field_val->{'enum'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field_val->{"enum"}) 10 204 214
398 10 ▶ my $ref = ref($field_val->{'enum'}) ? $field_val->{'enum'} : [split(/\s*\|\|\s*/,$field_val->{'enum'})];
Branch: 1 branch
BranchTrueFalseTotal
ref $field_val->{'enum'} ? : 5 5 10
399 10 my $found = 0;
400 10 foreach (@$ref) {
401 30 ▶ $found = 1 if defined($value) && $_ eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $_ eq $value 7 23 30
Condition100%3/3
defined $value and $_ eq $value
!ll&&!rl&&r
6177
MC/DC100%2/2
defined $value and $_ eq $value
defined $value $_ eq $value
Truth table100%3/3
A and B
A= defined $valueB= $_ eq $value
ABresult
0X0
100
111
402 }
403 10 ▶ if (! $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 3 7 10
404 3 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 3 3
405 3 push @errors, [$field, 'enum', $field_val, $ifs_match];
406 3 next OUTER;
407 }
408 7 $content_checked = 1;
409 }
410  
411 # do specific type checks
412 211 ▶ if (exists $field_val->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field_val->{"type"}) 65 146 211
413 65 ▶ if (! $self->check_type($value, $field_val->{'type'}, $field, $form)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_type($value, $field_val->{"type"}, $field, $form)) 35 30 65
414 35 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 35 35
415 35 push @errors, [$field, 'type', $field_val, $ifs_match];
416 35 next OUTER;
417 }
418 30 $content_checked = 1;
419 }
420  
421 # field equals another field
422 176 ▶ if ($types{'equals'}) { foreach my $type (@{ $types{'equals'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"equals"}) 9 167 176
423 9 my $field2 = $field_val->{$type};
424 9 ▶ my $not = ($field2 =~ s/^!\s*//) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$field2 =~ s/^!\s*// ? : 0 9 9
425 9 my $success = 0;
426 9 ▶ if ($field2 =~ m/^([\"\'])(.*)\1$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field2 =~ /^([\"\'])(.*)\1$/) { } 2 7 9
427 2 my $test = $2;
428 2 ▶ $success = (defined($value) && $value eq $test);
Condition66%2/3
defined $value && $value eq $test
!ll&&!rl&&r
011
MC/DC50%1/2
defined $value && $value eq $test
defined $value $value eq $test
Truth table66%2/3
A && B
A= defined $valueB= $value eq $test
ABresult
0X0
100
111
429 } else {
430 7 ▶ $field2 =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 2 5 7
defined $ifs_match->[$1] ? : 2 0 2
431 7 ▶ if (exists($form->{$field2}) && defined($form->{$field2})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $form->{$field2} and defined $form->{$field2}) { } 5 2 7
elsif (not defined $value) { } 1 1 2
Condition66%2/3
exists $form->{$field2} and defined $form->{$field2}
!ll&&!rl&&r
205
MC/DC50%1/2
exists $form->{$field2} and defined $form->{$field2}
exists $form->{$field2} defined $form->{$field2}
Truth table66%2/3
A and B
A= exists $form->{$field2}B= defined $form->{$field2}
ABresult
0X0
100
111
432 5 ▶ $success = (defined($value) && $value eq $form->{$field2});
Condition100%3/3
defined $value && $value eq $form->{$field2}
!ll&&!rl&&r
122
MC/DC100%2/2
defined $value && $value eq $form->{$field2}
defined $value $value eq $form->{$field2}
Truth table100%3/3
A && B
A= defined $valueB= $value eq $form->{$field2}
ABresult
0X0
100
111
433 } elsif (! defined($value)) {
434 1 $success = 1; # occurs if they are both undefined
435 }
436 }
437 9 ▶ if ($not ? $success : ! $success) {
Branch: 2 branches
BranchTrueFalseTotal
if ($not ? $success : !$success) 5 4 9
$not ? : 0 9 9
438 5 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 5 5
439 5 push @errors, [$field, $type, $field_val, $ifs_match];
440 5 next OUTER;
441 }
442 4 $content_checked = 1;
443 } }
444  
445 171 ▶ if (exists $field_val->{'min_len'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field_val->{"min_len"}) 4 167 171
446 4 my $n = $field_val->{'min_len'};
447 4 ▶ if (! defined($value) || length($value) < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or length $value < $n) 3 1 4
Condition100%3/3
not defined $value or length $value < $n
l!l&&r!l&&!r
121
MC/DC100%2/2
not defined $value or length $value < $n
not defined $value length $value < $n
Truth table100%3/3
not(A) or B
A= not defined $valueB= length $value < $n
ABresult
1X1
011
000
448 3 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 3 3
449 3 push @errors, [$field, 'min_len', $field_val, $ifs_match];
450 }
451 }
452  
453 171 ▶ if (exists $field_val->{'max_len'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field_val->{"max_len"}) 17 154 171
454 17 my $n = $field_val->{'max_len'};
455 17 ▶ if (defined($value) && length($value) > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and length $value > $n) 1 16 17
Condition100%3/3
defined $value and length $value > $n
!ll&&!rl&&r
5111
MC/DC100%2/2
defined $value and length $value > $n
defined $value length $value > $n
Truth table100%3/3
A and B
A= defined $valueB= length $value > $n
ABresult
0X0
100
111
456 1 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 1 1
457 1 push @errors, [$field, 'max_len', $field_val, $ifs_match];
458 }
459 }
460  
461 # now do match types
462 171 ▶ if ($types{'match'}) { foreach my $type (@{ $types{'match'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"match"}) 17 154 171
463 my $ref = UNIVERSAL::isa($field_val->{$type},'ARRAY') ? $field_val->{$type}
464 : UNIVERSAL::isa($field_val->{$type}, 'Regexp') ? [$field_val->{$type}]
465 17 ▶ : [split(/\s*\|\|\s*/,$field_val->{$type})];
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($field_val->{$type}, 'ARRAY') ? : 2 15 17
UNIVERSAL::isa($field_val->{$type}, 'Regexp') ? : 2 13 15
466 17 foreach my $rx (@$ref) {
467 21 ▶ if (UNIVERSAL::isa($rx,'Regexp')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($rx, 'Regexp')) { } 6 15 21
468 6 ▶ if (! defined($value) || $value !~ $rx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and $value =~ /$rx/) 2 4 6
Condition66%2/3
defined $value and $value =~ /$rx/
!ll&&!rl&&r
024
MC/DC50%1/2
defined $value and $value =~ /$rx/
defined $value $value =~ /$rx/
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /$rx/
ABresult
0X0
100
111
469 2 push @errors, [$field, $type, $field_val, $ifs_match];
470 }
471 } else {
472 15 ▶ if ($rx !~ m/^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rx =~ /^(!\s*|)m([^\s\w])(.*)\2([eigsmx]*)$/s) 0 15 15
473 0 die "Not sure how to parse that match ($rx)";
474 }
475 15 my ($not, $pat, $opt) = ($1, $3, $4);
476 15 $opt =~ tr/g//d;
477 15 ▶ die "The e option cannot be used on validation keys on field $field" if $opt =~ /e/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /e/ 0 15 15
478 15 ▶ if ( ( $not && ( defined($value) && $value =~ m/(?$opt:$pat)/))
Branch: 1 branch
BranchTrueFalseTotal
if ($not and (defined $value and $value =~ /(?$opt:$pat)/) or not $not || defined $value && $value =~ /(?$opt:$pat)/) 6 9 15
Condition100%15/15
$not and (defined $value and $value =~ /(?$opt:$pat)/) or not $not || defined $value && $value =~ /(?$opt:$pat)/
l!l&&r!l&&!r
159
$not and (defined $value and $value =~ /(?$opt:$pat)/)
!ll&&!rl&&r
1221
defined $value and $value =~ /(?$opt:$pat)/
!ll&&!rl&&r
111
$not || defined $value && $value =~ /(?$opt:$pat)/
l!l&&r!l&&!r
275
defined $value and $value =~ /(?$opt:$pat)/
!ll&&!rl&&r
147
MC/DC100%6/6
$not and (defined $value and $value =~ /(?$opt:$pat)/) or not $not || defined $value && $value =~ /(?$opt:$pat)/
$not defined $value $value =~ /(?$opt:$pat)/ $not defined $value $value =~ /(?$opt:$pat)/
Truth table46%6/13
A and B and C or not(D || E and F)
A= $notB= defined $valueC= $value =~ /(?$opt:$pat)/D= $notE= defined $valueF= $value =~ /(?$opt:$pat)/
ABCDEFresult
111XXX1
0XX00X1
0XX0101
10X00X1
10X0101
11000X1
1100101
0XX1XX0
0XX0110
10X1XX0
10X0110
1101XX0
1100110
479 || (! $not && (! defined($value) || $value !~ m/(?$opt:$pat)/)) ) {
480 6 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 6 6
481 6 push @errors, [$field, $type, $field_val, $ifs_match];
482 }
483 }
484 }
485 17 $content_checked = 1;
486 } }
487  
488 # allow for comparison checks
489 171 ▶ if ($types{'compare'}) { foreach my $type (@{ $types{'compare'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"compare"}) 30 141 171
490 my $ref = UNIVERSAL::isa($field_val->{$type},'ARRAY') ? $field_val->{$type}
491 30 ▶ : [split(/\s*\|\|\s*/,$field_val->{$type})];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($field_val->{$type}, 'ARRAY') ? : 0 30 30
492 30 foreach my $comp (@$ref) {
493 30 ▶ next if ! $comp;
Branch: 1 branch
BranchTrueFalseTotal
unless $comp 0 30 30
494 30 my $test = 0;
495 30 ▶ if ($comp =~ /^\s*(>|<|[><!=]=)\s*([\d\.\-]+)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($comp =~ /^\s*(>|<|[><!=]=)\s*([\d\.\-]+)\s*$/) { } 15 15 30
elsif ($comp =~ /^\s*(eq|ne|gt|ge|lt|le)\s+(.+?)\s*$/) { } 15 0 15
496 15 ▶ my $val = $value || 0;
Condition100%2/2
$value || 0
l!l
123
MC/DC100%1/1
$value || 0
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
497 15 $val *= 1;
498 15 ▶ if ($1 eq '>' ) { $test = ($val > $2) }
Branch: 6 branches
BranchTrueFalseTotal
if ($1 eq '>') { } 3 12 15
elsif ($1 eq '<') { } 3 9 12
elsif ($1 eq '>=') { } 2 7 9
elsif ($1 eq '<=') { } 2 5 7
elsif ($1 eq '!=') { } 2 3 5
elsif ($1 eq '==') { } 3 0 3
499 3 elsif ($1 eq '<' ) { $test = ($val < $2) }
500 2 elsif ($1 eq '>=') { $test = ($val >= $2) }
501 2 elsif ($1 eq '<=') { $test = ($val <= $2) }
502 2 elsif ($1 eq '!=') { $test = ($val != $2) }
503 3 elsif ($1 eq '==') { $test = ($val == $2) }
504  
505 } elsif ($comp =~ /^\s*(eq|ne|gt|ge|lt|le)\s+(.+?)\s*$/) {
506 15 ▶ my $val = defined($value) ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 12 3 15
507 15 my ($op, $value2) = ($1, $2);
508 15 $value2 =~ s/^([\"\'])(.*)\1$/$2/;
509 15 ▶ if ($op eq 'gt') { $test = ($val gt $value2) }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq 'gt') { } 3 12 15
elsif ($op eq 'lt') { } 3 9 12
elsif ($op eq 'ge') { } 2 7 9
elsif ($op eq 'le') { } 2 5 7
elsif ($op eq 'ne') { } 2 3 5
elsif ($op eq 'eq') { } 3 0 3
510 3 elsif ($op eq 'lt') { $test = ($val lt $value2) }
511 2 elsif ($op eq 'ge') { $test = ($val ge $value2) }
512 2 elsif ($op eq 'le') { $test = ($val le $value2) }
513 2 elsif ($op eq 'ne') { $test = ($val ne $value2) }
514 3 elsif ($op eq 'eq') { $test = ($val eq $value2) }
515  
516 } else {
517 0 die "Not sure how to compare \"$comp\"";
518 }
519 30 ▶ if (! $test) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($test) 16 14 30
520 16 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 16 16
521 16 push @errors, [$field, $type, $field_val, $ifs_match];
522 }
523 }
524 30 $content_checked = 1;
525 } }
526  
527 # server side sql type
528 171 ▶ if ($types{'sql'}) { foreach my $type (@{ $types{'sql'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"sql"}) 0 171 171
529 0 my $db_type = $field_val->{"${type}_db_type"};
530 0 ▶ my $dbh = ($db_type) ? $self->{dbhs}->{$db_type} : $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
$db_type ? : 0 0 0
531 0 ▶ if (! $dbh) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $dbh) { } 0 0 0
elsif (UNIVERSAL::isa($dbh, 'CODE')) { } 0 0 0
532 0 ▶ die "Missing dbh for $type type on field $field" . ($db_type ? " and db_type $db_type" : "");
Branch: 1 branch
BranchTrueFalseTotal
$db_type ? : 0 0 0
533 } elsif (UNIVERSAL::isa($dbh,'CODE')) {
534 0 ▶ $dbh = &$dbh($field, $self) || die "SQL Coderef did not return a dbh";
Condition0%0/2
&$dbh($field, $self) || die('SQL Coderef did not return a dbh')
l!l
00
MC/DC0%0/1
&$dbh($field, $self) || die('SQL Coderef did not return a dbh')
&$dbh($field, $self)
Truth table0%0/2
A
A= &$dbh($field, $self)
Aresult
11
00
535 }
536 0 my $sql = $field_val->{$type};
537 0 my @args = ($value) x $sql =~ tr/?//;
538 0 my $return = $dbh->selectrow_array($sql, {}, @args); # is this right - copied from O::FORMS
539 0 ▶ $field_val->{"${type}_error_if"} = 1 if ! defined $field_val->{"${type}_error_if"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$field_val{"${type}_error_if"} 0 0 0
540 0 ▶ if ( (! $return && $field_val->{"${type}_error_if"})
Branch: 1 branch
BranchTrueFalseTotal
if (not $return and $$field_val{"${type}_error_if"} or $return and not $$field_val{"${type}_error_if"}) 0 0 0
Condition0%0/9
not $return and $$field_val{"${type}_error_if"} or $return and not $$field_val{"${type}_error_if"}
l!l&&r!l&&!r
000
not $return and $$field_val{"${type}_error_if"}
!ll&&!rl&&r
000
$return and not $$field_val{"${type}_error_if"}
!ll&&!rl&&r
000
MC/DC0%0/4
not $return and $$field_val{"${type}_error_if"} or $return and not $$field_val{"${type}_error_if"}
not $return $$field_val{"${type}_error_if"} $return not $$field_val{"${type}_error_if"}
Truth table0%0/7
not(A) and B or C and not(D)
A= not $returnB= $$field_val{"${type}_error_if"}C= $returnD= not $$field_val{"${type}_error_if"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
541 || ($return && ! $field_val->{"${type}_error_if"}) ) {
542 0 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 0 0
543 0 push @errors, [$field, $type, $field_val, $ifs_match];
544 }
545 0 $content_checked = 1;
546 } }
547  
548 # server side custom type
549 171 ▶ if ($types{'custom'}) { foreach my $type (@{ $types{'custom'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{"custom"}) 7 164 171
550 7 my $check = $field_val->{$type};
551 7 my $err;
552 7 ▶ if (UNIVERSAL::isa($check, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($check, 'CODE')) { } 3 4 7
553 3 my $ok;
554 3 ▶ $err = "$@" if ! eval { $ok = $check->($field, $value, $field_val, $type, $form); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $ok = &$check($field, $value, $field_val, $type, $form); 1 } } 1 2 3
555 3 ▶ next if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 2 3
556 2 ▶ chomp($err) if !ref($@) && defined($err);
Branch: 1 branch
BranchTrueFalseTotal
if not ref $@ and defined $err 1 1 2
Condition66%2/3
not ref $@ and defined $err
!ll&&!rl&&r
011
MC/DC50%1/2
not ref $@ and defined $err
not ref $@ defined $err
Truth table66%2/3
not(A) and B
A= not ref $@B= defined $err
ABresult
0X0
100
111
557 } else {
558 4 ▶ next if $check;
Branch: 1 branch
BranchTrueFalseTotal
if $check 2 2 4
559 }
560 4 ▶ return [] if $self->{'_check_conditional'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_check_conditional"} 0 4 4
561 4 ▶ push @errors, [$field, $type, $field_val, $ifs_match, (defined($err) ? $err : ())];
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 1 3 4
562 4 $content_checked = 1;
563 } }
564  
565 }
566  
567 # allow for the data to be "untainted"
568 # this is only allowable if the user ran some other check for the datatype
569 206 ▶ if ($field_val->{'untaint'} && $#errors == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"untaint"} and $#errors == -1) 2 204 206
Condition66%2/3
$field_val->{"untaint"} and $#errors == -1
!ll&&!rl&&r
20402
MC/DC50%1/2
$field_val->{"untaint"} and $#errors == -1
$field_val->{"untaint"} $#errors == -1
Truth table66%2/3
A and B
A= $field_val->{"untaint"}B= $#errors == -1
ABresult
0X0
100
111
570 2 ▶ if (! $content_checked) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $content_checked) { } 1 1 2
571 1 push @errors, [$field, 'untaint', $field_val, $ifs_match];
572 } else {
573 # generic untainter - assuming the other required content_checks did good validation
574 1 ▶ $_ = /(.*)/ ? $1 : die "Couldn't match?" foreach @$values;
Branch: 1 branch
BranchTrueFalseTotal
/(.*)/ ? : 1 0 1
575 1 ▶ if ($n_values == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_values == 1) 1 0 1
576 1 $form->{$field} = $values->[0];
577 }
578 }
579 }
580  
581 # all done - time to return
582 206 ▶ return @errors ? \@errors : 0;
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 76 130 206
583 }
584  
585 ###---------------------###
586  
587 ### used to validate specific types
588 sub check_type {
589 65 ▶ my ($self, $value, $type) = @_;
Subroutine check_type: called CC=49 SCAR=66.1
Pod undocumented
590 65 $type = lc $type;
591 65 ▶ if ($type eq 'email') {
Branch: 10 branches
BranchTrueFalseTotal
if ($type eq 'email') { } 0 65 65
elsif ($type eq 'local_part') { } 0 65 65
elsif ($type eq 'ip') { } 3 62 65
elsif ($type eq 'domain') { } 8 54 62
elsif ($type eq 'url') { } 0 54 54
elsif ($type eq 'uri') { } 0 54 54
elsif ($type eq 'int') { } 19 35 54
elsif ($type eq 'uint') { } 16 19 35
elsif ($type eq 'num') { } 19 0 19
elsif ($type eq 'cc') { } 0 0 0
592 0 ▶ return 0 if ! $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
593 0 ▶ my ($local_p,$dom) = ($value =~ /^(.+)\@(.+?)$/) ? ($1,$2) : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^(.+)\@(.+?)$/ ? : 0 0 0
594 0 ▶ return 0 if length($local_p) > 60;
Branch: 1 branch
BranchTrueFalseTotal
if length $local_p > 60 0 0 0
595 0 ▶ return 0 if length($dom) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $dom > 100 0 0 0
596 0 ▶ return 0 if ! $self->check_type($dom,'domain') && ! $self->check_type($dom,'ip');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($dom, "domain") or $self->check_type($dom, "ip") 0 0 0
Condition0%0/3
$self->check_type($dom, "domain") or $self->check_type($dom, "ip")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->check_type($dom, "domain") or $self->check_type($dom, "ip")
$self->check_type($dom, "domain") $self->check_type($dom, "ip")
Truth table0%0/3
A or B
A= $self->check_type($dom, "domain")B= $self->check_type($dom, "ip")
ABresult
1X1
011
000
597 0 ▶ return 0 if ! $self->check_type($local_p,'local_part');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($local_p, "local_part") 0 0 0
598  
599 # the "username" portion of an email address - sort of arbitrary
600 } elsif ($type eq 'local_part') {
601 0 ▶ return 0 if ! defined($value) || ! length($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
602 # ignoring all valid quoted string local parts
603 0 ▶ return 0 if $value =~ m/[^\w.~!\#\$%\^&*\-=+?]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[^\w.~!\#\$%\^&*\-=+?]/ 0 0 0
604  
605 # standard IP address
606 } elsif ($type eq 'ip') {
607 3 ▶ return 0 if ! $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 3 3
608 3 ▶ return (4 == grep {!/\D/ && $_ < 256} split /\./, $value, 4);
Branch: 1 branch
BranchTrueFalseTotal
not /\D/ and $_ < 256 11 0 11
609  
610 # domain name - including tld and subdomains (which are all domains)
611 } elsif ($type eq 'domain') {
612 8 ▶ return 0 if ! $value || length($value) > 255;
Branch: 1 branch
BranchTrueFalseTotal
if not $value or length $value > 255 0 8 8
Condition33%1/3
not $value or length $value > 255
l!l&&r!l&&!r
008
MC/DC0%0/2
not $value or length $value > 255
not $value length $value > 255
Truth table33%1/3
not(A) or B
A= not $valueB= length $value > 255
ABresult
1X1
011
000
613 8 ▶ return 0 if $value !~ /^([a-z0-9][a-z0-9\-]{0,62} \.)+ [a-z]{1,63}$/ix
Branch: 1 branch
BranchTrueFalseTotal
if not $value =~ /^([a-z0-9][a-z0-9\-]{0,62} \.)+ [a-z]{1,63}$/xi or $value =~ /(\.\-|\-\.|\.\.)/ 4 4 8
Condition100%3/3
not $value =~ /^([a-z0-9][a-z0-9\-]{0,62} \.)+ [a-z]{1,63}$/xi or $value =~ /(\.\-|\-\.|\.\.)/
l!l&&r!l&&!r
314
MC/DC100%2/2
not $value =~ /^([a-z0-9][a-z0-9\-]{0,62} \.)+ [a-z]{1,63}$/xi or $value =~ /(\.\-|\-\.|\.\.)/
not $value =~ /^([a-z0-9][a-z0-9\-]{0,62} \.)+ [a-z]{1,63}$/xi $value =~ /(\.\-|\-\.|\.\.)/
Truth table100%3/3
not(A) or B
A= not $value =~ /^([a-z0-9][a-z0-9\-]{0,62} \.)+ [a-z]{1,63}$/xiB= $value =~ /(\.\-|\-\.|\.\.)/
ABresult
1X1
011
000
614 || $value =~ m/(\.\-|\-\.|\.\.)/;
615  
616 # validate a url
617 } elsif ($type eq 'url') {
618 0 ▶ return 0 if ! $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
619 0 ▶ $value =~ s|^https?://([^/]+)||i || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ s[^https?://([^/]+)][]i or return 0 0 0 0
620 0 my $dom = $1;
621 0 ▶ return 0 if ! $self->check_type($dom,'domain') && ! $self->check_type($dom,'ip');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($dom, "domain") or $self->check_type($dom, "ip") 0 0 0
Condition0%0/3
$self->check_type($dom, "domain") or $self->check_type($dom, "ip")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->check_type($dom, "domain") or $self->check_type($dom, "ip")
$self->check_type($dom, "domain") $self->check_type($dom, "ip")
Truth table0%0/3
A or B
A= $self->check_type($dom, "domain")B= $self->check_type($dom, "ip")
ABresult
1X1
011
000
622 0 ▶ return 0 if $value && ! $self->check_type($value,'uri');
Branch: 1 branch
BranchTrueFalseTotal
if $value and not $self->check_type($value, "uri") 0 0 0
Condition0%0/3
$value and not $self->check_type($value, "uri")
!ll&&!rl&&r
000
MC/DC0%0/2
$value and not $self->check_type($value, "uri")
$value not $self->check_type($value, "uri")
Truth table0%0/3
A and not(B)
A= $valueB= not $self->check_type($value, "uri")
ABresult
0X0
100
111
623  
624 # validate a uri - the path portion of a request
625 } elsif ($type eq 'uri') {
626 0 ▶ return 0 if ! $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
627 0 ▶ return 0 if $value =~ m/\s+/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\s+/ 0 0 0
628  
629 } elsif ($type eq 'int') {
630 19 ▶ return 0 if $value !~ /^-? (?: 0 | [1-9]\d*) $/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^-? (?: 0 | [1-9]\d*) $/x 9 10 19
631 10 ▶ return 0 if ($value < 0) ? $value < -2**31 : $value > 2**31-1;
Branch: 2 branches
BranchTrueFalseTotal
if $value < 0 ? $value < "-2147483648" : $value > 2147483647 2 8 10
$value < 0 ? : 4 6 10
632 } elsif ($type eq 'uint') {
633 16 ▶ return 0 if $value !~ /^ (?: 0 | [1-9]\d*) $/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^ (?: 0 | [1-9]\d*) $/x 11 5 16
634 5 ▶ return 0 if $value > 2**32-1;
Branch: 1 branch
BranchTrueFalseTotal
if $value > 4294967295 1 4 5
635 } elsif ($type eq 'num') {
636 19 ▶ return 0 if $value !~ /^-? (?: 0 | [1-9]\d* (?:\.\d+)? | 0?\.\d+) $/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^-? (?: 0 | [1-9]\d* (?:\.\d+)? | 0?\.\d+) $/x 7 12 19
637  
638 } elsif ($type eq 'cc') {
639 0 ▶ return 0 if ! $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
640 0 ▶ return 0 if $value =~ /[^\d\-\ ]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[^\d\-\ ]/ 0 0 0
641 0 $value =~ s/\D//g;
642 0 ▶ return 0 if length($value) > 16 || length($value) < 13;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > 16 or length $value < 13 0 0 0
Condition0%0/3
length $value > 16 or length $value < 13
l!l&&r!l&&!r
000
MC/DC0%0/2
length $value > 16 or length $value < 13
length $value > 16 length $value < 13
Truth table0%0/3
A or B
A= length $value > 16B= length $value < 13
ABresult
1X1
011
000
643  
644 # simple mod10 check
645 0 my $sum = 0;
646 0 my $switch = 0;
647 0 foreach my $digit (reverse split //, $value) {
648 0 ▶ $switch = 1 if ++$switch > 2;
Branch: 1 branch
BranchTrueFalseTotal
if ++$switch > 2 0 0 0
649 0 my $y = $digit * $switch;
650 0 ▶ $y -= 9 if $y > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $y > 9 0 0 0
651 0 $sum += $y;
652 }
653 0 ▶ return 0 if $sum % 10;
Branch: 1 branch
BranchTrueFalseTotal
if $sum % 10 0 0 0
654  
655 }
656  
657 28 return 1;
658 }
659  
660 ###---------------------###
661  
662 sub get_validation {
663 1 ▶ my ($self, $val) = @_;
Subroutine get_validation: called CC=1 SCAR=0.0
Pod documented
664 1 require CGI::Ex::Conf;
665 1 return CGI::Ex::Conf::conf_read($val, {html_key => 'validation', default_ext => 'val'});
666 }
667  
668 ### returns all keys from all groups - even if group has validate_if
669 sub get_validation_keys {
670 0 ▶ my ($self, $val_hash, $form) = @_; # with optional form - will only return keys in validated groups
Subroutine get_validation_keys: not called CC=9 SCAR=45.0
Pod documented
671  
672 0 ▶ if ($form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form) 0 0 0
673 0 ▶ die "Invalid form hash or cgi object" if ! ref $form;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $form 0 0 0
674 0 ▶ $form = $self->cgix->get_form($form) if ref $form ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $form ne "HASH" 0 0 0
675 }
676  
677 0 my ($fields, $ARGS) = $self->get_ordered_fields($val_hash);
678 0 ▶ return {} if ! @$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @$fields 0 0 0
679 0 ▶ return {} if $form && $ARGS->{'validate_if'} && ! $self->check_conditional($form, $ARGS->{'validate_if'});
Branch: 1 branch
BranchTrueFalseTotal
if $form and $ARGS->{"validate_if"} and not $self->check_conditional($form, $ARGS->{"validate_if"}) 0 0 0
Condition0%0/6
$form and $ARGS->{"validate_if"} and not $self->check_conditional($form, $ARGS->{"validate_if"})
!ll&&!rl&&r
000
$form and $ARGS->{"validate_if"}
!ll&&!rl&&r
000
MC/DC0%0/3
$form and $ARGS->{"validate_if"} and not $self->check_conditional($form, $ARGS->{"validate_if"})
$form $ARGS->{"validate_if"} not $self->check_conditional($form, $ARGS->{"validate_if"})
Truth table0%0/4
A and B and not(C)
A= $formB= $ARGS->{"validate_if"}C= not $self->check_conditional($form, $ARGS->{"validate_if"})
ABCresult
0XX0
10X0
1100
1111
680 0 ▶ return {map { $_->{'field'} = $_->{'name'} || 1 } @$fields};
Condition0%0/2
$_->{'name'} || 1
l!l
00
MC/DC0%0/1
$_->{'name'} || 1
$_->{'name'}
Truth table0%0/2
A
A= $_->{'name'}
Aresult
11
00
681 }
682  
683 ###---------------------###
684  
685 sub generate_js {
686 return "<!-- JS validation not supported in this browser $_ -->"
687 2 ▶ if $ENV{'HTTP_USER_AGENT'} && grep {$ENV{'HTTP_USER_AGENT'} =~ $_} @UNSUPPORTED_BROWSERS;
Subroutine generate_js: called CC=15 SCAR=38.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HTTP_USER_AGENT"} and grep {$ENV{"HTTP_USER_AGENT"} =~ /$_/;} @UNSUPPORTED_BROWSERS 0 2 2
Condition33%1/3
$ENV{"HTTP_USER_AGENT"} and grep {$ENV{"HTTP_USER_AGENT"} =~ /$_/;} @UNSUPPORTED_BROWSERS
!ll&&!rl&&r
200
MC/DC0%0/2
$ENV{"HTTP_USER_AGENT"} and grep {$ENV{"HTTP_USER_AGENT"} =~ /$_/;} @UNSUPPORTED_BROWSERS
$ENV{"HTTP_USER_AGENT"} grep {$ENV{"HTTP_USER_AGENT"} =~ /$_/;} @UNSUPPORTED_BROWSERS
Truth table33%1/3
A and B
A= $ENV{"HTTP_USER_AGENT"}B= grep {$ENV{"HTTP_USER_AGENT"} =~ /$_/;} @UNSUPPORTED_BROWSERS
ABresult
0X0
100
111
688  
689 2 my $self = shift;
690 2 ▶ my $val_hash = shift || croak "Missing validation hash";
Condition33%1/3
shift() || croak('Missing validation hash')
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || croak('Missing validation hash')
shift() croak('Missing validation hash')
Truth table33%1/3
A || B
A= shift()B= croak('Missing validation hash')
ABresult
1X1
011
000
691 2 ▶ if (ref $val_hash ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val_hash ne "HASH") 0 2 2
692 0 ▶ $val_hash = $self->get_validation($val_hash) if ref $val_hash ne 'SCALAR' || ! ref $val_hash;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val_hash ne "SCALAR" or not ref $val_hash 0 0 0
Condition0%0/3
ref $val_hash ne "SCALAR" or not ref $val_hash
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $val_hash ne "SCALAR" or not ref $val_hash
ref $val_hash ne "SCALAR" not ref $val_hash
Truth table0%0/3
A or not(B)
A= ref $val_hash ne "SCALAR"B= not ref $val_hash
ABresult
1X1
011
000
693 0 ▶ croak "Validation groups must be a hashref" if ref $val_hash ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val_hash ne "HASH" 0 0 0
694 }
695  
696 2 my ($args, $form_name, $js_uri_path);
697 2 ▶ croak "Missing args or form_name" if ! $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 2 2
698 2 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
699 0 $args = shift;
700 } else {
701 2 ($args, $form_name, $js_uri_path) = ({}, @_);
702 }
703  
704 2 ▶ $form_name ||= $args->{'form_name'} || croak 'Missing form_name';
Condition16%1/6
$form_name ||= $args->{"form_name"} || croak("Missing form_name")
l!l&&r!l&&!r
200
$args->{'form_name'} || croak('Missing form_name')
l!l&&r!l&&!r
000
MC/DC0%0/3
$form_name ||= $args->{"form_name"} || croak("Missing form_name")
$form_name $args->{'form_name'} croak('Missing form_name')
Truth table0%0/4
A ||= B || C
A= $form_nameB= $args->{'form_name'}C= croak('Missing form_name')
ABCresult
1XX1
01X1
0011
0000
705 2 ▶ $js_uri_path ||= $args->{'js_uri_path'};
Condition33%1/3
$js_uri_path ||= $args->{"js_uri_path"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$js_uri_path ||= $args->{"js_uri_path"}
$js_uri_path $args->{"js_uri_path"}
Truth table33%1/3
A ||= B
A= $js_uri_pathB= $args->{"js_uri_path"}
ABresult
1X1
011
000
706  
707 2 ▶ my $js_uri_path_validate = $JS_URI_PATH_VALIDATE || do {
Condition33%1/3
$JS_URI_PATH_VALIDATE || do { croak('Missing js_uri_path') unless $js_uri_path; "$js_uri_path/CGI/Ex/validate.js" }
l!l&&r!l&&!r
020
MC/DC0%0/2
$JS_URI_PATH_VALIDATE || do { croak('Missing js_uri_path') unless $js_uri_path; "$js_uri_path/CGI/Ex/validate.js" }
$JS_URI_PATH_VALIDATE do { croak('Missing js_uri_path') unless $js_uri_path; "$js_uri_path/CGI/Ex/validate.js" }
Truth table33%1/3
A || B
A= $JS_URI_PATH_VALIDATEB= do { croak('Missing js_uri_path') unless $js_uri_path; "$js_uri_path/CGI/Ex/validate.js" }
ABresult
1X1
011
000
708 2 ▶ croak 'Missing js_uri_path' if ! $js_uri_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $js_uri_path 0 2 2
709 2 "$js_uri_path/CGI/Ex/validate.js";
710 };
711  
712 2 require CGI::Ex::JSONDump;
713 2 my $json = CGI::Ex::JSONDump->new({pretty => 1})->dump($val_hash);
714 2 return qq{<script src="$js_uri_path_validate"></script>
715 <script>
716 document.validation = $json;
717 if (document.check_form) document.check_form("$form_name");
718 </script>
719 };
720 }
721  
722 sub generate_form {
723 0 ▶ my ($self, $val_hash, $form_name, $args) = @_;
Subroutine generate_form: not called CC=60 SCAR=82.1
Pod documented
724 0 ▶ ($args, $form_name) = ($form_name, undef) if ref($form_name) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $form_name eq "HASH" 0 0 0
725  
726 0 my ($fields, $ARGS) = $self->get_ordered_fields($val_hash);
727 0 ▶ $args = {%{ $ARGS->{'form_args'} || {}}, %{ $args || {} }};
Branch: 2 branches
BranchTrueFalseTotal
$ARGS->{"form_args"} or {} 0 0 0
$args or {} 0 0 0
728  
729 0 ▶ my $cols = ($args->{'no_inline_error'} || ! $args->{'columns'} || $args->{'columns'} != 3) ? 2 : 3;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'no_inline_error'} || !$args->{'columns'} || $args->{'columns'} != 3 ? : 0 0 0
Condition0%0/6
$args->{"no_inline_error"} or not $args->{"columns"} or $args->{"columns"} != 3
l!l&&r!l&&!r
000
$args->{"no_inline_error"} or not $args->{"columns"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args->{"no_inline_error"} or not $args->{"columns"} or $args->{"columns"} != 3
$args->{"no_inline_error"} not $args->{"columns"} $args->{"columns"} != 3
Truth table0%0/4
A or not(B) or C
A= $args->{"no_inline_error"}B= not $args->{"columns"}C= $args->{"columns"} != 3
ABCresult
1XX1
01X1
0011
0000
730 0 ▶ $args->{'div'} ||= "<div class=\"form_div\">\n";
Condition0%0/2
$args->{"div"} ||= "<div class=\"form_div\">\n"
l!l
00
MC/DC0%0/1
$args->{"div"} ||= "<div class=\"form_div\">\n"
$args->{"div"}
Truth table0%0/2
A
A= $args->{"div"}
Aresult
11
00
731 0 ▶ $args->{'open'} ||= "<form name=\"\$form_name\" id=\"\$form_name\" method=\"\$method\" action=\"\$action\"\$extra_form_attrs>\n";
Condition0%0/2
$args->{"open"} ||= "<form name=\"\$form_name\" id=\"\$form_name\" method=\"\$method\" action=\"\$action\"\$extra_form_attrs>\n"
l!l
00
MC/DC0%0/1
$args->{"open"} ||= "<form name=\"\$form_name\" id=\"\$form_name\" method=\"\$method\" action=\"\$action\"\$extra_form_attrs>\n"
$args->{"open"}
Truth table0%0/2
A
A= $args->{"open"}
Aresult
11
00
732 0 ▶ $args->{'form_name'} ||= $form_name || 'the_form_'.int(rand * 1000);
Condition0%0/5
$args->{"form_name"} ||= $form_name || 'the_form_' . int(rand *1000)
l!l&&r!l&&!r
000
$form_name || 'the_form_' . int(rand *1000)
l!l
00
MC/DC0%0/2
$args->{"form_name"} ||= $form_name || 'the_form_' . int(rand *1000)
$args->{"form_name"} $form_name
Truth table0%0/3
A ||= B
A= $args->{"form_name"}B= $form_name
ABresult
1X1
011
000
733 0 ▶ $args->{'action'} ||= '';
Condition0%0/2
$args->{"action"} ||= ""
l!l
00
MC/DC0%0/1
$args->{"action"} ||= ""
$args->{"action"}
Truth table0%0/2
A
A= $args->{"action"}
Aresult
11
00
734 0 ▶ $args->{'method'} ||= 'POST';
Condition0%0/2
$args->{"method"} ||= "POST"
l!l
00
MC/DC0%0/1
$args->{"method"} ||= "POST"
$args->{"method"}
Truth table0%0/2
A
A= $args->{"method"}
Aresult
11
00
735 0 ▶ $args->{'submit'} ||= "<input type=\"submit\" value=\"".($args->{'submit_name'} || 'Submit')."\">";
Condition0%0/4
$args->{"submit"} ||= '<input type="submit" value="' . ($args->{"submit_name"} || "Submit") . '">'
l!l
00
$args->{'submit_name'} || 'Submit'
l!l
00
MC/DC0%0/1
$args->{"submit"} ||= '<input type="submit" value="' . ($args->{"submit_name"} || "Submit") . '">'
$args->{"submit"}
MC/DC0%0/1
$args->{'submit_name'} || 'Submit'
$args->{'submit_name'}
Truth table0%0/2
A
A= $args->{"submit"}
Aresult
11
00
Truth table0%0/2
A
A= $args->{'submit_name'}
Aresult
11
00
736 0 ▶ $args->{'header'} ||= "<table class=\"form_table\">\n";
Condition0%0/2
$args->{"header"} ||= "<table class=\"form_table\">\n"
l!l
00
MC/DC0%0/1
$args->{"header"} ||= "<table class=\"form_table\">\n"
$args->{"header"}
Truth table0%0/2
A
A= $args->{"header"}
Aresult
11
00
737 0 ▶ $args->{'header'} .= " <tr class=\"header\"><th colspan=\"$cols\">\$title</th></tr>\n" if $args->{'title'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"title"} 0 0 0
738 0 ▶ $args->{'footer'} ||= " <tr class=\"submit_row\"><th colspan=\"2\">\$submit</th></tr>\n</table>\n";
Condition0%0/2
$args->{"footer"} ||= " <tr class=\"submit_row\"><th colspan=\"2\">\$submit</th></tr>\n</table>\n"
l!l
00
MC/DC0%0/1
$args->{"footer"} ||= " <tr class=\"submit_row\"><th colspan=\"2\">\$submit</th></tr>\n</table>\n"
$args->{"footer"}
Truth table0%0/2
A
A= $args->{"footer"}
Aresult
11
00
739 0 ▶ $args->{'extra_form_attrs'} ||= '';
Condition0%0/2
$args->{"extra_form_attrs"} ||= ""
l!l
00
MC/DC0%0/1
$args->{"extra_form_attrs"} ||= ""
$args->{"extra_form_attrs"}
Truth table0%0/2
A
A= $args->{"extra_form_attrs"}
Aresult
11
00
740 $args->{'row_template'} ||= " <tr class=\"\$oddeven\" id=\"\$field_row\">\n"
741 ." <td class=\"field\">\$name</td>\n"
742 ." <td class=\"input\">\$input"
743 . ($cols == 2
744 0 ▶ ? ($args->{'no_inline_error'} ? '' : "<br /><span class=\"error\" id=\"\$field_error\">[% \$field_error %]</span></td>\n")
Branch: 2 branches
BranchTrueFalseTotal
$cols == 2 ? : 0 0 0
$args->{'no_inline_error'} ? : 0 0 0
Condition0%0/2
$args->{"row_template"} ||= qq[ <tr class="\$oddeven" id="\$field_row">\n <td class="field">\$name</td>\n <td class="input">\$input] . ($cols == 2 ? $args->{"no_inline_error"} ? "" : "<br /><span class=\"error\" id=\"\$field_error\">[% \$field_error %]</span></td>\n" : "</td>\n <td class=\"error\" id=\"\$field_error\">[% \$field_error %]</td>\n") . " </tr>\n"
l!l
00
MC/DC0%0/1
$args->{"row_template"} ||= qq[ <tr class="\$oddeven" id="\$field_row">\n <td class="field">\$name</td>\n <td class="input">\$input] . ($cols == 2 ? $args->{"no_inline_error"} ? "" : "<br /><span class=\"error\" id=\"\$field_error\">[% \$field_error %]</span></td>\n" : "</td>\n <td class=\"error\" id=\"\$field_error\">[% \$field_error %]</td>\n") . " </tr>\n"
$args->{"row_template"}
Truth table0%0/2
A
A= $args->{"row_template"}
Aresult
11
00
745 : "</td>\n <td class=\"error\" id=\"\$field_error\">[% \$field_error %]</td>\n")
746 ." </tr>\n";
747  
748 0 ▶ my $js = ! defined($args->{'use_js_validation'}) || $args->{'use_js_validation'};
Condition0%0/3
!defined($args->{'use_js_validation'}) || $args->{'use_js_validation'}
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($args->{'use_js_validation'}) || $args->{'use_js_validation'}
!defined($args->{'use_js_validation'}) $args->{'use_js_validation'}
Truth table0%0/3
not(A) || B
A= !defined($args->{'use_js_validation'})B= $args->{'use_js_validation'}
ABresult
1X1
011
000
749  
750 $args->{'css'} = ".odd { background: #eee }\n"
751 . ".form_div { width: 40em; }\n"
752 . ".form_div td { padding:.5ex;}\n"
753 . ".form_div label { width: 10em }\n"
754 . ".form_div .error { color: darkred }\n"
755 . "table { border-spacing: 0px }\n"
756 . ".submit_row { text-align: right }\n"
757 0 ▶ if ! defined $args->{'css'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"css"} 0 0 0
758  
759 0 ▶ my $txt = ($args->{'css'} ? "<style>\n$args->{'css'}\n</style>\n" : '') . $args->{'div'} . $args->{'open'} . $args->{'header'};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'css'} ? : 0 0 0
760 0 s/\$(form_name|title|method|action|submit|extra_form_attrs)/$args->{$1}/g foreach $txt, $args->{'footer'};
761 0 my $n = 0;
762 0 foreach my $field (@$fields) {
763 0 my $input;
764 0 ▶ my $type = $field->{'htype'} ? $field->{'htype'} : $field->{'field'} =~ /^pass(?:|wd|word|\d+|_\w+)$/i ? 'password' : 'text';
Branch: 2 branches
BranchTrueFalseTotal
$field->{'htype'} ? : 0 0 0
$field->{'field'} =~ /^pass(?:|wd|word|\d+|_\w+)$/i ? : 0 0 0
765 0 ▶ if ($type eq 'hidden') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'hidden') { } 0 0 0
elsif ($type eq 'textarea' or $field->{'rows'} or $field->{'cols'}) { } 0 0 0
elsif ($type eq 'radio' or $type eq 'checkbox') { } 0 0 0
elsif ($type eq 'select' or $field->{'enum'} or $field->{'label'}) { } 0 0 0
766 0 $txt .= "$input\n";
767 0 next;
768 ▶ } elsif ($type eq 'textarea' || $field->{'rows'} || $field->{'cols'}) {
Condition0%0/6
$type eq "textarea" or $field->{"rows"} or $field->{"cols"}
l!l&&r!l&&!r
000
$type eq "textarea" or $field->{"rows"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "textarea" or $field->{"rows"} or $field->{"cols"}
$type eq "textarea" $field->{"rows"} $field->{"cols"}
Truth table0%0/4
A or B or C
A= $type eq "textarea"B= $field->{"rows"}C= $field->{"cols"}
ABCresult
1XX1
01X1
0011
0000
769 0 ▶ my $r = $field->{'rows'} ? " rows=\"$field->{'rows'}\"" : '';
Branch: 1 branch
BranchTrueFalseTotal
$field->{'rows'} ? : 0 0 0
770 0 ▶ my $c = $field->{'cols'} ? " cols=\"$field->{'cols'}\"" : '';
Branch: 1 branch
BranchTrueFalseTotal
$field->{'cols'} ? : 0 0 0
771 0 ▶ my $w = $field->{'wrap'} ? " wrap=\"$field->{'wrap'}\"" : '';
Branch: 1 branch
BranchTrueFalseTotal
$field->{'wrap'} ? : 0 0 0
772 0 $input = "<textarea name=\"$field->{'field'}\" id=\"$field->{'field'}\"$r$c$w></textarea>";
773 ▶ } elsif ($type eq 'radio' || $type eq 'checkbox') {
Condition0%0/3
$type eq "radio" or $type eq "checkbox"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "radio" or $type eq "checkbox"
$type eq "radio" $type eq "checkbox"
Truth table0%0/3
A or B
A= $type eq "radio"B= $type eq "checkbox"
ABresult
1X1
011
000
774 0 ▶ my $e = $field->{'enum'} || [];
Condition0%0/2
$field->{'enum'} || []
l!l
00
MC/DC0%0/1
$field->{'enum'} || []
$field->{'enum'}
Truth table0%0/2
A
A= $field->{'enum'}
Aresult
11
00
775 0 ▶ my $l = $field->{'label'} || $e;
Condition0%0/3
$field->{'label'} || $e
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->{'label'} || $e
$field->{'label'} $e
Truth table0%0/3
A || B
A= $field->{'label'}B= $e
ABresult
1X1
011
000
776 0 ▶ my $I = @$e > @$l ? $#$e : $#$l;
Branch: 1 branch
BranchTrueFalseTotal
@$e > @$l ? : 0 0 0
777 0 ▶ for (my $i = 0; $i <= $I; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $I) 0 0 0
778 0 my $_e = $e->[$i];
779 0 $_e =~ s/\"/&quot;/g;
780 0 ▶ $input .= "<div class=\"option\"><input type=\"$type\" name=\"$field->{'field'}\" id=\"$field->{'field'}_$i\" value=\"$_e\">"
Branch: 1 branch
BranchTrueFalseTotal
defined $l->[$i] ? : 0 0 0
781 .(defined($l->[$i]) ? $l->[$i] : '')."</div>\n";
782 }
783 ▶ } elsif ($type eq 'select' || $field->{'enum'} || $field->{'label'}) {
Condition0%0/6
$type eq "select" or $field->{"enum"} or $field->{"label"}
l!l&&r!l&&!r
000
$type eq "select" or $field->{"enum"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "select" or $field->{"enum"} or $field->{"label"}
$type eq "select" $field->{"enum"} $field->{"label"}
Truth table0%0/4
A or B or C
A= $type eq "select"B= $field->{"enum"}C= $field->{"label"}
ABCresult
1XX1
01X1
0011
0000
784 0 $input = "<select name=\"$field->{'field'}\" id=\"$field->{'field'}\">\n";
785 0 ▶ my $e = $field->{'enum'} || [];
Condition0%0/2
$field->{'enum'} || []
l!l
00
MC/DC0%0/1
$field->{'enum'} || []
$field->{'enum'}
Truth table0%0/2
A
A= $field->{'enum'}
Aresult
11
00
786 0 ▶ my $l = $field->{'label'} || $e;
Condition0%0/3
$field->{'label'} || $e
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->{'label'} || $e
$field->{'label'} $e
Truth table0%0/3
A || B
A= $field->{'label'}B= $e
ABresult
1X1
011
000
787 0 ▶ my $I = @$e > @$l ? $#$e : $#$l;
Branch: 1 branch
BranchTrueFalseTotal
@$e > @$l ? : 0 0 0
788 0 ▶ for (my $i = 0; $i <= $I; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $I) 0 0 0
789 0 ▶ $input .= " <option".(defined($e->[$i]) ? " value=\"".do { my $_e = $e->[$i]; $_e =~ s/\"/&quot;/g; $_e }.'"' : '').">"
Branch: 2 branches
BranchTrueFalseTotal
defined $e->[$i] ? : 0 0 0
defined $l->[$i] ? : 0 0 0
790 .(defined($l->[$i]) ? $l->[$i] : '')."</option>\n";
791 }
792 0 $input .= "</select>\n";
793 } else {
794 0 ▶ my $s = $field->{'size'} ? " size=\"$field->{'size'}\"" : '';
Branch: 1 branch
BranchTrueFalseTotal
$field->{'size'} ? : 0 0 0
795 0 ▶ my $m = $field->{'maxlength'} || $field->{'max_len'}; $m = $m ? " maxlength=\"$m\"" : '';
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
Condition0%0/3
$field->{'maxlength'} || $field->{'max_len'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->{'maxlength'} || $field->{'max_len'}
$field->{'maxlength'} $field->{'max_len'}
Truth table0%0/3
A || B
A= $field->{'maxlength'}B= $field->{'max_len'}
ABresult
1X1
011
000
796 0 $input = "<input type=\"$type\" name=\"$field->{'field'}\" id=\"$field->{'field'}\"$s$m value=\"\" />";
797 }
798  
799 0 $n++;
800 0 my $copy = $args->{'row_template'};
801 0 my $name = $field->{'field'};
802 0 ▶ $name = $field->{'name'} || do { $name =~ tr/_/ /; $name =~ s/\b(\w)/\u$1/g; $name };
Condition0%0/3
$field->{'name'} || do { $name =~ tr/_/ /; $name =~ s/\b(\w)/\u$1/g; $name }
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->{'name'} || do { $name =~ tr/_/ /; $name =~ s/\b(\w)/\u$1/g; $name }
$field->{'name'} do { $name =~ tr/_/ /; $name =~ s/\b(\w)/\u$1/g; $name }
Truth table0%0/3
A || B
A= $field->{'name'}B= do { $name =~ tr/_/ /; $name =~ s/\b(\w)/\u$1/g; $name }
ABresult
1X1
011
000
803 0 $name = "<label for=\"$field->{'field'}\">$name</label>";
804 0 $copy =~ s/\$field/$field->{'field'}/g;
805 0 $copy =~ s/\$name/$name/g;
806 0 $copy =~ s/\$input/$input/g;
807 0 ▶ $copy =~ s/\$oddeven/$n % 2 ? 'odd' : 'even'/eg;
Branch: 1 branch
BranchTrueFalseTotal
$n % 2 ? : 0 0 0
808 0 $txt .= $copy;
809 }
810 0 ▶ $txt .= $args->{'footer'} . ($args->{'close'} || "</form>\n") . ($args->{'div_close'} || "</div>\n");
Condition0%0/4
$args->{'close'} || "</form>\n"
l!l
00
$args->{'div_close'} || "</div>\n"
l!l
00
MC/DC0%0/1
$args->{'close'} || "</form>\n"
$args->{'close'}
MC/DC0%0/1
$args->{'div_close'} || "</div>\n"
$args->{'div_close'}
Truth table0%0/2
A
A= $args->{'close'}
Aresult
11
00
Truth table0%0/2
A
A= $args->{'div_close'}
Aresult
11
00
811 0 ▶ if ($js) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js) 0 0 0
812 0 local @{ $val_hash }{('general form_args', 'group form_args')};
813 0 delete @{ $val_hash }{('general form_args', 'group form_args')};
814 0 $txt .= $self->generate_js($val_hash, $args);
815 }
816 0 return $txt;
817 }
818  
819 ###---------------------###
820 ### How to handle errors
821  
822 package CGI::Ex::Validate::Error;
823  
824 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
825 9 ▶use overload '""' => \&as_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
826  
827 sub new {
828 107 ▶ my ($class, $errors, $extra) = @_;
Subroutine new: called CC=3 SCAR=11.4
829 107 ▶ die "Missing or invalid errors arrayref" if ref $errors ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $errors ne "ARRAY" 0 107 107
830 107 ▶ die "Missing or invalid extra hashref" if ref $extra ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $extra ne "HASH" 0 107 107
831 107 return bless {errors => $errors, extra => $extra}, $class;
832 }
833  
834 sub as_string {
835 119 ▶ my $self = shift;
Subroutine as_string: called CC=9 SCAR=24.0
836 119 ▶ my $extra = $self->{extra} || {};
Condition50%1/2
$self->{'extra'} || {}
l!l
1190
MC/DC0%0/1
$self->{'extra'} || {}
$self->{'extra'}
Truth table50%1/2
A
A= $self->{'extra'}
Aresult
11
00
837 119 ▶ my $extra2 = shift || {};
Condition50%1/2
shift() || {}
l!l
0119
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
838  
839 # allow for formatting
840 my $join = defined($extra2->{as_string_join}) ? $extra2->{as_string_join}
841 : defined($extra->{as_string_join}) ? $extra->{as_string_join}
842 119 ▶ : "\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_string_join'} ? : 0 119 119
defined $extra->{'as_string_join'} ? : 97 22 119
843 my $header = defined($extra2->{as_string_header}) ? $extra2->{as_string_header}
844 119 ▶ : defined($extra->{as_string_header}) ? $extra->{as_string_header} : "";
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_string_header'} ? : 0 119 119
defined $extra->{'as_string_header'} ? : 0 119 119
845 my $footer = defined($extra2->{as_string_footer}) ? $extra2->{as_string_footer}
846 119 ▶ : defined($extra->{as_string_footer}) ? $extra->{as_string_footer} : "";
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_string_footer'} ? : 0 119 119
defined $extra->{'as_string_footer'} ? : 0 119 119
847  
848 119 return $header . join($join, @{ $self->as_array($extra2) }) . $footer;
849 }
850  
851 sub as_array {
852 119 ▶ my $self = shift;
Subroutine as_array: called CC=16 SCAR=30.1
853 119 ▶ my $errors = $self->{errors} || die "Missing errors";
Condition50%1/2
$self->{'errors'} || die('Missing errors')
l!l
1190
MC/DC0%0/1
$self->{'errors'} || die('Missing errors')
$self->{'errors'}
Truth table50%1/2
A
A= $self->{'errors'}
Aresult
11
00
854 119 ▶ my $extra = $self->{extra} || {};
Condition50%1/2
$self->{'extra'} || {}
l!l
1190
MC/DC0%0/1
$self->{'extra'} || {}
$self->{'extra'}
Truth table50%1/2
A
A= $self->{'extra'}
Aresult
11
00
855 119 ▶ my $extra2 = shift || {};
Condition50%1/2
shift() || {}
l!l
1190
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
856  
857 my $title = defined($extra2->{as_array_title}) ? $extra2->{as_array_title}
858 : defined($extra->{as_array_title}) ? $extra->{as_array_title}
859 119 ▶ : "Please correct the following items:";
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_array_title'} ? : 0 119 119
defined $extra->{'as_array_title'} ? : 97 22 119
860  
861 # if there are heading items then we may end up needing a prefix
862 119 my $has_headings;
863 119 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) { } 22 97 119
864 22 $has_headings = 1;
865 } else {
866 97 foreach (@$errors) {
867 98 ▶ next if ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 98 0 98
868 0 $has_headings = 1;
869 0 last;
870 }
871 }
872  
873 my $prefix = defined($extra2->{as_array_prefix}) ? $extra2->{as_array_prefix}
874 : defined($extra->{as_array_prefix}) ? $extra->{as_array_prefix}
875 119 ▶ : $has_headings ? ' ' : '';
Branch: 3 branches
BranchTrueFalseTotal
defined $extra2->{'as_array_prefix'} ? : 0 119 119
defined $extra->{'as_array_prefix'} ? : 0 119 119
$has_headings ? : 22 97 119
876  
877 # get the array ready
878 119 my @array = ();
879 119 ▶ push @array, $title if length $title;
Branch: 1 branch
BranchTrueFalseTotal
if length $title 22 97 119
880  
881 # add the errors
882 119 my %found = ();
883 119 foreach my $err (@$errors) {
884 120 ▶ if (! ref $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $err) { } 0 120 120
885 0 push @array, $err;
886 0 %found = ();
887 } else {
888 120 my $text = $self->get_error_text($err);
889 120 ▶ next if $found{$text};
Branch: 1 branch
BranchTrueFalseTotal
if $found{$text} 0 120 120
890 120 $found{$text} = 1;
891 120 push @array, "$prefix$text";
892 }
893 }
894  
895 119 return \@array;
896 }
897  
898 sub as_hash {
899 6 ▶ my $self = shift;
Subroutine as_hash: called CC=21 SCAR=35.4
900 6 ▶ my $errors = $self->{errors} || die "Missing errors";
Condition50%1/2
$self->{'errors'} || die('Missing errors')
l!l
60
MC/DC0%0/1
$self->{'errors'} || die('Missing errors')
$self->{'errors'}
Truth table50%1/2
A
A= $self->{'errors'}
Aresult
11
00
901 6 ▶ my $extra = $self->{extra} || {};
Condition50%1/2
$self->{'extra'} || {}
l!l
60
MC/DC0%0/1
$self->{'extra'} || {}
$self->{'extra'}
Truth table50%1/2
A
A= $self->{'extra'}
Aresult
11
00
902 6 ▶ my $extra2 = shift || {};
Condition100%2/2
shift() || {}
l!l
42
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
903  
904 my $suffix = defined($extra2->{as_hash_suffix}) ? $extra2->{as_hash_suffix}
905 6 ▶ : defined($extra->{as_hash_suffix}) ? $extra->{as_hash_suffix} : '_error';
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_hash_suffix'} ? : 4 2 6
defined $extra->{'as_hash_suffix'} ? : 0 2 2
906 my $join = defined($extra2->{as_hash_join}) ? $extra2->{as_hash_join}
907 6 ▶ : defined($extra->{as_hash_join}) ? $extra->{as_hash_join} : '<br />';
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_hash_join'} ? : 4 2 6
defined $extra->{'as_hash_join'} ? : 0 2 2
908  
909 6 my %found;
910 my %return;
911 6 foreach my $err (@$errors) {
912 6 ▶ next if ! ref $err;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 0 6 6
913  
914 6 my ($field, $type, $field_val, $ifs_match) = @$err;
915 6 ▶ die "Missing field name" if ! $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 6 6
916 6 ▶ if ($field_val->{delegate_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"delegate_error"}) 0 6 6
917 0 $field = $field_val->{delegate_error};
918 0 ▶ $field =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 0 0 0
defined $ifs_match->[$1] ? : 0 0 0
919 }
920  
921 6 my $text = $self->get_error_text($err);
922 6 ▶ next if $found{$field}->{$text};
Branch: 1 branch
BranchTrueFalseTotal
if $found{$field}{$text} 0 6 6
923 6 $found{$field}->{$text} = 1;
924  
925 6 $field .= $suffix;
926 6 push @{ $return{$field} }, $text;
927 }
928  
929 6 ▶ if ($join) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join) 6 0 6
930 my $header = defined($extra2->{as_hash_header}) ? $extra2->{as_hash_header}
931 6 ▶ : defined($extra->{as_hash_header}) ? $extra->{as_hash_header} : "";
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_hash_header'} ? : 0 6 6
defined $extra->{'as_hash_header'} ? : 0 6 6
932 my $footer = defined($extra2->{as_hash_footer}) ? $extra2->{as_hash_footer}
933 6 ▶ : defined($extra->{as_hash_footer}) ? $extra->{as_hash_footer} : "";
Branch: 2 branches
BranchTrueFalseTotal
defined $extra2->{'as_hash_footer'} ? : 0 6 6
defined $extra->{'as_hash_footer'} ? : 0 6 6
934 6 foreach my $key (keys %return) {
935 6 $return{$key} = $header . join($join,@{ $return{$key} }) . $footer;
936 }
937 }
938  
939 6 return \%return;
940 }
941  
942 ### return a user friendly error message
943 sub get_error_text {
944 126 ▶ my $self = shift;
Subroutine get_error_text: called CC=49 SCAR=42.5
945 126 my $err = shift;
946 126 ▶ my $extra = $self->{extra} || {};
Condition50%1/2
$self->{'extra'} || {}
l!l
1260
MC/DC0%0/1
$self->{'extra'} || {}
$self->{'extra'}
Truth table50%1/2
A
A= $self->{'extra'}
Aresult
11
00
947 126 my ($field, $type, $field_val, $ifs_match, $custom_err) = @$err;
948 126 ▶ return $custom_err if defined($custom_err) && length($custom_err);
Branch: 1 branch
BranchTrueFalseTotal
if defined $custom_err and length $custom_err 2 124 126
Condition66%2/3
defined $custom_err and length $custom_err
!ll&&!rl&&r
12402
MC/DC50%1/2
defined $custom_err and length $custom_err
defined $custom_err length $custom_err
Truth table66%2/3
A and B
A= defined $custom_errB= length $custom_err
ABresult
0X0
100
111
949 124 ▶ my $dig = ($type =~ s/(_?\d+)$//) ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$type =~ s/(_?\d+)$// ? : 0 124 124
950 124 my $type_lc = lc($type);
951  
952 # allow for delegated field names - only used for defaults
953 124 ▶ if ($field_val->{delegate_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_val->{"delegate_error"}) 0 124 124
954 0 $field = $field_val->{delegate_error};
955 0 ▶ $field =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 0 0 0
defined $ifs_match->[$1] ? : 0 0 0
956 }
957  
958 # the the name of this thing
959 124 my $name = $field_val->{'name'};
960 124 ▶ $name = "The field $field" if ! $name && ($field =~ /\W/ || ($field =~ /\d/ && $field =~ /\D/));
Branch: 1 branch
BranchTrueFalseTotal
if not $name and $field =~ /\W/ || $field =~ /\d/ && $field =~ /\D/ 8 116 124
Condition66%6/9
not $name and $field =~ /\W/ || $field =~ /\d/ && $field =~ /\D/
!ll&&!rl&&r
09826
$field =~ /\W/ or $field =~ /\d/ and $field =~ /\D/
l!l&&r!l&&!r
02698
$field =~ /\d/ and $field =~ /\D/
!ll&&!rl&&r
11608
MC/DC0%0/4
not $name and $field =~ /\W/ || $field =~ /\d/ && $field =~ /\D/
not $name $field =~ /\W/ $field =~ /\d/ $field =~ /\D/
Truth table20%1/5
not(A) and B or C and D
A= not $nameB= $field =~ /\W/C= $field =~ /\d/D= $field =~ /\D/
ABCDresult
0XXX0
100X0
10100
11XX1
10111
961 124 ▶ if (! $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 116 8 124
962 116 $name = $field;
963 116 $name =~ tr/_/ /;
964 116 $name =~ s/\b(\w)/\u$1/g;
965 }
966 124 ▶ $name =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 4 120 124
defined $ifs_match->[$1] ? : 0 0 0
967  
968 # type can look like "required" or "required2" or "required100023"
969 # allow for fallback from required100023_error through required_error
970  
971 # look in the passed hash or self first
972 124 my $return;
973 124 ▶ foreach my $key ((length($dig) ? "${type}${dig}_error" : ()), "${type}_error", 'error') {
Branch: 1 branch
BranchTrueFalseTotal
length $dig ? : 0 124 124
974 236 ▶ $return = $field_val->{$key} || $extra->{$key} || next;
Condition80%4/5
$field_val->{$key} || $extra->{$key} || (next)
l!l
12224
$field_val->{$key} or $extra->{$key}
l!l&&r!l&&!r
120224
MC/DC50%1/2
$field_val->{$key} || $extra->{$key} || (next)
$field_val->{$key} $extra->{$key}
Truth table66%2/3
A or B
A= $field_val->{$key}B= $extra->{$key}
ABresult
1X1
011
000
975 12 ▶ $return =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 0 12 12
defined $ifs_match->[$1] ? : 0 0 0
976 12 $return =~ s/\$field/$field/g;
977 12 $return =~ s/\$name/$name/g;
978 12 ▶ if (my $value = $field_val->{"$type$dig"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $$field_val{"$type$dig"}) 12 0 12
979 12 ▶ $return =~ s/\$value/$value/g if ! ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 12 0 12
980 }
981 12 last;
982 }
983  
984 # set default messages
985 124 ▶ if (! $return) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($return) 112 12 124
986 112 ▶ if ($type eq 'required' || $type eq 'required_if') {
Branch: 16 branches
BranchTrueFalseTotal
if ($type eq 'required' or $type eq 'required_if') { } 19 93 112
elsif ($type eq 'min_values') { } 3 90 93
elsif ($type eq 'max_values') { } 2 88 90
elsif ($type eq 'enum') { } 4 84 88
elsif ($type eq 'equals') { } 5 79 84
elsif ($type eq 'min_len') { } 3 76 79
elsif ($type eq 'max_len') { } 1 75 76
elsif ($type eq 'max_in_set') { } 2 73 75
elsif ($type eq 'min_in_set') { } 3 70 73
elsif ($type eq 'match') { } 10 60 70
elsif ($type eq 'compare') { } 16 44 60
elsif ($type eq 'sql') { } 0 44 44
elsif ($type eq 'custom') { } 3 41 44
elsif ($type eq 'type') { } 36 5 41
elsif ($type eq 'untaint') { } 1 4 5
elsif ($type eq 'no_extra_fields') { } 4 0 4
Condition100%3/3
$type eq "required" or $type eq "required_if"
l!l&&r!l&&!r
18193
MC/DC100%2/2
$type eq "required" or $type eq "required_if"
$type eq "required" $type eq "required_if"
Truth table100%3/3
A or B
A= $type eq "required"B= $type eq "required_if"
ABresult
1X1
011
000
987 19 $return = "$name is required.";
988  
989 } elsif ($type eq 'min_values') {
990 3 my $n = $field_val->{"min_values${dig}"};
991 3 ▶ my $values = ($n == 1) ? 'value' : 'values';
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 0 3 3
992 3 $return = "$name had less than $n $values.";
993  
994 } elsif ($type eq 'max_values') {
995 2 my $n = $field_val->{"max_values${dig}"};
996 2 ▶ my $values = ($n == 1) ? 'value' : 'values';
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 1 1 2
997 2 $return = "$name had more than $n $values.";
998  
999 } elsif ($type eq 'enum') {
1000 4 $return = "$name is not in the given list.";
1001  
1002 } elsif ($type eq 'equals') {
1003 5 my $field2 = $field_val->{"equals${dig}"};
1004 5 ▶ my $name2 = $field_val->{"equals${dig}_name"} || "the field $field2";
Condition50%1/2
$$field_val{"equals${dig}_name"} || "the field $field2"
l!l
05
MC/DC0%0/1
$$field_val{"equals${dig}_name"} || "the field $field2"
$$field_val{"equals${dig}_name"}
Truth table50%1/2
A
A= $$field_val{"equals${dig}_name"}
Aresult
11
00
1005 5 ▶ $name2 =~ s/\$(\d+)/defined($ifs_match->[$1]) ? $ifs_match->[$1] : ''/eg if $ifs_match;
Branch: 2 branches
BranchTrueFalseTotal
if $ifs_match 1 4 5
defined $ifs_match->[$1] ? : 1 0 1
1006 5 $return = "$name did not equal $name2.";
1007  
1008 } elsif ($type eq 'min_len') {
1009 3 my $n = $field_val->{"min_len${dig}"};
1010 3 ▶ my $char = ($n == 1) ? 'character' : 'characters';
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 0 3 3
1011 3 $return = "$name was less than $n $char.";
1012  
1013 } elsif ($type eq 'max_len') {
1014 1 my $n = $field_val->{"max_len${dig}"};
1015 1 ▶ my $char = ($n == 1) ? 'character' : 'characters';
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 0 1 1
1016 1 $return = "$name was more than $n $char.";
1017  
1018 } elsif ($type eq 'max_in_set') {
1019 2 my $set = $field_val->{"max_in_set${dig}"};
1020 2 $return = "Too many fields were chosen from the set ($set)";
1021  
1022 } elsif ($type eq 'min_in_set') {
1023 3 my $set = $field_val->{"min_in_set${dig}"};
1024 3 $return = "Not enough fields were chosen from the set ($set)";
1025  
1026 } elsif ($type eq 'match') {
1027 10 $return = "$name contains invalid characters.";
1028  
1029 } elsif ($type eq 'compare') {
1030 16 $return = "$name did not fit comparison.";
1031  
1032 } elsif ($type eq 'sql') {
1033 0 $return = "$name did not match sql test.";
1034  
1035 } elsif ($type eq 'custom') {
1036 3 $return = "$name did not match custom test.";
1037  
1038 } elsif ($type eq 'type') {
1039 36 my $_type = $field_val->{"type${dig}"};
1040 36 $return = "$name did not match type $_type.";
1041  
1042 } elsif ($type eq 'untaint') {
1043 1 $return = "$name cannot be untainted without one of the following checks: enum, equals, match, compare, sql, type, custom";
1044  
1045 } elsif ($type eq 'no_extra_fields') {
1046 4 $return = "$name should not be passed to validate.";
1047 }
1048 }
1049  
1050 124 ▶ die "Missing error on field $field for type $type$dig" if ! $return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 124 124
1051 124 return $return;
1052  
1053 }
1054  
1055 9 1;
1056  
1057 ### See the perldoc in CGI/Ex/Validate.pod
↑ Summary Ex/Die.pm »