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