line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Plugin::DataTransposeValidator::Validator; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
913
|
use Data::Transpose::Validator; |
|
2
|
|
|
|
|
79581
|
|
|
2
|
|
|
|
|
60
|
|
4
|
2
|
|
|
2
|
|
12
|
use File::Spec; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
38
|
|
5
|
2
|
|
|
2
|
|
6
|
use Moo; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
7
|
|
6
|
2
|
|
|
2
|
|
391
|
use namespace::clean; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
14
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Dancer::Plugin::DataTransposeValidator::Validator - validator class for |
11
|
|
|
|
|
|
|
Dancer::Plugin::DataTransposeValidator |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 METHODS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head2 additional_args |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Any additional arguments passed in to validator are passed as arguments |
18
|
|
|
|
|
|
|
to L if it contains a code reference. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has additional_args => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => sub { |
25
|
|
|
|
|
|
|
die "params must be as array reference" unless ref( $_[0] ) eq 'ARRAY'; |
26
|
|
|
|
|
|
|
}, |
27
|
|
|
|
|
|
|
default => sub { [] }, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head2 appdir |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Dancer's appdir. Required. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has appdir => ( |
37
|
|
|
|
|
|
|
is => 'ro', |
38
|
|
|
|
|
|
|
isa => sub { |
39
|
|
|
|
|
|
|
die "appdir must be a valid string" |
40
|
|
|
|
|
|
|
unless ( defined $_[0] && $_[0] =~ /\S/ ); |
41
|
|
|
|
|
|
|
die "appdir must be a valid directory" unless -d $_[0]; |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
required => 1, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head2 css_error_class |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Returns the value supplied via L or 'has-error'. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
has css_error_class => ( |
53
|
|
|
|
|
|
|
is => 'lazy', |
54
|
|
|
|
|
|
|
isa => sub { |
55
|
|
|
|
|
|
|
die "rules_file must be a valid string" |
56
|
|
|
|
|
|
|
unless ( defined $_[0] && $_[0] =~ /\S/ ); |
57
|
|
|
|
|
|
|
}, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _build_css_error_class { |
61
|
13
|
|
|
13
|
|
824
|
my $self = shift; |
62
|
|
|
|
|
|
|
return |
63
|
|
|
|
|
|
|
defined $self->plugin_setting->{css_error_class} |
64
|
|
|
|
|
|
|
? $self->plugin_setting->{css_error_class} |
65
|
13
|
100
|
|
|
|
199
|
: 'has-error'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 params |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Hash reference of params to validate. Required. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
has params => ( |
75
|
|
|
|
|
|
|
is => 'ro', |
76
|
|
|
|
|
|
|
isa => sub { |
77
|
|
|
|
|
|
|
die "params must be a hash reference" unless ref( $_[0] ) eq 'HASH'; |
78
|
|
|
|
|
|
|
}, |
79
|
|
|
|
|
|
|
required => 1, |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 plugin_setting |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
plugin_setting hash reference. Required. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
has plugin_setting => ( |
89
|
|
|
|
|
|
|
is => 'ro', |
90
|
|
|
|
|
|
|
isa => sub { |
91
|
|
|
|
|
|
|
die "plugin_setting must be a hash reference" |
92
|
|
|
|
|
|
|
unless ref( $_[0] ) eq 'HASH'; |
93
|
|
|
|
|
|
|
}, |
94
|
|
|
|
|
|
|
required => 1, |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 rules_dir |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
rules_dir setting from L or 'validation' if that is undef |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
has rules_dir => ( |
104
|
|
|
|
|
|
|
is => 'lazy', |
105
|
|
|
|
|
|
|
isa => sub { |
106
|
|
|
|
|
|
|
die "rules directory does not exist: $_[0]" unless -d $_[0]; |
107
|
|
|
|
|
|
|
}, |
108
|
|
|
|
|
|
|
); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _build_rules_dir { |
111
|
19
|
|
|
19
|
|
861
|
my $self = shift; |
112
|
|
|
|
|
|
|
my $dir = |
113
|
|
|
|
|
|
|
defined $self->plugin_setting->{rules_dir} |
114
|
|
|
|
|
|
|
? $self->plugin_setting->{rules_dir} |
115
|
19
|
100
|
|
|
|
71
|
: 'validation'; |
116
|
19
|
|
|
|
|
366
|
return File::Spec->catdir( $self->appdir, $dir ); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 rules_file |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
The name of the rules file. Required. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
has rules_file => ( |
126
|
|
|
|
|
|
|
is => 'ro', |
127
|
|
|
|
|
|
|
isa => sub { |
128
|
|
|
|
|
|
|
die "rules_file must be a valid string" |
129
|
|
|
|
|
|
|
unless ( defined $_[0] && $_[0] =~ /\S/ ); |
130
|
|
|
|
|
|
|
}, |
131
|
|
|
|
|
|
|
required => 1, |
132
|
|
|
|
|
|
|
); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 rules |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
The rules produced via eval of L |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
has rules => ( |
141
|
|
|
|
|
|
|
is => 'lazy', |
142
|
|
|
|
|
|
|
isa => sub { |
143
|
|
|
|
|
|
|
die "plugin_setting must be a hash reference" |
144
|
|
|
|
|
|
|
unless ref( $_[0] ) eq 'HASH'; |
145
|
|
|
|
|
|
|
}, |
146
|
|
|
|
|
|
|
); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub _build_rules { |
149
|
19
|
|
|
19
|
|
865
|
my $self = shift; |
150
|
19
|
|
|
|
|
234
|
my $path = File::Spec->catfile( $self->rules_dir, $self->rules_file ); |
151
|
17
|
50
|
|
|
|
4058
|
my $rules = do $path or die "bad rules file: $path - $! $@"; |
152
|
17
|
100
|
|
|
|
327
|
if ( ref($rules) eq 'CODE' ) { |
153
|
4
|
|
|
|
|
5
|
return $rules->( @{ $self->additional_args } ); |
|
4
|
|
|
|
|
20
|
|
154
|
|
|
|
|
|
|
} |
155
|
13
|
|
|
|
|
251
|
return $rules; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 transpose |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Uses Data::Transpose::Validator to transpose and validate L. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub transpose { |
165
|
19
|
|
|
19
|
1
|
167
|
my $self = shift; |
166
|
|
|
|
|
|
|
|
167
|
19
|
|
|
|
|
233
|
my $rules = $self->rules; |
168
|
|
|
|
|
|
|
|
169
|
17
|
|
50
|
|
|
158
|
my $options = $rules->{options} || {}; |
170
|
17
|
|
50
|
|
|
38
|
my $prepare = $rules->{prepare} || {}; |
171
|
|
|
|
|
|
|
|
172
|
17
|
|
|
|
|
334
|
my $dtv = Data::Transpose::Validator->new(%$options); |
173
|
17
|
|
|
|
|
14803
|
$dtv->prepare(%$prepare); |
174
|
|
|
|
|
|
|
|
175
|
17
|
|
|
|
|
192063
|
my $params = $self->params; |
176
|
|
|
|
|
|
|
|
177
|
17
|
|
|
|
|
53
|
my $clean = $dtv->transpose($params); |
178
|
17
|
|
|
|
|
56751
|
my $ret; |
179
|
|
|
|
|
|
|
|
180
|
17
|
100
|
|
|
|
45
|
if ($clean) { |
181
|
4
|
|
|
|
|
8
|
$ret->{valid} = 1; |
182
|
4
|
|
|
|
|
7
|
$ret->{values} = $clean; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
else { |
185
|
13
|
|
|
|
|
29
|
$ret->{valid} = 0; |
186
|
13
|
|
|
|
|
38
|
$ret->{values} = $dtv->transposed_data; |
187
|
|
|
|
|
|
|
|
188
|
13
|
|
|
|
|
33
|
my $errors_hash = $self->plugin_setting->{errors_hash}; |
189
|
|
|
|
|
|
|
|
190
|
13
|
|
|
|
|
35
|
my $v_hash = $dtv->errors_hash; |
191
|
13
|
|
|
|
|
525
|
while ( my ( $key, $value ) = each %$v_hash ) { |
192
|
|
|
|
|
|
|
|
193
|
24
|
|
|
|
|
317
|
$ret->{css}->{$key} = $self->css_error_class; |
194
|
|
|
|
|
|
|
|
195
|
24
|
|
|
|
|
166
|
my @errors = map { $_->{value} } @{$value}; |
|
45
|
|
|
|
|
59
|
|
|
24
|
|
|
|
|
30
|
|
196
|
|
|
|
|
|
|
|
197
|
24
|
100
|
100
|
|
|
112
|
if ( $errors_hash && $errors_hash eq 'joined' ) { |
|
|
100
|
66
|
|
|
|
|
198
|
4
|
|
|
|
|
23
|
$ret->{errors}->{$key} = join( ". ", @errors ); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
elsif ( $errors_hash && $errors_hash eq 'arrayref' ) { |
201
|
4
|
|
|
|
|
18
|
$ret->{errors}->{$key} = \@errors; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
else { |
204
|
16
|
|
|
|
|
68
|
$ret->{errors}->{$key} = $errors[0]; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
} |
208
|
17
|
|
|
|
|
253
|
return $ret; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
1; |