LIVR/Rules/Meta.pm

filter: Statementstmt 100% 118 / 118 Branchbran 92.8% 39 / 42 Conditioncond 77.7% 21 / 27 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 6 totaltotal 90.5% 202 / 223 CC 37 SCAR 17.9
CC
37
Cov
95%
__ANON__
10.1
__ANON__
7.1
__ANON__
7.1
CRAP 6.0
« LIVR/Rules/Numeric.pm ↑ Summary LIVR.pm »
1 package Validator::LIVR::Rules::Meta;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '2.22';
7  
8 # The fields next to an item of list_of: none. Items have no containing object, so
9 # equal_to_field and the references of required_if and when see a missing value there
10 10 my $NO_FIELDS = {};
11  
12 # Arguments after the ones a rule takes are ignored: the rule builders are always the last one
13  
14 122 ▶sub nested_object {
Subroutine nested_object: called CC=1 SCAR=0.0
Pod undocumented
15 122 my ( $livr, $rule_builders ) = ( $_[0], $_[-1] );
16  
17 122 my $validator = Validator::LIVR->_new_with_rules( $livr, $rule_builders )->prepare();
18  
19 88 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
20 88 my ( $nested_object, $params, $output_ref ) = @_;
21 88 ▶ return if !defined($nested_object) || $nested_object eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $nested_object or $nested_object eq "" 2 86 88
Condition100%3/3
not defined $nested_object or $nested_object eq ""
l!l&&r!l&&!r
1186
MC/DC100%2/2
not defined $nested_object or $nested_object eq ""
not defined $nested_object $nested_object eq ""
Truth table100%3/3
not(A) or B
A= not defined $nested_objectB= $nested_object eq ""
ABresult
1X1
011
000
22  
23 86 ▶ return 'FORMAT_ERROR' unless ref($nested_object) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nested_object eq "HASH" 3 83 86
24  
25 83 my $result = $validator->validate( $nested_object );
26  
27 83 ▶ if ( $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 49 34 83
28 49 $$output_ref = $result;
29 49 return;
30 } else {
31 34 return $validator->get_errors();
32 }
33 }
34 120 }
35  
36  
37 69 ▶sub list_of {
Subroutine list_of: called CC=2 SCAR=6.9
Pod undocumented
38 69 my ( $rules, $rule_builders );
39  
40 69 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 5 64 69
41 5 ( $rules, $rule_builders ) = ( $_[0], $_[-1] );
42 } else {
43 64 $rules = [@_];
44 64 $rule_builders = pop @$rules;
45 }
46  
47 69 my $item_rule = Validator::LIVR->_new_with_rules( { field => $rules }, $rule_builders )->_field_rule('field');
48  
49 71 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
50 71 my ( $values, $params, $output_ref ) = @_;
51 71 ▶ return if !defined($values) || $values eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $values or $values eq "" 2 69 71
Condition100%3/3
not defined $values or $values eq ""
l!l&&r!l&&!r
1169
MC/DC100%2/2
not defined $values or $values eq ""
not defined $values $values eq ""
Truth table100%3/3
not(A) or B
A= not defined $valuesB= $values eq ""
ABresult
1X1
011
000
52  
53 69 ▶ return 'FORMAT_ERROR' unless ref($values) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "ARRAY" 4 65 69
54  
55 65 my ( @results, @errors, $has_errors );
56  
57 65 foreach my $val (@$values) {
58 143 my $output = $val;
59  
60 143 ▶ if ( my $err_code = $item_rule->( $val, $NO_FIELDS, \$output ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_code = &$item_rule($val, $NO_FIELDS, \$output)) { } 32 111 143
61 32 push @errors, $err_code;
62 32 push @results, undef;
63 32 $has_errors = 1;
64 } else {
65 111 push @results, $output;
66 111 push @errors, undef;
67 }
68 }
69  
70 65 ▶ if ( $has_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_errors) { } 25 40 65
71 25 return \@errors;
72 } else {
73 40 $$output_ref = \@results;
74 40 return;
75 }
76 }
77 68 }
78  
79  
80 17 ▶sub list_of_objects {
Subroutine list_of_objects: called CC=1 SCAR=0.0
Pod undocumented
81 17 my ( $livr, $rule_builders ) = ( $_[0], $_[-1] );
82  
83 17 my $validator = Validator::LIVR->_new_with_rules( $livr, $rule_builders )->prepare();
84  
85 17 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
86 17 my ( $objects, $params, $output_ref ) = @_;
87 17 ▶ return if !defined($objects) || $objects eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $objects or $objects eq "" 0 17 17
Condition33%1/3
not defined $objects or $objects eq ""
l!l&&r!l&&!r
0017
MC/DC0%0/2
not defined $objects or $objects eq ""
not defined $objects $objects eq ""
Truth table33%1/3
not(A) or B
A= not defined $objectsB= $objects eq ""
ABresult
1X1
011
000
88  
89 17 ▶ return 'FORMAT_ERROR' unless ref($objects) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $objects eq "ARRAY" 2 15 17
90  
91 15 my ( @results, @errors );
92  
93 15 foreach my $obj (@$objects) {
94 32 ▶ if ( my $result = $validator->validate($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $validator->validate($obj)) { } 21 11 32
95 21 push @results, $result;
96 21 push @errors, undef;
97 } else {
98 11 push @errors, $validator->get_errors();
99 11 push @results, undef;
100 }
101 }
102  
103 15 ▶ if ( grep {$_} @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_;} @errors) { } 7 8 15
104 7 return \@errors;
105 } else {
106 8 $$output_ref = \@results;
107 8 return;
108 }
109 }
110 17 }
111  
112 14 ▶sub list_of_different_objects {
Subroutine list_of_different_objects: called CC=2 SCAR=6.9
Pod undocumented
113 14 my ( $selector_field, $livrs, $rule_builders ) = ( @_[ 0, 1 ], $_[-1] );
114  
115 14 my %validators;
116 14 foreach my $selector_value ( keys %$livrs ) {
117 25 my $validator = Validator::LIVR->_new_with_rules( $livrs->{$selector_value}, $rule_builders )->prepare();
118  
119 25 $validators{$selector_value} = $validator;
120 }
121  
122  
123 14 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
124 14 my ( $objects, $params, $output_ref ) = @_;
125 14 ▶ return if !defined($objects) || $objects eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $objects or $objects eq "" 0 14 14
Condition33%1/3
not defined $objects or $objects eq ""
l!l&&r!l&&!r
0014
MC/DC0%0/2
not defined $objects or $objects eq ""
not defined $objects $objects eq ""
Truth table33%1/3
not(A) or B
A= not defined $objectsB= $objects eq ""
ABresult
1X1
011
000
126  
127 14 ▶ return 'FORMAT_ERROR' unless ref($objects) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $objects eq "ARRAY" 1 13 14
128  
129 13 my ( @results, @errors );
130  
131 13 foreach my $obj (@$objects) {
132 30 ▶ if ( ref($obj) ne 'HASH' || !$obj->{$selector_field} || !$validators{$obj->{$selector_field}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj ne "HASH" or not $obj->{$selector_field} or not $validators{$obj->{$selector_field}}) 15 15 30
Condition100%6/6
ref $obj ne "HASH" or not $obj->{$selector_field} or not $validators{$obj->{$selector_field}}
l!l&&r!l&&!r
7815
ref $obj ne "HASH" or not $obj->{$selector_field}
l!l&&r!l&&!r
6123
MC/DC100%3/3
ref $obj ne "HASH" or not $obj->{$selector_field} or not $validators{$obj->{$selector_field}}
ref $obj ne "HASH" not $obj->{$selector_field} not $validators{$obj->{$selector_field}}
Truth table100%4/4
A or not(B) or not(C)
A= ref $obj ne "HASH"B= not $obj->{$selector_field}C= not $validators{$obj->{$selector_field}}
ABCresult
1XX1
01X1
0011
0000
133 15 push @errors, 'FORMAT_ERROR';
134 15 next;
135 }
136  
137 15 my $validator = $validators{ $obj->{$selector_field} };
138  
139 15 ▶ if ( my $result = $validator->validate($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $validator->validate($obj)) { } 9 6 15
140 9 push @results, $result;
141 9 push @errors, undef;
142 } else {
143 6 push @errors, $validator->get_errors();
144 }
145 }
146  
147 13 ▶ if ( grep {$_} @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_;} @errors) { } 9 4 13
148 9 return \@errors;
149 } else {
150 4 $$output_ref = \@results;
151 4 return;
152 }
153 }
154 14 }
155  
156  
157  
158 21 ▶sub variable_object {
Subroutine variable_object: called CC=2 SCAR=7.0
Pod undocumented
159 21 my ( $selector_field, $livrs, $rule_builders ) = ( @_[ 0, 1 ], $_[-1] );
160  
161 21 my %validators;
162 21 foreach my $selector_value ( keys %$livrs ) {
163 37 my $validator = Validator::LIVR->_new_with_rules( $livrs->{$selector_value}, $rule_builders )->prepare();
164  
165 37 $validators{$selector_value} = $validator;
166 }
167  
168  
169 21 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
170 21 my ( $object, $params, $output_ref ) = @_;
171 21 ▶ return if !defined($object) || $object eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $object or $object eq "" 0 21 21
Condition33%1/3
not defined $object or $object eq ""
l!l&&r!l&&!r
0021
MC/DC0%0/2
not defined $object or $object eq ""
not defined $object $object eq ""
Truth table33%1/3
not(A) or B
A= not defined $objectB= $object eq ""
ABresult
1X1
011
000
172  
173  
174 21 ▶ if ( ref($object) ne 'HASH' || !$object->{$selector_field} || !$validators{$object->{$selector_field}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "HASH" or not $object->{$selector_field} or not $validators{$object->{$selector_field}}) 10 11 21
Condition100%6/6
ref $object ne "HASH" or not $object->{$selector_field} or not $validators{$object->{$selector_field}}
l!l&&r!l&&!r
3711
ref $object ne "HASH" or not $object->{$selector_field}
l!l&&r!l&&!r
2118
MC/DC100%3/3
ref $object ne "HASH" or not $object->{$selector_field} or not $validators{$object->{$selector_field}}
ref $object ne "HASH" not $object->{$selector_field} not $validators{$object->{$selector_field}}
Truth table100%4/4
A or not(B) or not(C)
A= ref $object ne "HASH"B= not $object->{$selector_field}C= not $validators{$object->{$selector_field}}
ABCresult
1XX1
01X1
0011
0000
175 10 return 'FORMAT_ERROR';
176 }
177  
178 11 my $validator = $validators{ $object->{$selector_field} };
179  
180 11 ▶ if ( my $result = $validator->validate($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $validator->validate($object)) { } 5 6 11
181 5 $$output_ref = $result;
182 5 return;
183 } else {
184 6 return $validator->get_errors();
185 }
186 }
187 21 }
188  
189  
190 28 ▶sub livr_or { # we call it livr_or to avoid conflicts with the "or" operator
Subroutine livr_or: called CC=1 SCAR=0.0
Pod undocumented
191 28 my @rule_sets = @_;
192 28 my $rule_builders = pop @rule_sets;
193  
194 # One validator for all rule sets, a field for each
195 28 my %livr = map { ( "or$_" => $rule_sets[$_] ) } 0 .. $#rule_sets;
196 28 my $validator = Validator::LIVR->_new_with_rules( \%livr, $rule_builders )->prepare();
197 28 my @field_rules = map { $validator->_field_rule("or$_") } 0 .. $#rule_sets;
198  
199 30 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
200 30 my ($value, $params, $output_ref) = @_;
201  
202 # Empty values go to the rule sets too, so "required" and "not_empty" in them work.
203 # The rule sets are transparent: their rules see the fields next to the field
204 30 my $last_error;
205  
206 30 for my $field_rule (@field_rules) {
207 53 my $output = $value;
208 53 my $err_code = $field_rule->( $value, $params, \$output );
209  
210 53 ▶ if ( !$err_code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err_code) 17 36 53
211 17 $$output_ref = $output;
212 17 return;
213 }
214  
215 36 $last_error = $err_code;
216 }
217  
218 13 return $last_error;
219 }
220 28 }
221  
222 10 1;
223  
224 =head1 NAME
225  
226 Validator::LIVR::Rules::Meta - Metarules of Validator::LIVR: nested_object, list_of, list_of_objects, variable_object, list_of_different_objects and or
227  
228 =head1 DESCRIPTION
229  
230 This module has the code of the metarules, which are documented in
231 L<Validator::LIVR/Metarules>.
232  
233 Each sub is the builder of the rule with the same name. The builder of C<or>
234 is C<livr_or>, as C<or> is a Perl operator. The code is a good example for
235 L<writing your own rules|Validator::LIVR/WRITING RULES IN PERL>.
236  
237 =cut
« LIVR/Rules/Numeric.pm ↑ Summary LIVR.pm »