line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Plugin::ValidateTiny; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16375
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
51
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
697
|
use Dancer ':syntax'; |
|
1
|
|
|
|
|
195735
|
|
|
1
|
|
|
|
|
9
|
|
7
|
1
|
|
|
1
|
|
1321
|
use Dancer::Plugin; |
|
1
|
|
|
|
|
1446
|
|
|
1
|
|
|
|
|
92
|
|
8
|
1
|
|
|
1
|
|
705
|
use Validate::Tiny ':all'; |
|
1
|
|
|
|
|
12239
|
|
|
1
|
|
|
|
|
236
|
|
9
|
1
|
|
|
1
|
|
759
|
use Email::Valid; |
|
1
|
|
|
|
|
93814
|
|
|
1
|
|
|
|
|
351
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $settings = plugin_setting; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
register validator => sub |
18
|
|
|
|
|
|
|
{ |
19
|
0
|
|
|
0
|
|
|
my ($params, $rules_file) = @_; |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
my $result = {}; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Loading rules from file |
24
|
0
|
|
|
|
|
|
my $rules = _load_rules($rules_file); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Validating |
27
|
0
|
|
|
|
|
|
my $validator = Validate::Tiny->check($params, $rules); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# If you need a full Validate::Tiny object |
30
|
0
|
0
|
|
|
|
|
if($settings->{is_full} eq 1) |
31
|
|
|
|
|
|
|
{ |
32
|
0
|
|
|
|
|
|
return $validator; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
if($validator->success) |
36
|
|
|
|
|
|
|
{ |
37
|
|
|
|
|
|
|
# All ok |
38
|
0
|
|
|
|
|
|
$result = { |
39
|
|
|
|
|
|
|
result => $validator->data, |
40
|
|
|
|
|
|
|
valid => $validator->success |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else |
44
|
|
|
|
|
|
|
{ |
45
|
|
|
|
|
|
|
# Returning errors |
46
|
0
|
0
|
|
|
|
|
if(exists $settings->{error_prefix}) |
47
|
|
|
|
|
|
|
{ |
48
|
|
|
|
|
|
|
# With error prefixes from config |
49
|
0
|
|
|
|
|
|
$result = { |
50
|
|
|
|
|
|
|
result => _set_error_prefixes($validator->error), |
51
|
|
|
|
|
|
|
valid => $validator->success |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else |
55
|
|
|
|
|
|
|
{ |
56
|
|
|
|
|
|
|
# Without error prefixes |
57
|
0
|
|
|
|
|
|
$result = { |
58
|
|
|
|
|
|
|
result => $validator->error, |
59
|
|
|
|
|
|
|
valid => $validator->success |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Combining filtered params and validation results |
65
|
0
|
|
|
|
|
|
%{$result->{result}} = (%{$result->{result}}, %{$validator->data}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Returning validated data |
68
|
0
|
|
|
|
|
|
return $result; |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _set_error_prefixes |
72
|
|
|
|
|
|
|
{ |
73
|
0
|
|
|
0
|
|
|
my $errors = shift; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
foreach my $error (keys %{$errors}) |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
{ |
77
|
|
|
|
|
|
|
# Replacing keys with prefix. O_o |
78
|
0
|
|
|
|
|
|
$errors->{$settings->{error_prefix} . $error} = delete $errors->{$error}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return $errors; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _load_rules |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
|
|
0
|
|
|
my $rules_file = shift; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Checking plugin settings and rules file for existing |
89
|
0
|
0
|
|
|
|
|
die "Rules directory not specified in plugin settings!" if !$settings->{rules_dir}; |
90
|
0
|
0
|
|
|
|
|
die "Rules file not specified!" if !$rules_file; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Making full path to rules file |
93
|
0
|
|
|
|
|
|
$rules_file = setting('appdir') . '/' . $settings->{rules_dir} . "/" . $rules_file; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Putting rules from file to $rules |
96
|
0
|
|
0
|
|
|
|
my $rules = do $rules_file || die $! . "\n" . $@; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
return $rules; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub check_email |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
0
|
1
|
|
my ($email, $message) = @_; |
105
|
0
|
0
|
|
|
|
|
Email::Valid->address($email) ? undef : $message; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
register_plugin; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
__END__ |