TUWF/Validate.pm

filter: Statementstmt 98.2% 221 / 225 Branchbran 93.3% 155 / 166 Conditioncond 86.9% 80 / 92 MC/DCmcdc 80.8% 38 / 47 Subroutinesub 97.6% 42 / 43 Podpod 0.0% 0 / 3 totaltotal 93.0% 536 / 576 CC 125 SCAR 26.9
CC
125
Cov
94%
_validate_input
26.2
compile
23.2
_validate_rec
23.0
CRAP 14.7
« TUWF/XML.pm ↑ Summary
1 package TUWF::Validate;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our @EXPORT_OK = qw/compile validate/;
10 3 our $VERSION = '1.6';
11  
12  
13 # Unavailable as custom validation names
14 3 my %builtin = map +($_,1), qw/
15 type
16 default
17 onerror
18 rmwhitespace
19 values scalar sort unique
20 keys unknown missing
21 func
22 /;
23  
24 3 my %type_vals = map +($_,1), qw/scalar hash array any/;
25 3 my %unknown_vals = map +($_,1), qw/remove reject pass/;
26 3 my %missing_vals = map +($_,1), qw/create reject ignore/;
27  
28 99 ▶sub _length {
Subroutine _length: called CC=1 SCAR=0.0
29 99 my($exp, $min, $max) = @_;
30 94 ▶ +{ _analyze_minlength => $min, _analyze_maxlength => $max, func => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
31 94 ▶ my $got = ref $_[0] eq 'HASH' ? keys %{$_[0]} : ref $_[0] eq 'ARRAY' ? @{$_[0]} : length $_[0];
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 6 88 94
ref $_[0] eq 'ARRAY' ? : 4 84 88
32 94 ▶ (!defined $min || $got >= $min) && (!defined $max || $got <= $max) ? 1 : { expected => $exp, got => $got };
Branch: 1 branch
BranchTrueFalseTotal
(!defined($min) || $got >= $min) && (!defined($max) || $got <= $max) ? : 78 16 94
Condition100%9/9
!defined($min) || $got >= $min and !defined($max) || $got <= $max
!ll&&!rl&&r
41278
not defined $min or $got >= $min
l!l&&r!l&&!r
68224
not defined $max or $got <= $max
l!l&&r!l&&!r
47412
MC/DC100%4/4
!defined($min) || $got >= $min and !defined($max) || $got <= $max
not defined $min $got >= $min not defined $max $got <= $max
Truth table85%6/7
not(A) or B and not(C) or D
A= not defined $minB= $got >= $minC= not defined $maxD= $got <= $max
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
33 }}
34 99 }
35  
36 # Basically the same as ( regex => $arg ), but hides the regex error
37 27 ▶sub _reg {
Subroutine _reg: called CC=1 SCAR=0.0
38 27 my $reg = $_[0];
39 27 ▶ ( type => 'scalar', _analyze_regex => "$reg", func => sub { $_[0] =~ $reg ? 1 : { got => $_[0] } } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$reg/ ? : 64 64 128
40 }
41  
42  
43 3 our $re_num = qr/^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/;
44 3 my $re_int = qr/^-?(?:0|[1-9][0-9]*)$/;
45 3 our $re_uint = qr/^(?:0|[1-9][0-9]*)$/;
46 3 my $re_fqdn = qr/(?:[a-zA-Z0-9][\w-]*\.)+[a-zA-Z][a-zA-Z0-9-]{1,25}\.?/;
47 3 my $re_ip4_digit = qr/(?:0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/;
48 3 my $re_ip4 = qr/($re_ip4_digit\.){3}$re_ip4_digit/;
49 # This monstrosity is based on http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
50 # Doesn't allow IPv4-mapped-IPv6 addresses or other fancy stuff.
51 3 my $re_ip6 = qr/(?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)/;
52 3 my $re_ip = qr/(?:$re_ip4|$re_ip6)/;
53 3 my $re_domain = qr/(?:$re_fqdn|$re_ip4|\[$re_ip6\])/;
54 # Also used by the TUWF::Misc::kv_validate()
55 3 our $re_email = qr/^[-\+\.#\$=\w]+\@$re_fqdn$/;
56 3 our $re_weburl = qr/^https?:\/\/$re_domain(?::[1-9][0-9]{0,5})?(?:\/[^\s<>"]*)$/;
57  
58  
59 our %default_validations = (
60 10 ▶ regex => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 10 my $reg = shift;
62 # Error objects should be plain data structures so that they can easily
63 # be converted to JSON for debugging. We have to stringify $reg in the
64 # error object to ensure that.
65 8 ▶ +{ type => 'scalar', _analyze_regex => "$reg", func => sub { $_[0] =~ $reg ? 1 : { regex => "$reg", got => $_[0] } } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$reg/ ? : 4 4 8
66 10 },
67 14 ▶ enum => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
68 14 ▶ my @l = ref $_[0] eq 'HASH' ? sort keys %{$_[0]} : ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]);
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 10 14
ref $_[0] eq 'ARRAY' ? : 6 4 10
69 14 my %opts = map +($_,1), @l;
70 14 ▶ +{ type => 'scalar', func => sub { $opts{ (my $v = $_[0]) } ? 1 : { expected => \@l, got => $_[0] } } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$opts{my $v = $_[0]} ? : 8 6 14
71 14 },
72  
73 7 ▶ minlength => sub { _length $_[0], $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 70 ▶ maxlength => sub { _length $_[0], undef, $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 22 ▶ length => sub { _length($_[0], ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0], $_[0])) },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 5 17 22
76  
77 8 ▶ anybool => { type => 'any', default => 0, func => sub { $_[0] = $_[0] ? 1 : 0; 1 } },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 8 0 8
78 0 ▶ undefbool => { type => 'any', default => undef, func => sub { $_[0] = $_[0] ? 1 : 0; 1 } },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
79 10 ▶ jsonbool => { type => 'any', func => sub {
Subroutine __ANON__: called CC=7 SCAR=22.5
80 10 my $r = $_[0];
81 10 ▶ blessed $r && (
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($r) && ($r->isa('JSON::PP::Boolean') || $r->isa('JSON::XS::Boolean') || $r->isa('Types::Serialiser::Boolean') || $r->isa('Cpanel::JSON::XS::Boolean') || $r->isa('boolean')) ? : 4 6 10
Condition53%8/15
builtin::blessed($r) and $r->isa("JSON::PP::Boolean") || $r->isa("JSON::XS::Boolean") || $r->isa("Types::Serialiser::Boolean") || $r->isa("Cpanel::JSON::XS::Boolean") || $r->isa("boolean")
!ll&&!rl&&r
424
$r->isa("JSON::PP::Boolean") or $r->isa("JSON::XS::Boolean") or $r->isa("Types::Serialiser::Boolean") or $r->isa("Cpanel::JSON::XS::Boolean") or $r->isa("boolean")
l!l&&r!l&&!r
042
$r->isa("JSON::PP::Boolean") or $r->isa("JSON::XS::Boolean") or $r->isa("Types::Serialiser::Boolean") or $r->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
006
$r->isa("JSON::PP::Boolean") or $r->isa("JSON::XS::Boolean") or $r->isa("Types::Serialiser::Boolean")
l!l&&r!l&&!r
006
$r->isa("JSON::PP::Boolean") or $r->isa("JSON::XS::Boolean")
l!l&&r!l&&!r
006
MC/DC33%2/6
builtin::blessed($r) and $r->isa("JSON::PP::Boolean") || $r->isa("JSON::XS::Boolean") || $r->isa("Types::Serialiser::Boolean") || $r->isa("Cpanel::JSON::XS::Boolean") || $r->isa("boolean")
builtin::blessed($r) $r->isa("JSON::PP::Boolean") $r->isa("JSON::XS::Boolean") $r->isa("Types::Serialiser::Boolean") $r->isa("Cpanel::JSON::XS::Boolean") $r->isa("boolean")
Truth table42%3/7
A and B or C or D or E or F
A= builtin::blessed($r)B= $r->isa("JSON::PP::Boolean")C= $r->isa("JSON::XS::Boolean")D= $r->isa("Types::Serialiser::Boolean")E= $r->isa("Cpanel::JSON::XS::Boolean")F= $r->isa("boolean")
ABCDEFresult
0XXXXX0
1000000
11XXXX1
101XXX1
1001XX1
10001X1
1000011
82 $r->isa('JSON::PP::Boolean')
83 || $r->isa('JSON::XS::Boolean')
84 || $r->isa('Types::Serialiser::Boolean')
85 || $r->isa('Cpanel::JSON::XS::Boolean')
86 || $r->isa('boolean')
87 ) ? 1 : {};
88 } },
89  
90 # JSON number format, regex from http://stackoverflow.com/questions/13340717/json-numbers-regular-expression
91 num => { _reg $re_num },
92 int => { _reg $re_int }, # implies num
93 uint => { _reg $re_uint }, # implies num
94 23 ▶ min => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 23 my $min = shift;
96 10 ▶ +{ num => 1, _analyze_min => $min, func => sub { $_[0] >= $min ? 1 : { expected => $min, got => $_[0] } } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= $min ? : 6 4 10
97 23 },
98 22 ▶ max => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 22 my $max = shift;
100 12 ▶ +{ num => 1, _analyze_max => $max, func => sub { $_[0] <= $max ? 1 : { expected => $max, got => $_[0] } } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] <= $max ? : 8 4 12
101 22 },
102 14 ▶ range => sub { +{ min => $_[0][0], max => $_[0][1] } },
Subroutine __ANON__: called CC=1 SCAR=0.0
103  
104 3 ascii => { _reg qr/^[\x20-\x7E]*$/ },
105 ipv4 => { _reg $re_ip4 },
106 ipv6 => { _reg $re_ip6 },
107 ip => { _reg $re_ip },
108 email => { _reg($re_email), maxlength => 254 },
109 weburl => { _reg($re_weburl), maxlength => 65536 }, # the maxlength is a bit arbitrary, but better than unlimited
110 );
111  
112  
113 # Loads a hashref of validations and a schema definition, and converts it into
114 # an object with:
115 # {
116 # name => $name_or_undef,
117 # validations => [ $recursive_compiled_object, .. ],
118 # schema => $modified_schema_without_validations,
119 # known_keys => { $key => 1, .. } # Extracted from 'keys', Used for the 'unknown' validation
120 # }
121 1082 ▶sub _compile {
Subroutine _compile: called CC=20 SCAR=30.0
122 1082 my($validations, $schema, $rec) = @_;
123  
124 1082 my(%top, @val);
125 1082 ▶ my @keys = keys %{$schema->{keys}} if $schema->{keys};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"keys"} 55 1027 1082
126  
127 1082 for(sort keys %$schema) {
128 2020 ▶ if($builtin{$_} || /^_analyze_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($builtin{$_} or /^_analyze_/) 1437 583 2020
Condition100%3/3
$builtin{$_} or /^_analyze_/
l!l&&r!l&&!r
1016421583
MC/DC100%2/2
$builtin{$_} or /^_analyze_/
$builtin{$_} /^_analyze_/
Truth table100%3/3
A or B
A= $builtin{$_}B= /^_analyze_/
ABresult
1X1
011
000
129 1437 $top{$_} = $schema->{$_};
130 1437 next;
131 }
132  
133 583 ▶ my $t = $validations->{$_} || $default_validations{$_};
Condition100%3/3
$validations->{$_} || $TUWF::Validate::default_validations{$_}
l!l&&r!l&&!r
1973842
MC/DC100%2/2
$validations->{$_} || $TUWF::Validate::default_validations{$_}
$validations->{$_} $TUWF::Validate::default_validations{$_}
Truth table100%3/3
A || B
A= $validations->{$_}B= $TUWF::Validate::default_validations{$_}
ABresult
1X1
011
000
134 583 ▶ croak "Unknown validation: $_" if !$t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 2 581 583
135 581 ▶ croak "Recursion limit exceeded while resolving validation '$_'" if $rec < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rec < 1 2 579 581
136 579 ▶ $t = ref $t eq 'HASH' ? $t : $t->($schema->{$_});
Branch: 1 branch
BranchTrueFalseTotal
ref $t eq 'HASH' ? : 393 186 579
137  
138 579 my $v = _compile($validations, $t, $rec-1);
139 451 $v->{name} = $_;
140 451 push @val, $v;
141 }
142  
143 # Inherit some builtin options from validations
144 950 for my $t (@val) {
145 451 ▶ if($top{type} && $t->{schema}{type} && $top{type} ne $t->{schema}{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top{"type"} and $t->{"schema"}{"type"} and $top{"type"} ne $t->{"schema"}{"type"}) 2 449 451
Condition100%6/6
$top{"type"} and $t->{"schema"}{"type"} and $top{"type"} ne $t->{"schema"}{"type"}
!ll&&!rl&&r
431182
$top{"type"} and $t->{"schema"}{"type"}
!ll&&!rl&&r
3448720
MC/DC100%3/3
$top{"type"} and $t->{"schema"}{"type"} and $top{"type"} ne $t->{"schema"}{"type"}
$top{"type"} $t->{"schema"}{"type"} $top{"type"} ne $t->{"schema"}{"type"}
Truth table100%4/4
A and B and C
A= $top{"type"}B= $t->{"schema"}{"type"}C= $top{"type"} ne $t->{"schema"}{"type"}
ABCresult
0XX0
10X0
1100
1111
146 2 ▶ croak "Incompatible types, the schema specifies '$top{type}' but validation '$t->{name}' requires '$t->{schema}{type}'" if $schema->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"type"} 2 0 2
147 0 croak "Incompatible types, '$t->[0]' requires '$t->{schema}{type}', but another validation requires '$top{type}'";
148 }
149 exists $t->{schema}{$_} and !exists $top{$_} and $top{$_} = delete $t->{schema}{$_}
150 449 ▶ for qw/default onerror rmwhitespace type scalar unknown missing sort unique/;
Branch: 1 branch
BranchTrueFalseTotal
exists $t->{"schema"}{$_} and not exists $top{$_} and $top{$_} = delete $t->{"schema"}{$_} 358 3683 4041
Condition100%3/3
exists $t->{"schema"}{$_} and not exists $top{$_}
!ll&&!rl&&r
365924358
MC/DC100%2/2
exists $t->{"schema"}{$_} and not exists $top{$_}
exists $t->{"schema"}{$_} not exists $top{$_}
Truth table100%3/3
A and not(B)
A= exists $t->{"schema"}{$_}B= not exists $top{$_}
ABresult
0X0
100
111
151  
152 449 push @keys, keys %{ delete $t->{known_keys} };
153 449 ▶ push @keys, keys %{ $t->{schema}{keys} } if $t->{schema}{keys};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"schema"}{"keys"} 18 431 449
154 }
155  
156 # Compile sub-schemas
157 948 ▶ $top{keys} = { map +($_, compile($validations, $top{keys}{$_})), keys %{$top{keys}} } if $top{keys};
Branch: 1 branch
BranchTrueFalseTotal
if $top{"keys"} 55 893 948
158 947 ▶ $top{values} = compile($validations, $top{values}) if $top{values};
Branch: 1 branch
BranchTrueFalseTotal
if $top{"values"} 12 935 947
159  
160 # XXX: Flattening recursive validations would be faster and may simplify
161 # the code a bit, but makes error objects harder to interpret.
162  
163 # XXX: As an optimization, it's possible to remove double validations (e.g.
164 # multiple invocations of the same validation with the same options due to
165 # validations calling each other). Care must be taken that this won't
166 # affect error objects (i.e. only subsequent invocations should be
167 # removed).
168  
169 return {
170 947 validations => \@val,
171 schema => \%top,
172 known_keys => { map +($_,1), @keys },
173 };
174 }
175  
176  
177 503 ▶sub compile {
Subroutine compile: called CC=23 SCAR=31.4
Pod undocumented
178 503 my($validations, $schema) = @_;
179  
180 503 ▶ return $schema if ref $schema eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $schema eq __PACKAGE__ 0 503 503
181  
182 503 my $c = _compile $validations, $schema, 64;
183  
184 496 ▶ $c->{schema}{type} //= 'scalar';
Condition100%2/2
$c->{"schema"}{"type"} //= "scalar"
l!l
332164
MC/DC100%1/1
$c->{"schema"}{"type"} //= "scalar"
$c->{"schema"}{"type"}
Truth table100%2/2
A
A= $c->{"schema"}{"type"}
Aresult
11
00
185 496 ▶ croak "Invalid value for 'type': $c->{schema}{type}" if !$type_vals{$c->{schema}{type}};
Branch: 1 branch
BranchTrueFalseTotal
unless $type_vals{$c->{"schema"}{"type"}} 1 495 496
186  
187 495 ▶ $c->{schema}{rmwhitespace} //= 1 if $c->{schema}{type} eq 'scalar';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"type"} eq "scalar" 337 158 495
Condition100%2/2
$c->{"schema"}{"rmwhitespace"} //= 1
l!l
10327
MC/DC100%1/1
$c->{"schema"}{"rmwhitespace"} //= 1
$c->{"schema"}{"rmwhitespace"}
Truth table100%2/2
A
A= $c->{"schema"}{"rmwhitespace"}
Aresult
11
00
188  
189 495 ▶ $c->{schema}{unknown} //= 'remove';
Condition100%2/2
$c->{"schema"}{"unknown"} //= "remove"
l!l
29466
MC/DC100%1/1
$c->{"schema"}{"unknown"} //= "remove"
$c->{"schema"}{"unknown"}
Truth table100%2/2
A
A= $c->{"schema"}{"unknown"}
Aresult
11
00
190 495 ▶ $c->{schema}{missing} //= 'create';
Condition100%2/2
$c->{"schema"}{"missing"} //= "create"
l!l
46449
MC/DC100%1/1
$c->{"schema"}{"missing"} //= "create"
$c->{"schema"}{"missing"}
Truth table100%2/2
A
A= $c->{"schema"}{"missing"}
Aresult
11
00
191 495 ▶ croak "Invalid value for 'unknown': $c->{schema}{unknown}" if !$unknown_vals{$c->{schema}{unknown}};
Branch: 1 branch
BranchTrueFalseTotal
unless $unknown_vals{$c->{"schema"}{"unknown"}} 0 495 495
192 495 ▶ croak "Invalid value for 'missing': $c->{schema}{missing}" if !$missing_vals{$c->{schema}{missing}};
Branch: 1 branch
BranchTrueFalseTotal
unless $missing_vals{$c->{"schema"}{"missing"}} 0 495 495
193  
194 495 ▶ delete $c->{schema}{default} if ref $c->{schema}{default} eq 'SCALAR' && ${$c->{schema}{default}} eq 'required';
Branch: 1 branch
BranchTrueFalseTotal
if ref $c->{"schema"}{"default"} eq "SCALAR" and ${$c->{"schema"}{"default"};} eq "required" 2 493 495
Condition66%2/3
ref $c->{"schema"}{"default"} eq "SCALAR" and ${$c->{"schema"}{"default"};} eq "required"
!ll&&!rl&&r
49302
MC/DC50%1/2
ref $c->{"schema"}{"default"} eq "SCALAR" and ${$c->{"schema"}{"default"};} eq "required"
ref $c->{"schema"}{"default"} eq "SCALAR" ${$c->{"schema"}{"default"};} eq "required"
Truth table66%2/3
A and B
A= ref $c->{"schema"}{"default"} eq "SCALAR"B= ${$c->{"schema"}{"default"};} eq "required"
ABresult
0X0
100
111
195 495 ▶ delete $c->{schema}{keys} if $c->{schema}{type} ne 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"type"} ne "hash" 429 66 495
196 495 ▶ delete $c->{schema}{values} if $c->{schema}{type} ne 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"type"} ne "array" 445 50 495
197 495 ▶ delete $c->{schema}{sort} if $c->{schema}{type} ne 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"type"} ne "array" 445 50 495
198 495 ▶ delete $c->{schema}{unique} if $c->{schema}{type} ne 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"type"} ne "array" 445 50 495
199  
200 495 ▶ if(exists $c->{schema}{sort}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->{"schema"}{"sort"}) 10 485 495
201 10 my $s = $c->{schema}{sort};
202 $c->{schema}{sort} =
203 ref $s eq 'CODE' ? $s
204 6 ▶ : $s eq 'str' ? sub { $_[0] cmp $_[1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
205 22 ▶ : $s eq 'num' ? sub { $_[0] <=> $_[1] }
Subroutine __ANON__: called CC=1 SCAR=0.0
206 10 ▶ : croak "Unknown value for 'sort': $c->{schema}{sort}";
Branch: 3 branches
BranchTrueFalseTotal
ref $s eq 'CODE' ? : 2 8 10
$s eq 'str' ? : 2 6 8
$s eq 'num' ? : 6 0 6
207 }
208 495 ▶ $c->{schema}{unique} = sub { $_[0] } if $c->{schema}{unique} && !ref $c->{schema}{unique} && !$c->{schema}{sort};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"unique"} and not ref $c->{"schema"}{"unique"} and not $c->{"schema"}{"sort"} 4 491 495
Condition100%6/6
$c->{"schema"}{"unique"} and not ref $c->{"schema"}{"unique"} and not $c->{"schema"}{"sort"}
!ll&&!rl&&r
48744
$c->{"schema"}{"unique"} and not ref $c->{"schema"}{"unique"}
!ll&&!rl&&r
48348
MC/DC100%3/3
$c->{"schema"}{"unique"} and not ref $c->{"schema"}{"unique"} and not $c->{"schema"}{"sort"}
$c->{"schema"}{"unique"} not ref $c->{"schema"}{"unique"} not $c->{"schema"}{"sort"}
Truth table100%4/4
A and not(B) and not(C)
A= $c->{"schema"}{"unique"}B= not ref $c->{"schema"}{"unique"}C= not $c->{"schema"}{"sort"}
ABCresult
0XX0
10X0
1100
1111
209  
210 495 bless $c, __PACKAGE__;
211 }
212  
213  
214 668 ▶sub _validate_rec {
Subroutine _validate_rec: called CC=23 SCAR=31.4
215 668 my($c, $input) = @_;
216  
217 # hash keys
218 668 ▶ if($c->{schema}{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"schema"}{"keys"}) 36 632 668
219 36 my @err;
220 36 for my $k (keys %{$c->{schema}{keys}}) {
221 58 my $s = $c->{schema}{keys}{$k};
222 58 ▶ if(!exists $input->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $input->{$k}) 26 32 58
223 26 ▶ next if $s->{schema}{missing} eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"schema"}{"missing"} eq "ignore" 14 12 26
224 12 ▶ return [$input, { validation => 'missing', key => $k }] if $s->{schema}{missing} eq 'reject';
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"schema"}{"missing"} eq "reject" 2 10 12
225 10 ▶ $input->{$k} = ref $s->{schema}{default} eq 'CODE' ? $s->{schema}{default}->() : $s->{schema}{default} // undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->{'schema'}{'default'} eq 'CODE' ? : 0 10 10
Condition50%1/2
$s->{"schema"}{"default"} // undef
l!l
010
MC/DC0%0/1
$s->{"schema"}{"default"} // undef
$s->{"schema"}{"default"}
Truth table50%1/2
A
A= $s->{"schema"}{"default"}
Aresult
11
00
226 10 ▶ next if exists $s->{schema}{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"schema"}{"default"} 4 6 10
227 }
228  
229 38 my $r = _validate($s, $input->{$k});
230 38 $input->{$k} = $r->[0];
231 38 ▶ if($r->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->[1]) 8 30 38
232 8 $r->[1]{key} = $k;
233 8 push @err, $r->[1];
234 }
235 }
236 34 ▶ return [$input, { validation => 'keys', errors => \@err }] if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 8 26 34
237 }
238  
239 # array values
240 658 ▶ if($c->{schema}{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"schema"}{"values"}) 10 648 658
241 10 my @err;
242 10 for my $i (0..$#$input) {
243 18 my $r = _validate($c->{schema}{values}, $input->[$i]);
244 18 $input->[$i] = $r->[0];
245 18 ▶ if($r->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->[1]) 2 16 18
246 2 $r->[1]{index} = $i;
247 2 push @err, $r->[1];
248 }
249 }
250 10 ▶ return [$input, { validation => 'values', errors => \@err }] if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 2 8 10
251 }
252  
253 # validations
254 656 for (@{$c->{validations}}) {
255 342 my $r = _validate_rec($_, $input);
256 342 $input = $r->[0];
257  
258 return [$input, {
259 # If the error was a custom 'func' object, then make that the primary cause.
260 # This makes it possible for validations to provide their own error objects.
261 2 $r->[1]{validation} eq 'func' && (!exists $r->[1]{result} || keys %{$r->[1]} > 2) ? %{$r->[1]} : (error => $r->[1]),
262 validation => $_->{name},
263 342 ▶ }] if $r->[1];
Branch: 2 branches
BranchTrueFalseTotal
if $r->[1] 124 218 342
$r->[1]{'validation'} eq 'func' && (!exists $r->[1]{'result'} || keys %{$$r[1];} > 2) ? : 104 20 124
Condition83%5/6
$r->[1]{"validation"} eq "func" and !exists $r->[1]{"result"} || keys %{$$r[1];} > 2
!ll&&!rl&&r
182104
not exists $r->[1]{"result"} or keys %{$$r[1];} > 2
l!l&&r!l&&!r
10402
MC/DC66%2/3
$r->[1]{"validation"} eq "func" and !exists $r->[1]{"result"} || keys %{$$r[1];} > 2
$r->[1]{"validation"} eq "func" not exists $r->[1]{"result"} keys %{$$r[1];} > 2
Truth table75%3/4
A and not(B) or C
A= $r->[1]{"validation"} eq "func"B= not exists $r->[1]{"result"}C= keys %{$$r[1];} > 2
ABCresult
0XX0
1000
11X1
1011
264 }
265  
266 # func
267 532 ▶ if($c->{schema}{func}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"schema"}{"func"}) 304 228 532
268 304 my $r = $c->{schema}{func}->($input);
269 304 ▶ return [$input, { %$r, validation => 'func' }] if ref $r eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $r eq "HASH" 104 200 304
270 200 ▶ return [$input, { validation => 'func', result => $r }] if !$r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 2 198 200
271 }
272  
273 426 return [$input]
274 }
275  
276  
277 208 ▶sub _validate_array {
Subroutine _validate_array: called CC=10 SCAR=23.0
278 208 my($c, $input) = @_;
279  
280 208 ▶ return [$input] if $c->{schema}{type} ne 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"type"} ne "array" 164 44 208
281  
282 44 ▶ $input = [sort { $c->{schema}{sort}->($a,$b) } @$input ] if $c->{schema}{sort};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"sort"} 10 34 44
283  
284 # Key-based uniqueness
285 44 ▶ if($c->{schema}{unique} && ref $c->{schema}{unique} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->{'schema'}{'unique'} and ref $c->{'schema'}{'unique'} eq 'CODE') { } 8 36 44
elsif ($c->{'schema'}{'unique'}) { } 4 32 36
Condition100%3/3
$c->{"schema"}{"unique"} and ref $c->{"schema"}{"unique"} eq "CODE"
!ll&&!rl&&r
3248
MC/DC100%2/2
$c->{"schema"}{"unique"} and ref $c->{"schema"}{"unique"} eq "CODE"
$c->{"schema"}{"unique"} ref $c->{"schema"}{"unique"} eq "CODE"
Truth table100%3/3
A and B
A= $c->{"schema"}{"unique"}B= ref $c->{"schema"}{"unique"} eq "CODE"
ABresult
0X0
100
111
286 8 my %h;
287 8 for my $i (0..$#$input) {
288 24 my $k = $c->{schema}{unique}->($input->[$i]);
289 24 ▶ return [$input, { validation => 'unique', index_a => $h{$k}, value_a => $input->[$h{$k}], index_b => $i, value_b => $input->[$i], key => $k }] if exists $h{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h{$k} 4 20 24
290 20 $h{$k} = $i;
291 }
292  
293 # Comparison-based uniqueness
294 } elsif($c->{schema}{unique}) {
295 4 for my $i (0..$#$input-1) {
296 return [$input, { validation => 'unique', index_a => $i, value_a => $input->[$i], index_b => $i+1, value_b => $input->[$i+1] }]
297 8 ▶ if $c->{schema}{sort}->($input->[$i], $input->[$i+1]) == 0
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"sort"}->($input->[$i], $$input[$i + 1]) == 0 2 6 8
298 }
299 }
300  
301 38 return [$input]
302 }
303  
304  
305 380 ▶sub _validate_input {
Subroutine _validate_input: called CC=26 SCAR=32.6
306 380 my($c, $input) = @_;
307  
308 # rmwhitespace (needs to be done before the 'default' test)
309 380 ▶ if(defined $input && !ref $input && $c->{schema}{type} eq 'scalar' && $c->{schema}{rmwhitespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and not ref $input and $c->{"schema"}{"type"} eq "scalar" and $c->{"schema"}{"rmwhitespace"}) 218 162 380
Condition100%9/9
defined $input and not ref $input and $c->{"schema"}{"type"} eq "scalar" and $c->{"schema"}{"rmwhitespace"}
!ll&&!rl&&r
1548218
defined $input and not ref $input and $c->{"schema"}{"type"} eq "scalar"
!ll&&!rl&&r
14014226
defined $input and not ref $input
!ll&&!rl&&r
28112240
MC/DC100%4/4
defined $input and not ref $input and $c->{"schema"}{"type"} eq "scalar" and $c->{"schema"}{"rmwhitespace"}
defined $input not ref $input $c->{"schema"}{"type"} eq "scalar" $c->{"schema"}{"rmwhitespace"}
Truth table100%5/5
A and not(B) and C and D
A= defined $inputB= not ref $inputC= $c->{"schema"}{"type"} eq "scalar"D= $c->{"schema"}{"rmwhitespace"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
310 218 $input =~ s/\r//g;
311 218 $input =~ s/^\s*//;
312 218 $input =~ s/\s*$//;
313 }
314  
315 # default
316 380 ▶ if(!defined $input || (!ref $input && $input eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $input or not ref $input and $input eq "") 42 338 380
Condition100%6/6
not defined $input or not ref $input and $input eq ""
l!l&&r!l&&!r
2814338
not ref $input and $input eq ""
!ll&&!rl&&r
11222614
MC/DC100%3/3
not defined $input or not ref $input and $input eq ""
not defined $input not ref $input $input eq ""
Truth table100%4/4
not(A) or not(B) and C
A= not defined $inputB= not ref $inputC= $input eq ""
ABCresult
1XX1
0111
00X0
0100
317 42 ▶ return [ref $c->{schema}{default} eq 'CODE' ? $c->{schema}{default}->($input) : $c->{schema}{default}] if exists $c->{schema}{default};
Branch: 2 branches
BranchTrueFalseTotal
if exists $c->{"schema"}{"default"} 22 20 42
ref $c->{'schema'}{'default'} eq 'CODE' ? : 6 16 22
318 20 return [$input, { validation => 'required' }];
319 }
320  
321 338 ▶ if($c->{schema}{type} eq 'scalar') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->{'schema'}{'type'} eq 'scalar') { } 216 122 338
elsif ($c->{'schema'}{'type'} eq 'hash') { } 50 72 122
elsif ($c->{'schema'}{'type'} eq 'array') { } 50 22 72
elsif ($c->{'schema'}{'type'} eq 'any') { } 22 0 22
322 216 ▶ return [$input, { validation => 'type', expected => 'scalar', got => lc ref $input }] if ref $input;
Branch: 1 branch
BranchTrueFalseTotal
if ref $input 2 214 216
323  
324 } elsif($c->{schema}{type} eq 'hash') {
325 50 ▶ return [$input, { validation => 'type', expected => 'hash', got => lc ref $input || 'scalar' }] if ref $input ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $input ne "HASH" 6 44 50
Condition100%2/2
lc ref $input || 'scalar'
l!l
24
MC/DC100%1/1
lc ref $input || 'scalar'
lc ref $input
Truth table100%2/2
A
A= lc ref $input
Aresult
11
00
326  
327 # unknown
328 # Each branch below makes a shallow copy of the hash, so that further
329 # validations can perform in-place modifications without affecting the
330 # input.
331 44 ▶ if($c->{schema}{unknown} eq 'remove') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->{'schema'}{'unknown'} eq 'remove') { } 24 20 44
elsif ($c->{'schema'}{'unknown'} eq 'reject') { } 4 16 20
332 24 $input = { map +($_, $input->{$_}), grep $c->{known_keys}{$_}, keys %$input };
333 } elsif($c->{schema}{unknown} eq 'reject') {
334 4 my @err = grep !$c->{known_keys}{$_}, keys %$input;
335 4 ▶ return [$input, { validation => 'unknown', keys => \@err, expected => [ sort keys %{$c->{known_keys}} ] }] if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 2 2 4
336 2 $input = { %$input };
337 } else {
338 16 $input = { %$input };
339 }
340  
341 } elsif($c->{schema}{type} eq 'array') {
342 50 ▶ $input = [$input] if $c->{schema}{scalar} && !ref $input;
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"schema"}{"scalar"} and not ref $input 2 48 50
Condition66%2/3
$c->{"schema"}{"scalar"} and not ref $input
!ll&&!rl&&r
4802
MC/DC50%1/2
$c->{"schema"}{"scalar"} and not ref $input
$c->{"schema"}{"scalar"} not ref $input
Truth table66%2/3
A and not(B)
A= $c->{"schema"}{"scalar"}B= not ref $input
ABresult
0X0
100
111
343 50 ▶ return [$input, { validation => 'type', expected => $c->{schema}{scalar} ? 'array or scalar' : 'array', got => lc ref $input || 'scalar' }] if ref $input ne 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $input ne "ARRAY" 2 48 50
$c->{'schema'}{'scalar'} ? : 0 2 2
Condition50%1/2
lc ref $input || 'scalar'
l!l
02
MC/DC0%0/1
lc ref $input || 'scalar'
lc ref $input
Truth table50%1/2
A
A= lc ref $input
Aresult
11
00
344 48 $input = [@$input]; # Create a shallow copy to prevent in-place modification.
345  
346 } elsif($c->{schema}{type} eq 'any') {
347 # No need to do anything here.
348  
349 } else {
350 0 croak "Unknown type '$c->{schema}{type}'"; # Should be checked in _compile(), preferably.
351 }
352  
353 326 my $r = _validate_rec($c, $input);
354 326 ▶ return $r if $r->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $r->[1] 118 208 326
355 208 $input = $r->[0];
356  
357 208 _validate_array($c, $input);
358 }
359  
360  
361 380 ▶sub _validate {
Subroutine _validate: called CC=4 SCAR=13.9
362 380 my($c, $input) = @_;
363 380 my $r = _validate_input($c, $input);
364 380 ▶ $r->[1] && exists $c->{schema}{onerror} ? [ref $c->{schema}{onerror} eq 'CODE' ? $c->{schema}{onerror}->(bless $r, 'TUWF::Validate::Result') : $c->{schema}{onerror}] : $r
Branch: 2 branches
BranchTrueFalseTotal
$r->[1] && exists $c->{'schema'}{'onerror'} ? : 8 372 380
ref $c->{'schema'}{'onerror'} eq 'CODE' ? : 2 6 8
Condition100%3/3
$r->[1] and exists $c->{"schema"}{"onerror"}
!ll&&!rl&&r
2241488
MC/DC100%2/2
$r->[1] and exists $c->{"schema"}{"onerror"}
$r->[1] exists $c->{"schema"}{"onerror"}
Truth table100%3/3
A and B
A= $r->[1]B= exists $c->{"schema"}{"onerror"}
ABresult
0X0
100
111
365 }
366  
367  
368 325 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod undocumented
369 325 ▶ my($c, $input) = ref $_[0] eq __PACKAGE__ ? @_ : (compile($_[0], $_[1]), $_[2]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 162 163 325
370 324 bless _validate($c, $input), 'TUWF::Validate::Result';
371 }
372  
373  
374 65 ▶sub analyze {
Subroutine analyze: called CC=1 SCAR=0.0
Pod undocumented
375 65 require TUWF::Validate::Interop;
376 65 TUWF::Validate::Interop::analyze($_[0]);
377 }
378  
379  
380  
381 package TUWF::Validate::Result;
382  
383 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
384 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
385 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
386  
387 # A result object contains: [$data, $error]
388  
389 # In boolean context, returns whether the validation succeeded.
390 3 ▶use overload bool => sub { !$_[0][1] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
391  
392 # Returns the validation errors, or undef if validation succeeded
393 324 ▶sub err { $_[0][1] }
Subroutine err: called CC=1 SCAR=0.0
394  
395 # Returns the validated and normalized input, dies if validation didn't succeed.
396 162 ▶sub data {
Subroutine data: called CC=2 SCAR=6.9
397 162 ▶ if($_[0][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][1]) 69 93 162
398 69 require Data::Dumper;
399 69 my $s = Data::Dumper->new([$_[0][1]])->Terse(1)->Pair(':')->Indent(0)->Sortkeys(1)->Dump;
400 69 croak "Validation failed: $s";
401 }
402 93 $_[0][0]
403 }
404  
405 # Same as 'data', but returns partially validated and normalized data if validation failed.
406 324 ▶sub unsafe_data { $_[0][0] }
Subroutine unsafe_data: called CC=1 SCAR=0.0
407  
408 # TODO: Human-readable error message formatting
409  
410 3 1;
« TUWF/XML.pm ↑ Summary