blib/lib/Validate/Tiny.pm

filter: Statementstmt 98.9% 181 / 183 Branchbran 94.4% 119 / 126 Conditioncond 82.3% 56 / 68 MC/DCmcdc 69.0% 29 / 42 Subroutinesub 97.4% 38 / 39 Podpod 100% 14 / 14 totaltotal 92.5% 437 / 472 CC 97 SCAR 17.0
CC
97
Cov
94%
validate
15.0
AUTOLOAD
9.1
_run_code
7.0
CRAP 5.5
↑ Summary
1 package Validate::Tiny;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use List::MoreUtils 'natatime';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 our @ISA = qw/Exporter/;
11 22 our @EXPORT_OK = qw/
12 validate
13 filter
14 is_required
15 is_required_if
16 is_existing
17 is_equal
18 is_long_between
19 is_long_at_least
20 is_long_at_most
21 is_a
22 is_like
23 is_in
24 /;
25  
26 22 our %EXPORT_TAGS = (
27 'all' => \@EXPORT_OK
28 );
29  
30 22 our $VERSION = '1.6';
31  
32 our %FILTERS = (
33 16 ▶ trim => sub { return unless defined $_[0]; $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 2 14 16
34 10 ▶ strip => sub { return unless defined $_[0]; $_[0] =~ s/(\s){2,}/$1/g; $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 9 10
35 4 ▶ lc => sub { return unless defined $_[0]; lc $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 3 4
36 14 ▶ uc => sub { return unless defined $_[0]; uc $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 14 14
37 1 ▶ ucfirst => sub { return unless defined $_[0]; ucfirst $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 1 1
38 22 );
39  
40 133 ▶sub validate {
Subroutine validate: called CC=15 SCAR=27.1
Pod documented
41 133 my ( $input, $rules ) = @_;
42 133 my $error = {};
43  
44 # Sanity check
45 #
46 133 ▶ die 'You must define a fields array' unless defined $rules->{fields};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules->{"fields"} 1 132 133
47  
48 132 for (qw/filters checks/) {
49 262 ▶ next unless exists $rules->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$_} 131 131 262
50 131 ▶ if ( ref( $rules->{$_} ) ne 'ARRAY' || @{ $rules->{$_} } % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2) 4 127 131
Condition100%3/3
ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2
l!l&&r!l&&!r
22127
MC/DC100%2/2
ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2
ref $rules->{$_} ne "ARRAY" @{$rules->{$_};} % 2
Truth table100%3/3
A or B
A= ref $rules->{$_} ne "ARRAY"B= @{$rules->{$_};} % 2
ABresult
1X1
011
000
51 4 die "$_ must be an array with an even number of elements";
52 }
53 }
54  
55 128 for ( keys %$rules ) {
56 255 ▶ /^(fields|filters|checks)$/ or die "Unknown key $_";
Branch: 1 branch
BranchTrueFalseTotal
/^(fields|filters|checks)$/ or die "Unknown key $_" 254 1 255
57 }
58  
59 127 my $param = {};
60 127 ▶ my @fields = @{ $rules->{fields} } ? @{ $rules->{fields} } : keys(%$input);
Branch: 1 branch
BranchTrueFalseTotal
@{$rules->{'fields'};} ? : 124 2 126
61  
62 # Add existing, filtered input to $param
63 #
64 126 for my $key ( @fields ) {
65 224 ▶ exists $input->{$key} and ($param->{$key} = _process( $rules->{filters}, $input, $key ));
Branch: 1 branch
BranchTrueFalseTotal
exists $input->{$key} and $param->{$key} = _process($rules->{"filters"}, $input, $key) 156 68 224
66 }
67  
68 # Process all checks for $param
69 #
70 125 for my $key ( @fields ) {
71 223 my $err = _process( $rules->{checks}, $param, $key, 1 );
72 222 ▶ $error->{$key} ||= $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 49 173 222
Condition33%1/3
$error->{$key} ||= $err
l!l&&r!l&&!r
0490
MC/DC0%0/2
$error->{$key} ||= $err
$error->{$key} $err
Truth table33%1/3
A ||= B
A= $error->{$key}B= $err
ABresult
1X1
011
000
73 }
74  
75 return {
76 124 ▶ success => keys %$error ? 0 : 1,
Branch: 1 branch
BranchTrueFalseTotal
keys %$error ? : 45 79 124
77 error => $error,
78 data => $param
79 };
80  
81 }
82  
83 185 ▶sub _run_code {
Subroutine _run_code: called CC=7 SCAR=19.5
84 185 my ( $code, $value, $param, $key ) = @_;
85 185 my $result = $value;
86 185 my $ref = ref $code;
87 185 ▶ if ( $ref eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 157 28 185
elsif ($ref eq 'ARRAY') { } 26 2 28
88 157 $result = $code->( $value, $param, $key );
89 157 ▶ $value = $result unless defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 56 101 157
90 }
91 elsif ( $ref eq 'ARRAY' ) {
92 26 for (@$code) {
93 39 $result = _run_code( $_, $value, $param, $key );
94 39 ▶ if ( defined $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param) { } 19 20 39
95 19 ▶ last if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 9 10 19
96 }
97 else {
98 20 $value = $result;
99 }
100 }
101 }
102 else {
103 2 die 'Filters and checks must be either sub{} or []';
104 }
105  
106 183 return $result;
107 }
108  
109 379 ▶sub _process {
Subroutine _process: called CC=7 SCAR=19.5
110 379 my ( $pairs, $param, $key, $check ) = @_;
111 379 my $value = $param->{$key};
112 379 my $iterator = natatime(2, @$pairs);
113 379 ▶ while ( my ( $match, $code ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 249 328 577
114 249 ▶ if ( _match($key, $match) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_match($key, $match)) 146 103 249
115 146 ▶ my $temp = _run_code( $code, $value, $check ? ($param, $key) : undef );
Branch: 1 branch
BranchTrueFalseTotal
$check ? : 98 48 146
116 144 ▶ if ( $check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) { } 97 47 144
117 97 ▶ return $temp if $temp
Branch: 1 branch
BranchTrueFalseTotal
if $temp 49 48 97
118 }
119 else {
120 47 $value = $temp;
121 }
122 }
123 }
124 328 ▶ return if $check;
Branch: 1 branch
BranchTrueFalseTotal
if $check 173 155 328
125 155 return $value;
126 }
127  
128 266 ▶sub _match {
Subroutine _match: called CC=4 SCAR=13.9
129 266 my ( $a, $b ) = @_;
130 266 ▶ if ( !ref($b) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $b) { } 231 35 266
elsif (ref $b eq 'ARRAY') { } 22 13 35
elsif (ref $b eq 'Regexp') { } 12 1 13
131 231 return $a eq $b;
132 }
133 elsif ( ref($b) eq 'ARRAY' ) {
134 22 return grep { $a eq $_ } @$b;
135 }
136 elsif ( ref($b) eq 'Regexp' ) {
137 12 return $a =~ $b;
138 }
139 else {
140 1 return 0;
141 }
142 }
143  
144 22 ▶sub filter {
Subroutine filter: called CC=4 SCAR=13.9
Pod documented
145 22 my @result = ();
146 22 for (@_) {
147 26 ▶ if ( exists $FILTERS{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Validate::Tiny::FILTERS{$_}) { } 25 1 26
148 25 push @result, $FILTERS{$_};
149 }
150 else {
151 1 die "Invalid filter: $_";
152 }
153 }
154 21 ▶ return @result == 1 ? $result[0] : \@result;
Branch: 1 branch
BranchTrueFalseTotal
@result == 1 ? : 17 4 21
155 }
156  
157 10 ▶sub is_required {
Subroutine is_required: called CC=2 SCAR=6.9
Pod documented
158 10 ▶ my $err_msg = shift || 'Required';
Condition100%2/2
shift() || 'Required'
l!l
19
MC/DC100%1/1
shift() || 'Required'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
159 37 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
160 37 ▶ return if defined $_[0] && $_[0] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] ne "" 17 20 37
Condition100%3/3
defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
61417
MC/DC100%2/2
defined $_[0] and $_[0] ne ""
defined $_[0] $_[0] ne ""
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] ne ""
ABresult
0X0
100
111
161 20 return $err_msg;
162 10 };
163 }
164  
165 3 ▶sub is_required_if {
Subroutine is_required_if: called CC=5 SCAR=16.3
Pod documented
166 3 my ( $condition, $err_msg ) = @_;
167 3 ▶ $condition = 0 unless defined $condition;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $condition 0 3 3
168 3 ▶ $err_msg ||= 'Required';
Condition100%2/2
$err_msg ||= "Required"
l!l
12
MC/DC100%1/1
$err_msg ||= "Required"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
169 3 ▶ if ( ref($condition) && ref($condition) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $condition and ref $condition ne "CODE") 0 3 3
Condition66%2/3
ref $condition and ref $condition ne "CODE"
!ll&&!rl&&r
210
MC/DC0%0/2
ref $condition and ref $condition ne "CODE"
ref $condition ref $condition ne "CODE"
Truth table66%2/3
A and B
A= ref $conditionB= ref $condition ne "CODE"
ABresult
0X0
100
111
170 0 croak "is_required_if condition must be CODE or SCALAR";
171 }
172 7 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
173 7 my ( $value, $params ) = @_;
174 7 ▶ my $required =
Branch: 1 branch
BranchTrueFalseTotal
ref $condition eq 'CODE' ? : 3 4 7
175 ref($condition) eq 'CODE'
176 ? $condition->($params)
177 : $condition;
178 7 ▶ return unless $required;
Branch: 1 branch
BranchTrueFalseTotal
unless $required 3 4 7
179 4 ▶ return if defined $value && $value ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value ne "" 2 2 4
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
022
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
180 2 return $err_msg;
181 3 };
182 }
183  
184 2 ▶sub is_existing {
Subroutine is_existing: called CC=2 SCAR=6.9
Pod documented
185 2 ▶ my $err_msg = shift || 'Must be defined';
Condition100%2/2
shift() || 'Must be defined'
l!l
11
MC/DC100%1/1
shift() || 'Must be defined'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
186 6 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
187 6 ▶ return if exists $_[1]->{$_[2]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[1]{$_[2]} 4 2 6
188 2 return $err_msg;
189 }
190 2 }
191  
192 3 ▶sub is_equal {
Subroutine is_equal: called CC=2 SCAR=7.0
Pod documented
193 3 my ( $other, $err_msg ) = @_;
194 3 ▶ $err_msg ||= 'Invalid value';
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
12
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
195 4 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
196 4 ▶ return if !defined($_[0]) || $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 3 4
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
013
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
197 3 ▶ return if defined $_[1]->{$other} && $_[0] eq $_[1]->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1]{$other} and $_[0] eq $_[1]{$other} 1 2 3
Condition66%2/3
defined $_[1]{$other} and $_[0] eq $_[1]{$other}
!ll&&!rl&&r
021
MC/DC50%1/2
defined $_[1]{$other} and $_[0] eq $_[1]{$other}
defined $_[1]{$other} $_[0] eq $_[1]{$other}
Truth table66%2/3
A and B
A= defined $_[1]{$other}B= $_[0] eq $_[1]{$other}
ABresult
0X0
100
111
198 2 return $err_msg;
199 3 };
200 }
201  
202 2 ▶sub is_long_between {
Subroutine is_long_between: called CC=2 SCAR=6.9
Pod documented
203 2 my ( $min, $max, $err_msg ) = @_;
204 2 ▶ $err_msg ||= "Must be between $min and $max symbols";
Condition100%2/2
$err_msg ||= "Must be between $min and $max symbols"
l!l
11
MC/DC100%1/1
$err_msg ||= "Must be between $min and $max symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
205 6 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
206 6 ▶ return if !defined($_[0]) || $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 5 6
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
015
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
207 5 ▶ return if length( $_[0] ) >= $min && length( $_[0] ) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] >= $min and length $_[0] <= $max 2 3 5
Condition100%3/3
length $_[0] >= $min and length $_[0] <= $max
!ll&&!rl&&r
122
MC/DC100%2/2
length $_[0] >= $min and length $_[0] <= $max
length $_[0] >= $min length $_[0] <= $max
Truth table100%3/3
A and B
A= length $_[0] >= $minB= length $_[0] <= $max
ABresult
0X0
100
111
208 3 return $err_msg;
209 2 };
210 }
211  
212 2 ▶sub is_long_at_least {
Subroutine is_long_at_least: called CC=2 SCAR=6.9
Pod documented
213 2 my ( $length, $err_msg ) = @_;
214 2 ▶ $err_msg ||= "Must be at least $length symbols";
Condition100%2/2
$err_msg ||= "Must be at least $length symbols"
l!l
11
MC/DC100%1/1
$err_msg ||= "Must be at least $length symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
215 4 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
216 4 ▶ return if !defined($_[0]) || $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 3 4
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
013
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
217 3 ▶ return if length( $_[0] ) >= $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] >= $length 1 2 3
218 2 return $err_msg;
219 2 };
220 }
221  
222 2 ▶sub is_long_at_most {
Subroutine is_long_at_most: called CC=2 SCAR=6.9
Pod documented
223 2 my ( $length, $err_msg ) = @_;
224 2 ▶ $err_msg ||= "Must be at the most $length symbols";
Condition100%2/2
$err_msg ||= "Must be at the most $length symbols"
l!l
11
MC/DC100%1/1
$err_msg ||= "Must be at the most $length symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
225 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
226 5 ▶ return if !defined($_[0]) || $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
227 4 ▶ return if length( $_[0] ) <= $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] <= $length 2 2 4
228 2 return $err_msg;
229 2 };
230 }
231  
232 2 ▶sub is_a {
Subroutine is_a: called CC=2 SCAR=6.9
Pod documented
233 2 my ( $class, $err_msg ) = @_;
234 2 ▶ $err_msg ||= "Invalid value";
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
11
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
235 5 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
236 5 ▶ return if !defined( $_[0] ) || ref( $_[0] ) eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] eq $class 2 3 5
Condition100%3/3
not defined $_[0] or ref $_[0] eq $class
l!l&&r!l&&!r
113
MC/DC100%2/2
not defined $_[0] or ref $_[0] eq $class
not defined $_[0] ref $_[0] eq $class
Truth table100%3/3
not(A) or B
A= not defined $_[0]B= ref $_[0] eq $class
ABresult
1X1
011
000
237 3 return $err_msg;
238 2 };
239 }
240  
241 3 ▶sub is_like {
Subroutine is_like: called CC=3 SCAR=11.0
Pod documented
242 3 my ( $regexp, $err_msg ) = @_;
243 3 ▶ $err_msg ||= "Invalid value";
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
12
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
244 3 ▶ croak 'Regexp expected' unless ref($regexp) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $regexp eq "Regexp" 1 2 3
245 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
246 5 ▶ return if !defined($_[0]) || $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
247 4 ▶ return if $_[0] =~ $regexp;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /$regexp/ 1 3 4
248 3 return $err_msg;
249 2 };
250 }
251  
252 3 ▶sub is_in {
Subroutine is_in: called CC=3 SCAR=11.0
Pod documented
253 3 my ( $arrayref, $err_msg ) = @_;
254 3 ▶ $err_msg ||= "Invalid value";
Condition100%2/2
$err_msg ||= "Invalid value"
l!l
12
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
Aresult
11
00
255 3 ▶ croak 'ArrayRef expected' unless ref($arrayref) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayref eq "ARRAY" 1 2 3
256 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
257 5 ▶ return if !defined($_[0]) || $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or $_[0] eq "" 1 4 5
Condition66%2/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
258 4 ▶ return if _match( $_[0], $arrayref );
Branch: 1 branch
BranchTrueFalseTotal
if _match($_[0], $arrayref) 1 3 4
259 3 return $err_msg;
260 }
261 2 }
262  
263 6 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
264 6 my ( $class, %args ) = @_;
265 6 my $filters = $args{filters};
266 6 ▶ if ( defined $filters && ref $filters eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filters and ref $filters eq "HASH") 1 5 6
Condition66%2/3
defined $filters and ref $filters eq "HASH"
!ll&&!rl&&r
501
MC/DC50%1/2
defined $filters and ref $filters eq "HASH"
defined $filters ref $filters eq "HASH"
Truth table66%2/3
A and B
A= defined $filtersB= ref $filters eq "HASH"
ABresult
0X0
100
111
267 1 for my $key ( keys %$filters ) {
268 1 ▶ $FILTERS{$key} = $filters->{$key} if ref $filters->{$key} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filters->{$key} eq "CODE" 1 0 1
269 }
270 }
271 6 bless \%args, $class;
272 }
273  
274 6 ▶sub check {
Subroutine check: called CC=4 SCAR=13.9
Pod documented
275 6 my ( $self, $input, $rules, %args ) = @_;
276 6 ▶ $self = $self->new( %args ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 5 1 6
277  
278 6 ▶ if ( ref $input ne 'HASH' || ref $rules ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "HASH" or ref $rules ne "HASH") 2 4 6
Condition100%3/3
ref $input ne "HASH" or ref $rules ne "HASH"
l!l&&r!l&&!r
114
MC/DC100%2/2
ref $input ne "HASH" or ref $rules ne "HASH"
ref $input ne "HASH" ref $rules ne "HASH"
Truth table100%3/3
A or B
A= ref $input ne "HASH"B= ref $rules ne "HASH"
ABresult
1X1
011
000
279 2 confess("Parameters and rules HASH refs are needed");
280 }
281  
282 4 $self->{input} = $input;
283 4 $self->{rules} = $rules;
284 4 $self->{result} = validate( $input, $rules );
285  
286 4 return $self;
287 }
288  
289 27 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=9 SCAR=22.0
290 27 my $self = shift;
291 27 our $AUTOLOAD;
292 27 ▶ my $sub = $AUTOLOAD =~ /::(\w+)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$Validate::Tiny::AUTOLOAD =~ /::(\w+)$/ ? : 27 0 27
293 27 ▶ if ( $sub =~ /(params|rules)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($sub =~ /(params|rules)/) { } 0 27 27
elsif ($sub =~ /(data|error)/) { } 21 6 27
elsif ($sub eq 'success') { } 3 3 6
elsif ($sub eq 'to_hash') { } 2 1 3
294 0 return $self->{$sub};
295 }
296 elsif ( $sub =~ /(data|error)/ ) {
297 21 ▶ if ( my $field = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = shift()) { } 8 13 21
298 8 my $fields = $self->{rules}->{fields};
299 8 ▶ if ( scalar(@$fields) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$fields) 6 2 8
300 6 ▶ croak("Undefined field $sub($field)")
Branch: 1 branch
BranchTrueFalseTotal
unless _match($field, $fields) 3 3 6
301 unless _match( $field, $fields );
302 }
303 5 return $self->{result}->{$sub}->{ $field };
304 }
305 else {
306 13 return {%{$self->{result}->{$sub}}};
307 }
308 }
309 elsif ( $sub eq 'success' ) {
310 return $self->{result}->{success}
311 3 }
312 elsif ( $sub eq 'to_hash' ) {
313 2 return {%{$self->{result}}}
314 }
315 else {
316 1 confess "Undefined method $AUTOLOAD";
317 }
318 }
319  
320 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
321  
322 22 1;
323  
324 __END__
↑ Summary