line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Validator::LIVR::Rules::Meta; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
15
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
112
|
|
4
|
4
|
|
|
4
|
|
14
|
use warnings; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
3254
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '2.0'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub nested_object { |
9
|
24
|
|
|
24
|
0
|
26
|
my ($livr, $rule_builders) = @_; |
10
|
|
|
|
|
|
|
|
11
|
24
|
|
|
|
|
45
|
my $validator = Validator::LIVR->new($livr)->register_rules(%$rule_builders)->prepare(); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
return sub { |
14
|
27
|
|
|
27
|
|
30
|
my ( $nested_object, $params, $output_ref ) = @_; |
15
|
27
|
50
|
33
|
|
|
97
|
return if !defined($nested_object) || $nested_object eq ''; |
16
|
|
|
|
|
|
|
|
17
|
27
|
100
|
|
|
|
51
|
return 'FORMAT_ERROR' unless ref($nested_object) eq 'HASH'; |
18
|
|
|
|
|
|
|
|
19
|
26
|
|
|
|
|
50
|
my $result = $validator->validate( $nested_object ); |
20
|
|
|
|
|
|
|
|
21
|
26
|
100
|
|
|
|
32
|
if ( $result ) { |
22
|
14
|
|
|
|
|
16
|
$$output_ref = $result; |
23
|
14
|
|
|
|
|
20
|
return; |
24
|
|
|
|
|
|
|
} else { |
25
|
12
|
|
|
|
|
22
|
return $validator->get_errors(); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
24
|
|
|
|
|
170
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub list_of { |
32
|
16
|
|
|
16
|
0
|
11
|
my ( $rules, $rule_builders ); |
33
|
|
|
|
|
|
|
|
34
|
16
|
100
|
|
|
|
28
|
if (ref $_[0] eq 'ARRAY') { |
35
|
4
|
|
|
|
|
6
|
( $rules, $rule_builders ) = @_; |
36
|
|
|
|
|
|
|
} else { |
37
|
12
|
|
|
|
|
19
|
$rules = [@_]; |
38
|
12
|
|
|
|
|
12
|
$rule_builders = pop @$rules; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
16
|
|
|
|
|
23
|
my $livr = { field => $rules }; |
42
|
|
|
|
|
|
|
|
43
|
16
|
|
|
|
|
31
|
my $validator = Validator::LIVR->new($livr)->register_rules(%$rule_builders)->prepare(); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
return sub { |
46
|
16
|
|
|
16
|
|
17
|
my ( $values, $params, $output_ref ) = @_; |
47
|
16
|
100
|
66
|
|
|
57
|
return if !defined($values) || $values eq ''; |
48
|
|
|
|
|
|
|
|
49
|
15
|
100
|
|
|
|
31
|
return 'FORMAT_ERROR' unless ref($values) eq 'ARRAY'; |
50
|
|
|
|
|
|
|
|
51
|
14
|
|
|
|
|
10
|
my ( @results, @errors ); |
52
|
|
|
|
|
|
|
|
53
|
14
|
|
|
|
|
13
|
foreach my $val (@$values) { |
54
|
43
|
100
|
|
|
|
84
|
if ( my $result = $validator->validate( {field => $val} ) ) { |
55
|
34
|
|
|
|
|
32
|
push @results, $result->{field}; |
56
|
34
|
|
|
|
|
55
|
push @errors, undef; |
57
|
|
|
|
|
|
|
} else { |
58
|
9
|
|
|
|
|
15
|
push @errors, $validator->get_errors()->{field}; |
59
|
9
|
|
|
|
|
15
|
push @results, undef; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
14
|
100
|
|
|
|
16
|
if ( grep {$_} @errors ) { |
|
43
|
|
|
|
|
47
|
|
64
|
4
|
|
|
|
|
8
|
return \@errors; |
65
|
|
|
|
|
|
|
} else { |
66
|
10
|
|
|
|
|
9
|
$$output_ref = \@results; |
67
|
10
|
|
|
|
|
18
|
return; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
16
|
|
|
|
|
122
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub list_of_objects { |
74
|
4
|
|
|
4
|
0
|
5
|
my ($livr, $rule_builders) = @_; |
75
|
|
|
|
|
|
|
|
76
|
4
|
|
|
|
|
11
|
my $validator = Validator::LIVR->new($livr)->register_rules(%$rule_builders)->prepare(); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return sub { |
79
|
4
|
|
|
4
|
|
4
|
my ( $objects, $params, $output_ref ) = @_; |
80
|
4
|
50
|
33
|
|
|
21
|
return if !defined($objects) || $objects eq ''; |
81
|
|
|
|
|
|
|
|
82
|
4
|
100
|
|
|
|
8
|
return 'FORMAT_ERROR' unless ref($objects) eq 'ARRAY'; |
83
|
|
|
|
|
|
|
|
84
|
3
|
|
|
|
|
4
|
my ( @results, @errors ); |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
4
|
foreach my $obj (@$objects) { |
87
|
7
|
100
|
|
|
|
12
|
if ( my $result = $validator->validate($obj) ) { |
88
|
4
|
|
|
|
|
5
|
push @results, $result; |
89
|
4
|
|
|
|
|
6
|
push @errors, undef; |
90
|
|
|
|
|
|
|
} else { |
91
|
3
|
|
|
|
|
7
|
push @errors, $validator->get_errors(); |
92
|
3
|
|
|
|
|
5
|
push @results, undef; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
3
|
100
|
|
|
|
4
|
if ( grep {$_} @errors ) { |
|
7
|
|
|
|
|
11
|
|
97
|
1
|
|
|
|
|
4
|
return \@errors; |
98
|
|
|
|
|
|
|
} else { |
99
|
2
|
|
|
|
|
3
|
$$output_ref = \@results; |
100
|
2
|
|
|
|
|
4
|
return; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
4
|
|
|
|
|
34
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub list_of_different_objects { |
106
|
3
|
|
|
3
|
0
|
5
|
my ( $selector_field, $livrs, $rule_builders ) = @_; |
107
|
|
|
|
|
|
|
|
108
|
3
|
|
|
|
|
5
|
my %validators; |
109
|
3
|
|
|
|
|
6
|
foreach my $selector_value ( keys %$livrs ) { |
110
|
6
|
|
|
|
|
17
|
my $validator = Validator::LIVR->new( $livrs->{$selector_value} )->register_rules(%$rule_builders)->prepare(); |
111
|
|
|
|
|
|
|
|
112
|
6
|
|
|
|
|
15
|
$validators{$selector_value} = $validator; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
return sub { |
117
|
3
|
|
|
3
|
|
5
|
my ( $objects, $params, $output_ref ) = @_; |
118
|
3
|
50
|
33
|
|
|
13
|
return if !defined($objects) || $objects eq ''; |
119
|
|
|
|
|
|
|
|
120
|
3
|
50
|
|
|
|
6
|
return 'FORMAT_ERROR' unless ref($objects) eq 'ARRAY'; |
121
|
|
|
|
|
|
|
|
122
|
3
|
|
|
|
|
4
|
my ( @results, @errors ); |
123
|
|
|
|
|
|
|
|
124
|
3
|
|
|
|
|
6
|
foreach my $obj (@$objects) { |
125
|
8
|
100
|
66
|
|
|
41
|
if ( ref($obj) ne 'HASH' || !$obj->{$selector_field} || !$validators{$obj->{$selector_field}} ) { |
|
|
|
100
|
|
|
|
|
126
|
2
|
|
|
|
|
3
|
push @errors, 'FORMAT_ERROR'; |
127
|
2
|
|
|
|
|
3
|
next; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
6
|
|
|
|
|
6
|
my $validator = $validators{ $obj->{$selector_field} }; |
131
|
|
|
|
|
|
|
|
132
|
6
|
100
|
|
|
|
12
|
if ( my $result = $validator->validate($obj) ) { |
133
|
3
|
|
|
|
|
4
|
push @results, $result; |
134
|
3
|
|
|
|
|
4
|
push @errors, undef; |
135
|
|
|
|
|
|
|
} else { |
136
|
3
|
|
|
|
|
7
|
push @errors, $validator->get_errors(); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
3
|
100
|
|
|
|
10
|
if ( grep {$_} @errors ) { |
|
8
|
|
|
|
|
9
|
|
141
|
1
|
|
|
|
|
3
|
return \@errors; |
142
|
|
|
|
|
|
|
} else { |
143
|
2
|
|
|
|
|
3
|
$$output_ref = \@results; |
144
|
2
|
|
|
|
|
4
|
return; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
3
|
|
|
|
|
23
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub variable_object { |
152
|
8
|
|
|
8
|
0
|
11
|
my ( $selector_field, $livrs, $rule_builders ) = @_; |
153
|
|
|
|
|
|
|
|
154
|
8
|
|
|
|
|
8
|
my %validators; |
155
|
8
|
|
|
|
|
15
|
foreach my $selector_value ( keys %$livrs ) { |
156
|
16
|
|
|
|
|
33
|
my $validator = Validator::LIVR->new( $livrs->{$selector_value} )->register_rules(%$rule_builders)->prepare(); |
157
|
|
|
|
|
|
|
|
158
|
16
|
|
|
|
|
34
|
$validators{$selector_value} = $validator; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
return sub { |
163
|
8
|
|
|
8
|
|
8
|
my ( $object, $params, $output_ref ) = @_; |
164
|
8
|
50
|
33
|
|
|
28
|
return if !defined($object) || $object eq ''; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
8
|
100
|
66
|
|
|
44
|
if ( ref($object) ne 'HASH' || !$object->{$selector_field} || !$validators{$object->{$selector_field}} ) { |
|
|
|
100
|
|
|
|
|
168
|
2
|
|
|
|
|
4
|
return 'FORMAT_ERROR'; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
6
|
|
|
|
|
8
|
my $validator = $validators{ $object->{$selector_field} }; |
172
|
|
|
|
|
|
|
|
173
|
6
|
100
|
|
|
|
9
|
if ( my $result = $validator->validate($object) ) { |
174
|
3
|
|
|
|
|
4
|
$$output_ref = $result; |
175
|
3
|
|
|
|
|
5
|
return; |
176
|
|
|
|
|
|
|
} else { |
177
|
3
|
|
|
|
|
10
|
return $validator->get_errors(); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
} |
180
|
8
|
|
|
|
|
52
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub livr_or { # we call it livr_or to avoid conflicts with the "or" operator |
184
|
11
|
|
|
11
|
0
|
16
|
my @rule_sets = @_; |
185
|
11
|
|
|
|
|
11
|
my $rule_builders = pop @rule_sets; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
my @validators = map { |
188
|
11
|
|
|
|
|
13
|
Validator::LIVR->new( { field => $_ } )->register_rules(%$rule_builders)->prepare() |
|
22
|
|
|
|
|
48
|
|
189
|
|
|
|
|
|
|
} @rule_sets; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
return sub { |
192
|
13
|
|
|
13
|
|
16
|
my ($value, undef, $output_ref) = @_; |
193
|
13
|
50
|
33
|
|
|
41
|
return if !defined($value) || $value eq ''; |
194
|
|
|
|
|
|
|
|
195
|
13
|
|
|
|
|
7
|
my $last_error; |
196
|
|
|
|
|
|
|
|
197
|
13
|
|
|
|
|
13
|
for my $validator (@validators) { |
198
|
22
|
|
|
|
|
70
|
my $result = $validator->validate({ field => $value }); |
199
|
|
|
|
|
|
|
|
200
|
22
|
100
|
|
|
|
31
|
if ($result) { |
201
|
8
|
|
|
|
|
8
|
$$output_ref = $result->{field}; |
202
|
8
|
|
|
|
|
12
|
return; |
203
|
|
|
|
|
|
|
} else { |
204
|
14
|
|
|
|
|
22
|
$last_error = $validator->get_errors()->{field}; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
5
|
50
|
|
|
|
15
|
return $last_error if $last_error; |
209
|
0
|
|
|
|
|
|
return; |
210
|
|
|
|
|
|
|
} |
211
|
11
|
|
|
|
|
71
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
1; |