line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Dancer-Plugin-FormValidator |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2013 by Natal Ngétal. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package Dancer::Plugin::FormValidator; |
10
|
|
|
|
|
|
|
{ |
11
|
|
|
|
|
|
|
$Dancer::Plugin::FormValidator::VERSION = '1.131620'; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
5
|
|
|
5
|
|
1676040
|
use strict; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
178
|
|
15
|
5
|
|
|
5
|
|
29
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
144
|
|
16
|
|
|
|
|
|
|
|
17
|
5
|
|
|
5
|
|
1597
|
use Dancer ':syntax'; |
|
5
|
|
|
|
|
302125
|
|
|
5
|
|
|
|
|
27
|
|
18
|
5
|
|
|
5
|
|
6604
|
use Dancer::Plugin; |
|
5
|
|
|
|
|
7652
|
|
|
5
|
|
|
|
|
391
|
|
19
|
5
|
|
|
5
|
|
67
|
use Dancer::Exception qw(:all); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
683
|
|
20
|
|
|
|
|
|
|
|
21
|
5
|
|
|
5
|
|
5437
|
use Data::FormValidator; |
|
5
|
|
|
|
|
184513
|
|
|
5
|
|
|
|
|
311
|
|
22
|
5
|
|
|
5
|
|
6426
|
use Module::Load; |
|
5
|
|
|
|
|
6319
|
|
|
5
|
|
|
|
|
67
|
|
23
|
|
|
|
|
|
|
|
24
|
5
|
|
|
5
|
|
1668
|
use 5.010; |
|
5
|
|
|
|
|
21
|
|
|
5
|
|
|
|
|
5543
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#ABSTRACT: Easy validates user input (usually from an HTML form) based on input profile for Dancer applications. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#Register exception |
29
|
|
|
|
|
|
|
register_exception('ProfileInvalidFormat', |
30
|
|
|
|
|
|
|
message_pattern => "Unknown format use yml, json or pl: %s" |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $dfv; |
34
|
|
|
|
|
|
|
my $results; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
register form_validator_error => sub { |
38
|
9
|
|
|
9
|
|
34414
|
$results = _dfv_check(@_); |
39
|
9
|
|
|
|
|
43
|
my $settings = plugin_setting; |
40
|
|
|
|
|
|
|
|
41
|
9
|
100
|
66
|
|
|
224
|
if ( $results->has_invalid || $results->has_missing ) { |
42
|
6
|
50
|
|
|
|
127
|
if ( $settings->{halt} ) { |
43
|
0
|
|
|
|
|
0
|
my @errors = keys(%{$results->{missing}}); |
|
0
|
|
|
|
|
0
|
|
44
|
0
|
|
|
|
|
0
|
my $string; |
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
0
|
$string = scalar(@errors) == 1 |
47
|
|
|
|
|
|
|
? "$settings->{msg}->{single} @errors" |
48
|
|
|
|
|
|
|
: "$settings->{msg}->{several} @errors"; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
0
|
return halt($string); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
6
|
50
|
|
|
|
45
|
return $results->has_missing |
54
|
|
|
|
|
|
|
? _error_return('missing') |
55
|
|
|
|
|
|
|
: _error_return('invalid'); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
60
|
return 0; |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
register dfv => sub { |
64
|
10
|
|
|
10
|
|
63931
|
_dfv_check(@_); |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
register_plugin for_versions => [2]; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _error_return { |
70
|
6
|
|
|
6
|
|
53
|
my $reason = shift; |
71
|
6
|
|
|
|
|
23
|
my $settings = plugin_setting; |
72
|
|
|
|
|
|
|
|
73
|
6
|
|
|
|
|
92
|
my @errors = keys(%{$results->{$reason}}); |
|
6
|
|
|
|
|
107
|
|
74
|
6
|
|
|
|
|
14
|
my $errors; |
75
|
|
|
|
|
|
|
my $value; |
76
|
|
|
|
|
|
|
|
77
|
6
|
50
|
|
|
|
29
|
if ( $results->{profile}->{msgs}->{$reason} ) { |
78
|
6
|
|
|
|
|
26
|
$value = $results->{profile}->{msgs}->{$reason}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
0
|
|
|
|
|
0
|
$value = $settings->{msg}->{single}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
6
|
|
|
|
|
16
|
foreach my $msg_errors (@errors) { |
85
|
12
|
|
|
|
|
35
|
$errors->{$msg_errors} = $value; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
6
|
|
|
|
|
31
|
return $errors; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _dfv_check { |
92
|
19
|
|
|
19
|
|
49
|
my ( $profile, $params ) = @_; |
93
|
|
|
|
|
|
|
|
94
|
19
|
100
|
|
|
|
84
|
_init_object_dfv() unless defined($dfv); |
95
|
18
|
|
33
|
|
|
213
|
$params //= params; |
96
|
18
|
|
|
|
|
5208
|
$results = $dfv->check($params, $profile); |
97
|
|
|
|
|
|
|
|
98
|
18
|
|
|
|
|
23162
|
return $results; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _init_object_dfv { |
102
|
4
|
|
|
4
|
|
25
|
my $settings = plugin_setting; |
103
|
4
|
|
100
|
|
|
98
|
my $path_file = $settings->{profile_file} // 'profile.yml'; |
104
|
4
|
|
|
|
|
22
|
my $profile_file = setting('appdir') . '/' . $path_file; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $available_deserializer = { |
107
|
|
|
|
|
|
|
json => sub { |
108
|
1
|
|
|
1
|
|
6
|
my ( $file ) = @_; |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
7
|
load JSON::Syck; |
111
|
|
|
|
|
|
|
|
112
|
1
|
|
|
|
|
83
|
my $data = JSON::Syck::LoadFile($file); |
113
|
1
|
|
|
|
|
377
|
return $data; |
114
|
|
|
|
|
|
|
}, |
115
|
|
|
|
|
|
|
yml => sub { |
116
|
1
|
|
|
1
|
|
3
|
my ( $file ) = @_; |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
|
|
6
|
load YAML::Syck; |
119
|
|
|
|
|
|
|
|
120
|
1
|
|
|
|
|
69
|
my $data = YAML::Syck::LoadFile($file); |
121
|
1
|
|
|
|
|
376
|
return $data; |
122
|
|
|
|
|
|
|
}, |
123
|
|
|
|
|
|
|
pl => sub { |
124
|
1
|
|
|
1
|
|
2
|
my ( $file ) = @_; |
125
|
|
|
|
|
|
|
|
126
|
1
|
|
|
|
|
2
|
my $exception; |
127
|
|
|
|
|
|
|
my $data; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
{ |
130
|
1
|
|
|
|
|
4
|
local $@; |
|
1
|
|
|
|
|
2
|
|
131
|
1
|
|
|
|
|
414
|
$data = do $file; |
132
|
1
|
|
|
|
|
15
|
$exception = $@; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
1
|
50
|
|
|
|
7
|
die $exception if $exception; |
136
|
|
|
|
|
|
|
|
137
|
1
|
|
|
|
|
11
|
return $data; |
138
|
|
|
|
|
|
|
}, |
139
|
4
|
|
|
|
|
299
|
}; |
140
|
|
|
|
|
|
|
|
141
|
4
|
|
|
|
|
32
|
$profile_file =~ m/\.(\w+$)/; |
142
|
4
|
|
|
|
|
23
|
my $ext = $1; |
143
|
|
|
|
|
|
|
|
144
|
4
|
100
|
|
|
|
22
|
if ( my $deserialize = $available_deserializer->{$ext} ) { |
145
|
3
|
|
|
|
|
9
|
$dfv = Data::FormValidator->new($deserialize->($profile_file)); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
else { |
148
|
1
|
|
|
|
|
5
|
raise ProfileInvalidFormat => $ext; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
1; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
__END__ |