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
|
5
|
|
|
5
|
|
4018
|
use strict; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
148
|
|
6
|
5
|
|
|
5
|
|
27
|
use warnings; |
|
5
|
|
|
|
|
22
|
|
|
5
|
|
|
|
|
137
|
|
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
27
|
use parent 'Mojolicious::Plugin'; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
24
|
|
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
332
|
use Carp; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
324
|
|
11
|
5
|
|
|
5
|
|
2639
|
use Data::Validate::WithYAML; |
|
5
|
|
|
|
|
46744
|
|
|
5
|
|
|
|
|
179
|
|
12
|
5
|
|
|
5
|
|
35
|
use Mojo::File qw(path); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
2591
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.06; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
5
|
|
|
5
|
1
|
4179
|
my ($self, $app, $config) = @_; |
18
|
|
|
|
|
|
|
|
19
|
5
|
100
|
|
|
|
29
|
$config->{conf_path} = $app->home if !$config->{conf_path}; |
20
|
5
|
100
|
|
|
|
24
|
$config->{no_steps} = 1 if !defined $config->{no_steps}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$app->helper( 'validate' => sub { |
23
|
7
|
|
|
7
|
|
78948
|
my ($c, $file, $step) = @_; |
24
|
|
|
|
|
|
|
|
25
|
7
|
|
|
|
|
29
|
my $validator = _validator( $file, $config ); |
26
|
7
|
|
|
|
|
13
|
my %params = %{ $c->req->params->to_hash }; |
|
7
|
|
|
|
|
41
|
|
27
|
7
|
100
|
|
|
|
3622
|
my @args = $step ? $step : (); |
28
|
7
|
|
|
|
|
57
|
my %errors = $validator->validate( @args, %params ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $prefix = exists $config->{error_prefix} ? |
31
|
|
|
|
|
|
|
$config->{error_prefix} : |
32
|
7
|
100
|
|
|
|
3490
|
'ERROR_'; |
33
|
|
|
|
|
|
|
|
34
|
7
|
|
|
|
|
30
|
my %prefixed_errors = map{ ( "$prefix$_" => $errors{$_} ) } keys %errors; |
|
11
|
|
|
|
|
46
|
|
35
|
|
|
|
|
|
|
|
36
|
7
|
|
|
|
|
168
|
return %prefixed_errors; |
37
|
5
|
|
|
|
|
53
|
}); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$app->helper( 'fieldinfo' => sub { |
40
|
6
|
|
|
6
|
|
38001
|
my ($c, $file, $field, $subinfo) = @_; |
41
|
|
|
|
|
|
|
|
42
|
6
|
|
|
|
|
19
|
my $validator = _validator( $file, $config ); |
43
|
3
|
|
|
|
|
15
|
my $info = $validator->fieldinfo( $field ); |
44
|
|
|
|
|
|
|
|
45
|
3
|
100
|
|
|
|
67
|
return if !$info; |
46
|
|
|
|
|
|
|
|
47
|
2
|
100
|
|
|
|
29
|
return $info if !$subinfo; |
48
|
1
|
|
|
|
|
41
|
return $info->{$subinfo}; |
49
|
5
|
|
|
|
|
830
|
}); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _validator { |
53
|
13
|
|
|
13
|
|
31
|
my ($file, $config) = @_; |
54
|
|
|
|
|
|
|
|
55
|
13
|
100
|
|
|
|
43
|
if ( !$file ) { |
56
|
6
|
|
|
|
|
77
|
my @caller = caller(3); |
57
|
6
|
|
|
|
|
47
|
$file = (split /::/, $caller[3])[-1]; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
13
|
|
|
|
|
69
|
my $path = path( $config->{conf_path}, $file . '.yml' )->to_string; |
61
|
|
|
|
|
|
|
|
62
|
13
|
100
|
|
|
|
1368
|
croak "$path does not exist" if !-e $path; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $validator = Data::Validate::WithYAML->new( |
65
|
|
|
|
|
|
|
$path, |
66
|
11
|
100
|
|
|
|
34
|
%{$config}, |
|
11
|
|
|
|
|
113
|
|
67
|
|
|
|
|
|
|
) or croak $Data::Validate::WithYAML::errstr; |
68
|
|
|
|
|
|
|
|
69
|
10
|
|
|
|
|
33269
|
return $validator; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
__END__ |