DynamicValidator.pm

filter: Statementstmt 100% 208 / 208 Branchbran 91.9% 57 / 62 Conditioncond 79.2% 42 / 53 MC/DCmcdc 57.1% 16 / 28 Subroutinesub 100% 35 / 35 Podpod 87.5% 7 / 8 totaltotal 92.6% 365 / 394 CC 51 SCAR 21.2
CC
51
Cov
95%
_expand_routes
22.0
validate
10.4
_validate_children
5.0
CRAP 8.3
↑ Summary DynamicValidator/Filter.pm »
1 package Data::DynamicValidator;
2 #ABSTRACT: JPointer-like and Perl union for flexible perl data structures validation
3 10 $Data::DynamicValidator::VERSION = '0.05';
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Devel::LexAlias qw(lexalias);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use PadWalker qw(peek_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use aliased qw/Data::DynamicValidator::Error/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use aliased qw/Data::DynamicValidator::Filter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use aliased qw/Data::DynamicValidator::Label/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use aliased qw/Data::DynamicValidator::Path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 use overload
19 10 ▶ fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
20 37 ▶ '&{}' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 37 my $self = shift;
22 37 ▶ return sub { $self->validate(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
23 10 };
24  
25 10 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 our @EXPORT_OK = qw/validator/;
27  
28 10 ▶use constant DEBUG => $ENV{DATA_DYNAMICVALIDATOR_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'} || 0
l!l
010
MC/DC0%0/1
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'} || 0
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'}
Truth table50%1/2
A
A= $ENV{'DATA_DYNAMICVALIDATOR_DEBUG'}
Aresult
11
00
29  
30  
31  
32  
33  
34  
35  
36  
37 50 ▶sub validator {
Subroutine validator: called CC=1 SCAR=0.0
Pod documented
38 50 return Data::DynamicValidator->new(@_);
39 }
40  
41 50 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
42 50 my ($class, $data) = @_;
43 50 my $self = {
44 _data => $data,
45 _errors => [],
46 _bases => [],
47 };
48 50 return bless $self => $class;
49 }
50  
51  
52 41 ▶sub validate {
Subroutine validate: called CC=10 SCAR=23.4
Pod documented
53 41 my ($self, %args) = @_;
54  
55 41 my $on = $args{on };
56 41 my $should = $args{should };
57 41 my $because = $args{because};
58 41 my $each = $args{each };
59  
60 41 ▶ croak("Wrong arguments: 'on', 'should', 'because' should be specified")
Branch: 1 branch
BranchTrueFalseTotal
unless $on and $should and $because 0 41 41
Condition33%2/6
$on and $should and $because
!ll&&!rl&&r
0041
$on and $should
!ll&&!rl&&r
0041
MC/DC0%0/3
$on and $should and $because
$on $should $because
Truth table25%1/4
A and B and C
A= $onB= $shouldC= $because
ABCresult
0XX0
10X0
1100
1111
61 if(!$on || !$should || !$because);
62  
63 41 warn "-- validating : $on \n" if DEBUG;
64  
65 41 my $errors = $self->{_errors};
66 41 my $selection_results;
67 41 ▶ if ( !@$errors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$errors) 41 0 41
68 41 my $success;
69 41 my $current_base = $self->current_base;
70 41 my $selector = $self->_rebase_selector($on);
71 41 ($success, $selection_results) = $self->_apply($selector, $should);
72 41 ▶ if (!$success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 6 35 41
73 # if we met an error, and there is only 1 error path
74 # we report with expanded path, istead of unexpanded
75 6 my $error_routes = $selection_results->{routes};
76 6 ▶ my $reported_error_path = $error_routes && @$error_routes == 1
Branch: 1 branch
BranchTrueFalseTotal
$error_routes && @$error_routes == 1 ? : 3 3 6
Condition66%2/3
$error_routes and @$error_routes == 1
!ll&&!rl&&r
033
MC/DC50%1/2
$error_routes and @$error_routes == 1
$error_routes @$error_routes == 1
Truth table66%2/3
A and B
A= $error_routesB= @$error_routes == 1
ABresult
0X0
100
111
77 ? $error_routes->[0]
78 : $selector;
79 6 $self->report_error($because, $reported_error_path);
80 }
81 }
82 # OK, now going to child rules if there is no errors
83 41 ▶ if ( !@$errors && $each ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$errors and $each) 11 30 41
Condition100%3/3
not @$errors and $each
!ll&&!rl&&r
62411
MC/DC100%2/2
not @$errors and $each
not @$errors $each
Truth table100%3/3
not(A) and B
A= not @$errorsB= $each
ABresult
0X0
100
111
84 11 warn "-- no errors, will check children\n" if DEBUG;
85 11 $self->_validate_children($selection_results, $each);
86 }
87  
88 41 return $self;
89 }
90  
91  
92 7 ▶sub report_error {
Subroutine report_error: called CC=3 SCAR=11.2
Pod documented
93 7 my ($self, $reason, $path) = @_;
94 7 ▶ $path //= $self->{_current_path};
Condition66%2/3
$path //= $self->{"_current_path"}
l!l&&r!l&&!r
610
MC/DC0%0/2
$path //= $self->{"_current_path"}
$path $self->{"_current_path"}
Truth table66%2/3
A //= B
A= $pathB= $self->{"_current_path"}
ABresult
1X1
011
000
95 7 ▶ croak "Can't report error unless path is undefined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 7 7
96 unless defined $path;
97 7 push @{ $self->{_errors} }, Error->new($reason, $path);
98 }
99  
100  
101 9 ▶sub is_valid { @{ $_[0]->{_errors} } == 0; }
Subroutine is_valid: called CC=1 SCAR=0.0
Pod documented
102  
103  
104  
105 11 ▶sub errors { $_[0]->{_errors} }
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
106  
107  
108 5 ▶sub rebase {
Subroutine rebase: called CC=3 SCAR=11.0
Pod documented
109 5 my ($self, $expandable_route, $rule) = @_;
110 5 my $current_base = $self->current_base;
111 5 my $selector = $self->_rebase_selector($expandable_route);
112 5 my $scenario = $self->_select($selector);
113 5 my $number_of_routes = @{ $scenario->{routes} };
114 5 ▶ carp "The route '$expandable_route' is ambigious for rebasing (should be unique)"
Branch: 1 branch
BranchTrueFalseTotal
if $number_of_routes > 1 0 5 5
115 if $number_of_routes > 1;
116  
117 5 ▶ return $self if $number_of_routes == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $number_of_routes == 0 1 4 5
118  
119 4 push @{ $self->{_bases} }, $scenario->{routes}->[0];
120 4 $rule->($self);
121 4 pop @{ $self->{_bases} };
122 4 return $self;
123 }
124  
125  
126 94 ▶sub current_base {
Subroutine current_base: called CC=2 SCAR=6.9
Pod documented
127 94 my $bases = $_[0]->{_bases};
128 94 ▶ return undef unless @$bases;
Branch: 1 branch
BranchTrueFalseTotal
unless @$bases 38 56 94
129 56 return $bases->[-1];
130 }
131  
132 ### private/implementation methods
133  
134 46 ▶sub _rebase_selector {
Subroutine _rebase_selector: called CC=3 SCAR=11.0
135 46 my ($self, $selector) = @_;
136 46 my $current_base = $self->current_base;
137 46 ▶ my $add_base = $current_base && $selector !~ /^\/{2,}/;
Condition100%3/3
$current_base && !($selector =~ m[^/{2,}])
!ll&&!rl&&r
19189
MC/DC100%2/2
$current_base && !($selector =~ m[^/{2,}])
$current_base !($selector =~ m[^/{2,}])
Truth table100%3/3
A && not(B)
A= $current_baseB= !($selector =~ m[^/{2,}])
ABresult
0X0
100
111
138 46 ▶ my $rebased = $add_base ? $current_base . $selector : $selector;
Branch: 1 branch
BranchTrueFalseTotal
$add_base ? : 9 37 46
139 46 warn "-- Rebasing selector $selector to $rebased \n" if DEBUG;
140 46 return $rebased;
141 }
142  
143 11 ▶sub _validate_children {
Subroutine _validate_children: called CC=5 SCAR=16.1
144 11 my ($self, $selection_results, $each) = @_;
145 11 my ($routes, $values) = @{$selection_results}{qw/routes values/};
146 11 my $errors = $self->{_errors};
147 11 my $data = $self->{_data};
148 11 for my $i (0 .. @$routes-1) {
149 23 my $route = $routes->[$i];
150 23 push @{ $self->{_bases} }, $route;
151 23 my $value = $values->[$i];
152 23 my $label_for = { map { $_ => 1 } ($route->labels) };
153 # prepare context
154 23 my $pad = peek_sub($each);
155 23 ▶ while (my ($var, $ref) = each %$pad) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$pad) 38 23 61
156 38 my $var_name = substr($var, 1); # chomp sigil
157 38 ▶ next unless exists $label_for->{$var_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $label_for->{$var_name} 15 23 38
158 23 my $label_obj = Label->new($var_name, $route, $data);
159 23 lexalias($each, $var, \$label_obj);
160 }
161 # call
162 23 $self->{_current_path} = $route;
163 23 $each->($self, local $_ = Label->new('_', $route, $data));
164 23 pop @{ $self->{_bases} };
165 23 ▶ last if(@$errors);
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 3 20 23
166 }
167 }
168  
169  
170 # Takes path-like expandable expression and returns hashref of path with corresponding
171 # values from data, e.g.
172  
173 # validator({ a => [5,'z']})->_select('/a/*');
174 # # will return
175 # # {
176 # # routes => ['/a/0', '/a/1'],
177 # # values => [5, z],
178 # # }
179  
180 # Actualy routes are presented by Path objects.
181  
182 60 ▶sub _select {
Subroutine _select: called CC=1 SCAR=0.0
183 60 my ($self, $expession) = @_;
184 60 my $data = $self->{_data};
185 60 my $routes = $self->_expand_routes($expession);
186 60 my $values = [ map { $_->value($data) } @$routes ];
187 return {
188 60 routes => $routes,
189 values => $values,
190 };
191 }
192  
193  
194  
195 # Takes xpath-like expandable expression and sorted array of exapnded path e.g.
196 # validator({ a => [5,'z']})->_expand_routes('/a/*');
197 # # will return [ '/a/0', '/a/1' ]
198 # validator({ a => [5,'z']})->_expand_routes('/a');
199 # # will return [ '/a' ]
200 # validator({ a => { b => [5,'z'], c => ['y']} })->_expand_routes('/a/*/*');
201 # # will return [ '/a/b/0', '/a/b/1', '/a/c/0' ]
202  
203 80 ▶sub _expand_routes {
Subroutine _expand_routes: called CC=22 SCAR=30.9
204 80 my ($self, $expression) = @_;
205 80 warn "-- Expanding routes for $expression\n" if DEBUG;
206 # striping leading slashes
207 80 $expression =~ s/\/{2,}/\//;
208 80 my @routes = ( Path->new($expression) );
209 80 my $result = [];
210 80 ▶ while (@routes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@routes) 187 80 267
211 187 my $route = shift(@routes);
212 187 my $current = $self->{_data};
213 187 my $elements = $route->components;
214 187 my $i;
215 187 my $can_be_accessed = 0;
216 187 ▶ for ($i = 0; $i < @$elements; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$elements) 597 106 703
217 597 $can_be_accessed = 0;
218 597 my $element = $elements->[$i];
219 # no futher examination if current value is undefined
220 597 ▶ last unless defined($current);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 6 591 597
221 591 ▶ next if($element eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $element eq "" 187 404 591
222 404 my $filter;
223 404 ($element, $filter) = _filter($element);
224 404 my $type = ref($current);
225 404 my $generator;
226 my $advancer;
227 404 ▶ if ($element eq '*') {
Branch: 3 branches
BranchTrueFalseTotal
if ($element eq '*') { } 75 329 404
elsif ($type eq 'HASH' and exists $current->{$element}) { } 228 101 329
elsif ($type eq 'ARRAY' and &looks_like_number($element) and $element >= 0 && $element < @$current || $element < 0 && abs $element <= @$current) { } 92 17 109
228 75 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 44 31 75
elsif ($type eq 'ARRAY') { } 31 0 31
229 44 my @keys = keys %$current;
230 44 my $idx = 0;
231 98 ▶ $generator = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
232 98 ▶ while($idx < @keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @keys) 86 44 130
233 86 my $key = $keys[$idx++];
234 86 my $match = $filter->($current->{$key}, {key => $key});
235 86 ▶ return $key if($match);
Branch: 1 branch
BranchTrueFalseTotal
if $match 54 32 86
236 }
237 44 return undef;
238 44 };
239 } elsif ($type eq 'ARRAY') {
240 31 my $idx = 0;
241 84 ▶ $generator = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
242 84 ▶ while($idx < @$current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @$current) 65 31 96
243 65 my $index = $idx++;
244 65 my $match = $filter->($current->[$index], {index => $index});
245 65 ▶ return $index if($match);
Branch: 1 branch
BranchTrueFalseTotal
if $match 53 12 65
246 }
247 31 return undef;
248 31 };
249 }
250 ▶ }elsif ($type eq 'HASH' && exists $current->{$element}) {
Condition100%3/3
$type eq "HASH" and exists $current->{$element}
!ll&&!rl&&r
956228
MC/DC100%2/2
$type eq "HASH" and exists $current->{$element}
$type eq "HASH" exists $current->{$element}
Truth table100%3/3
A and B
A= $type eq "HASH"B= exists $current->{$element}
ABresult
0X0
100
111
251 228 ▶ $advancer = sub { $current->{$element} };
Subroutine __ANON__: called CC=1 SCAR=0.0
252 ▶ }elsif ($type eq 'ARRAY' && looks_like_number($element)
Condition93%14/15
$type eq "ARRAY" and &looks_like_number($element) and $element >= 0 && $element < @$current || $element < 0 && abs $element <= @$current
!ll&&!rl&&r
7292
$type eq "ARRAY" and &looks_like_number($element)
!ll&&!rl&&r
7094
$element >= 0 and $element < @$current or $element < 0 and abs $element <= @$current
l!l&&r!l&&!r
9022
$element >= 0 and $element < @$current
!ll&&!rl&&r
3190
$element < 0 and abs $element <= @$current
!ll&&!rl&&r
112
MC/DC66%4/6
$type eq "ARRAY" and &looks_like_number($element) and $element >= 0 && $element < @$current || $element < 0 && abs $element <= @$current
$type eq "ARRAY" &looks_like_number($element) $element >= 0 $element < @$current $element < 0 abs $element <= @$current
Truth table55%5/9
A and B and C and D or E and F
A= $type eq "ARRAY"B= &looks_like_number($element)C= $element >= 0D= $element < @$currentE= $element < 0F= abs $element <= @$current
ABCDEFresult
0XXXXX0
10XXXX0
110X0X0
110X100
11100X0
1110100
1111XX1
110X111
1110111
253 && (
254 ($element >= 0 && $element < @$current)
255 || ($element < 0 && abs($element) <= @$current)
256 )
257 ){
258 92 ▶ $advancer = sub { $current->[$element] };
Subroutine __ANON__: called CC=1 SCAR=0.0
259 }
260 404 ▶ if ($generator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($generator) 75 329 404
261 75 ▶ while ( defined( my $new_element = $generator->()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $new_element = &$generator())) 107 75 182
262 107 my $new_path = dclone($route);
263 107 $new_path->components->[$i] = $new_element;
264 107 push @routes, $new_path;
265 }
266 75 $current = undef;
267 75 last;
268 }
269 329 ▶ if ($advancer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($advancer) 320 9 329
270 320 $current = $advancer->();
271 320 $can_be_accessed = 1;
272 320 next;
273 }
274 # the current element isn't hash nor array
275 # we can't traverse further, because there is more
276 # else current path
277 9 $current = undef;
278 9 $can_be_accessed = 0;
279 }
280 187 ▶ my $do_expansion = defined $current
Condition83%5/6
defined $current || $can_be_accessed && $i == @$elements
l!l&&r!l&&!r
99385
$can_be_accessed and $i == @$elements
!ll&&!rl&&r
8503
MC/DC66%2/3
defined $current || $can_be_accessed && $i == @$elements
defined $current $can_be_accessed $i == @$elements
Truth table75%3/4
A || B and C
A= defined $currentB= $can_be_accessedC= $i == @$elements
ABCresult
1XX1
0111
00X0
0100
281 || ($can_be_accessed && $i == @$elements);
282 187 warn "-- Expanded route : $route \n" if(DEBUG && $do_expansion);
283 187 ▶ push @$result, $route if($do_expansion);
Branch: 1 branch
BranchTrueFalseTotal
if $do_expansion 102 85 187
284 }
285 80 return [ sort @$result ];
286 }
287  
288 404 ▶sub _filter {
Subroutine _filter: called CC=3 SCAR=11.0
289 404 my $element = shift;
290 404 my $filter;
291 404 my $condition_re = qr/(.+?)(\[(.+)\])/;
292 404 my @parts = $element =~ /$condition_re/;
293 404 ▶ if (@parts == 3 && defined($parts[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 3 and defined $parts[2]) { } 20 384 404
Condition66%2/3
@parts == 3 and defined $parts[2]
!ll&&!rl&&r
384020
MC/DC50%1/2
@parts == 3 and defined $parts[2]
@parts == 3 defined $parts[2]
Truth table66%2/3
A and B
A= @parts == 3B= defined $parts[2]
ABresult
0X0
100
111
294 20 $element = $parts[0];
295 20 my $condition = $parts[2];
296 20 $filter = Filter->new($condition);
297 } else {
298 384 ▶ $filter = sub { 1 }; # always true
Subroutine __ANON__: called CC=1 SCAR=0.0
299 }
300 404 return ($element, $filter);
301 }
302  
303  
304 # Takes the expandable expression and validation closure, then
305 # expands it, and applies the closure for every data piese,
306 # obtainted from expansion.
307  
308 # Returns the list of success validation mark and the hash
309 # of details (obtained via _select).
310  
311 54 ▶sub _apply {
Subroutine _apply: called CC=3 SCAR=11.0
312 54 my ($self, $on, $should) = @_;
313 54 my $selection_results = $self->_select($on);
314 54 my $values = $selection_results->{values};
315 54 ▶ my $result = $values && @$values && $should->( @$values );
Condition83%5/6
$values && @$values && &$should(@$values)
!ll&&!rl&&r
5445
$values and @$values
!ll&&!rl&&r
0549
MC/DC66%2/3
$values && @$values && &$should(@$values)
$values @$values &$should(@$values)
Truth table75%3/4
A and B && C
A= $valuesB= @$valuesC= &$should(@$values)
ABCresult
0XX0
10X0
1100
1111
316 54 return ($result, $selection_results);
317 };
318  
319 10 1;
320  
321 __END__
↑ Summary DynamicValidator/Filter.pm »