line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Data::Validate::WithYAML; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: validate form input with Data::Validate::WithYAML |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
3008
|
use strict; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
117
|
|
6
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
109
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
20
|
use parent 'Mojolicious::Plugin'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
25
|
|
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
252
|
use Carp; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
231
|
|
11
|
4
|
|
|
4
|
|
1912
|
use Data::Validate::WithYAML; |
|
4
|
|
|
|
|
34283
|
|
|
4
|
|
|
|
|
149
|
|
12
|
4
|
|
|
4
|
|
28
|
use File::Spec; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
1761
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.05; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
4
|
|
|
4
|
1
|
2808
|
my ($self, $app, $config) = @_; |
18
|
|
|
|
|
|
|
|
19
|
4
|
100
|
|
|
|
28
|
$config->{conf_path} = $app->home if !$config->{conf_path}; |
20
|
4
|
100
|
|
|
|
23
|
$config->{no_steps} = 1 if !defined $config->{no_steps}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$app->helper( 'validate' => sub { |
23
|
5
|
|
|
5
|
|
42343
|
my ($c, $file) = @_; |
24
|
|
|
|
|
|
|
|
25
|
5
|
|
|
|
|
19
|
my $validator = _validator( $file, $config ); |
26
|
5
|
|
|
|
|
12
|
my %params = %{ $c->req->params->to_hash }; |
|
5
|
|
|
|
|
34
|
|
27
|
5
|
|
|
|
|
2548
|
my %errors = $validator->validate( %params ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $prefix = exists $config->{error_prefix} ? |
30
|
|
|
|
|
|
|
$config->{error_prefix} : |
31
|
5
|
100
|
|
|
|
2346
|
'ERROR_'; |
32
|
|
|
|
|
|
|
|
33
|
5
|
|
|
|
|
18
|
my %prefixed_errors = map{ ( "$prefix$_" => $errors{$_} ) } keys %errors; |
|
9
|
|
|
|
|
39
|
|
34
|
|
|
|
|
|
|
|
35
|
5
|
|
|
|
|
117
|
return %prefixed_errors; |
36
|
4
|
|
|
|
|
53
|
}); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$app->helper( 'fieldinfo' => sub { |
39
|
6
|
|
|
6
|
|
30263
|
my ($c, $file, $field, $subinfo) = @_; |
40
|
|
|
|
|
|
|
|
41
|
6
|
|
|
|
|
18
|
my $validator = _validator( $file, $config ); |
42
|
3
|
|
|
|
|
10
|
my $info = $validator->fieldinfo( $field ); |
43
|
|
|
|
|
|
|
|
44
|
3
|
100
|
|
|
|
62
|
return if !$info; |
45
|
|
|
|
|
|
|
|
46
|
2
|
100
|
|
|
|
25
|
return $info if !$subinfo; |
47
|
1
|
|
|
|
|
27
|
return $info->{$subinfo}; |
48
|
4
|
|
|
|
|
601
|
}); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _validator { |
52
|
11
|
|
|
11
|
|
23
|
my ($file, $config) = @_; |
53
|
|
|
|
|
|
|
|
54
|
11
|
100
|
|
|
|
38
|
if ( !$file ) { |
55
|
6
|
|
|
|
|
73
|
my @caller = caller(3); |
56
|
6
|
|
|
|
|
39
|
$file = (split /::/, $caller[3])[-1]; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $path = File::Spec->rel2abs( |
60
|
11
|
|
|
|
|
515
|
File::Spec->catfile( $config->{conf_path}, $file . '.yml' ) |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
11
|
100
|
|
|
|
774
|
croak "$path does not exist" if !-e $path; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $validator = Data::Validate::WithYAML->new( |
66
|
|
|
|
|
|
|
$path, |
67
|
9
|
100
|
|
|
|
25
|
%{$config}, |
|
9
|
|
|
|
|
99
|
|
68
|
|
|
|
|
|
|
) or croak $Data::Validate::WithYAML::errstr; |
69
|
|
|
|
|
|
|
|
70
|
8
|
|
|
|
|
25457
|
return $validator; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |