line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Validator::LIVR::Rules::Helpers; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
19
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
149
|
|
4
|
4
|
|
|
4
|
|
23
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
3281
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub nested_object { |
9
|
19
|
|
|
19
|
0
|
25
|
my ($livr, $rule_builders) = @_; |
10
|
|
|
|
|
|
|
|
11
|
19
|
|
|
|
|
51
|
my $validator = Validator::LIVR->new($livr)->register_rules(%$rule_builders)->prepare(); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
return sub { |
14
|
19
|
|
|
19
|
|
24
|
my ( $nested_object, $params, $output_ref ) = @_; |
15
|
19
|
50
|
33
|
|
|
95
|
return if !defined($nested_object) || $nested_object eq ''; |
16
|
|
|
|
|
|
|
|
17
|
19
|
100
|
|
|
|
43
|
return 'FORMAT_ERROR' unless ref($nested_object) eq 'HASH'; |
18
|
|
|
|
|
|
|
|
19
|
18
|
|
|
|
|
50
|
my $result = $validator->validate( $nested_object ); |
20
|
|
|
|
|
|
|
|
21
|
18
|
100
|
|
|
|
31
|
if ( $result ) { |
22
|
11
|
|
|
|
|
35
|
$$output_ref = $result; |
23
|
11
|
|
|
|
|
22
|
return; |
24
|
|
|
|
|
|
|
} else { |
25
|
7
|
|
|
|
|
18
|
return $validator->get_errors(); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
19
|
|
|
|
|
166
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub list_of { |
32
|
13
|
|
|
13
|
0
|
13
|
my ( $rules, $rule_builders ); |
33
|
|
|
|
|
|
|
|
34
|
13
|
100
|
|
|
|
20
|
if (ref $_[0] eq 'ARRAY') { |
35
|
4
|
|
|
|
|
5
|
( $rules, $rule_builders ) = @_; |
36
|
|
|
|
|
|
|
} else { |
37
|
9
|
|
|
|
|
14
|
$rules = [@_]; |
38
|
9
|
|
|
|
|
14
|
$rule_builders = pop @$rules; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
13
|
|
|
|
|
25
|
my $livr = { field => $rules }; |
42
|
|
|
|
|
|
|
|
43
|
13
|
|
|
|
|
34
|
my $validator = Validator::LIVR->new($livr)->register_rules(%$rule_builders)->prepare(); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
return sub { |
46
|
13
|
|
|
13
|
|
15
|
my ( $values, $params, $output_ref ) = @_; |
47
|
13
|
100
|
66
|
|
|
48
|
return if !defined($values) || $values eq ''; |
48
|
|
|
|
|
|
|
|
49
|
12
|
100
|
|
|
|
27
|
return 'FORMAT_ERROR' unless ref($values) eq 'ARRAY'; |
50
|
|
|
|
|
|
|
|
51
|
11
|
|
|
|
|
9
|
my ( @results, @errors ); |
52
|
|
|
|
|
|
|
|
53
|
11
|
|
|
|
|
12
|
foreach my $val (@$values) { |
54
|
39
|
100
|
|
|
|
84
|
if ( my $result = $validator->validate( {field => $val} ) ) { |
55
|
32
|
|
|
|
|
39
|
push @results, $result->{field}; |
56
|
32
|
|
|
|
|
51
|
push @errors, undef; |
57
|
|
|
|
|
|
|
} else { |
58
|
7
|
|
|
|
|
12
|
push @errors, $validator->get_errors()->{field}; |
59
|
7
|
|
|
|
|
13
|
push @results, undef; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
11
|
100
|
|
|
|
15
|
if ( grep {$_} @errors ) { |
|
39
|
|
|
|
|
41
|
|
64
|
3
|
|
|
|
|
7
|
return \@errors; |
65
|
|
|
|
|
|
|
} else { |
66
|
8
|
|
|
|
|
11
|
$$output_ref = \@results; |
67
|
8
|
|
|
|
|
14
|
return; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
13
|
|
|
|
|
100
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub list_of_objects { |
74
|
3
|
|
|
3
|
0
|
5
|
my ($livr, $rule_builders) = @_; |
75
|
|
|
|
|
|
|
|
76
|
3
|
|
|
|
|
8
|
my $validator = Validator::LIVR->new($livr)->register_rules(%$rule_builders)->prepare(); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return sub { |
79
|
3
|
|
|
3
|
|
5
|
my ( $objects, $params, $output_ref ) = @_; |
80
|
3
|
50
|
33
|
|
|
16
|
return if !defined($objects) || $objects eq ''; |
81
|
|
|
|
|
|
|
|
82
|
3
|
100
|
|
|
|
8
|
return 'FORMAT_ERROR' unless ref($objects) eq 'ARRAY'; |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
3
|
my ( @results, @errors ); |
85
|
|
|
|
|
|
|
|
86
|
2
|
|
|
|
|
4
|
foreach my $obj (@$objects) { |
87
|
7
|
100
|
|
|
|
12
|
if ( my $result = $validator->validate($obj) ) { |
88
|
4
|
|
|
|
|
4
|
push @results, $result; |
89
|
4
|
|
|
|
|
7
|
push @errors, undef; |
90
|
|
|
|
|
|
|
} else { |
91
|
3
|
|
|
|
|
6
|
push @errors, $validator->get_errors(); |
92
|
3
|
|
|
|
|
6
|
push @results, undef; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
2
|
100
|
|
|
|
4
|
if ( grep {$_} @errors ) { |
|
7
|
|
|
|
|
9
|
|
97
|
1
|
|
|
|
|
3
|
return \@errors; |
98
|
|
|
|
|
|
|
} else { |
99
|
1
|
|
|
|
|
2
|
$$output_ref = \@results; |
100
|
1
|
|
|
|
|
3
|
return; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
3
|
|
|
|
|
25
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub list_of_different_objects { |
106
|
2
|
|
|
2
|
0
|
3
|
my ( $selector_field, $livrs, $rule_builders ) = @_; |
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
3
|
my %validators; |
109
|
2
|
|
|
|
|
5
|
foreach my $selector_value ( keys %$livrs ) { |
110
|
4
|
|
|
|
|
11
|
my $validator = Validator::LIVR->new( $livrs->{$selector_value} )->register_rules(%$rule_builders)->prepare(); |
111
|
|
|
|
|
|
|
|
112
|
4
|
|
|
|
|
11
|
$validators{$selector_value} = $validator; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
return sub { |
117
|
2
|
|
|
2
|
|
3
|
my ( $objects, $params, $output_ref ) = @_; |
118
|
2
|
50
|
33
|
|
|
12
|
return if !defined($objects) || $objects eq ''; |
119
|
|
|
|
|
|
|
|
120
|
2
|
50
|
|
|
|
6
|
return 'FORMAT_ERROR' unless ref($objects) eq 'ARRAY'; |
121
|
|
|
|
|
|
|
|
122
|
2
|
|
|
|
|
2
|
my ( @results, @errors ); |
123
|
|
|
|
|
|
|
|
124
|
2
|
|
|
|
|
4
|
foreach my $obj (@$objects) { |
125
|
7
|
50
|
66
|
|
|
43
|
if ( ref($obj) ne 'HASH' || !$obj->{$selector_field} || !$validators{$obj->{$selector_field}} ) { |
|
|
|
66
|
|
|
|
|
126
|
1
|
|
|
|
|
2
|
push @errors, 'FORMAT_ERROR'; |
127
|
1
|
|
|
|
|
3
|
next; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
6
|
|
|
|
|
6
|
my $validator = $validators{ $obj->{$selector_field} }; |
131
|
|
|
|
|
|
|
|
132
|
6
|
100
|
|
|
|
13
|
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
|
2
|
100
|
|
|
|
5
|
if ( grep {$_} @errors ) { |
|
7
|
|
|
|
|
8
|
|
141
|
1
|
|
|
|
|
3
|
return \@errors; |
142
|
|
|
|
|
|
|
} else { |
143
|
1
|
|
|
|
|
2
|
$$output_ref = \@results; |
144
|
1
|
|
|
|
|
2
|
return; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
2
|
|
|
|
|
20
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |