line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
{ |
2
|
|
|
|
|
|
|
package Kelp::Module::ValidateTiny; |
3
|
|
|
|
|
|
|
|
4
|
5
|
|
|
5
|
|
156182
|
use Kelp::Base 'Kelp::Module'; |
|
5
|
|
|
|
|
868
|
|
|
5
|
|
|
|
|
38
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
7514
|
use Validate::Tiny; |
|
5
|
|
|
|
|
43370
|
|
|
5
|
|
|
|
|
44
|
|
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
5434
|
use Class::Load; |
|
5
|
|
|
|
|
281839
|
|
|
5
|
|
|
|
|
281
|
|
9
|
5
|
|
|
5
|
|
49
|
use Sub::Install; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
54
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# These are Validate::Tiny functions that we |
14
|
|
|
|
|
|
|
# forward into the application namespace |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my @forward_ok = qw{ |
17
|
|
|
|
|
|
|
filter |
18
|
|
|
|
|
|
|
is_required |
19
|
|
|
|
|
|
|
is_required_if |
20
|
|
|
|
|
|
|
is_equal |
21
|
|
|
|
|
|
|
is_long_between |
22
|
|
|
|
|
|
|
is_long_at_least |
23
|
|
|
|
|
|
|
is_long_at_most |
24
|
|
|
|
|
|
|
is_a |
25
|
|
|
|
|
|
|
is_like |
26
|
|
|
|
|
|
|
is_in |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub build { |
30
|
|
|
|
|
|
|
|
31
|
4
|
|
|
4
|
1
|
797
|
my ($self, %args) = @_; |
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
|
|
9
|
my @import; |
34
|
|
|
|
|
|
|
# Imported from Validate::Tiny? |
35
|
4
|
100
|
66
|
|
|
43
|
if (%args && |
36
|
|
|
|
|
|
|
exists $args{subs}) { |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
7
|
@import = @{$args{subs}}; |
|
3
|
|
|
|
|
13
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
4
|
50
|
66
|
|
|
44
|
@import = @forward_ok if (@import && $import[0] eq ':all'); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Namespaces to import into (default is our App) |
44
|
|
|
|
|
|
|
# If our App name is Kelp, we are probably running |
45
|
|
|
|
|
|
|
# from a standalone script and our classname is main |
46
|
|
|
|
|
|
|
|
47
|
4
|
|
|
|
|
18
|
my $class = ref $self->app; |
48
|
4
|
50
|
|
|
|
40
|
$class = 'main' if ($class eq 'Kelp'); |
49
|
4
|
|
|
|
|
8
|
my @into = ($class); |
50
|
|
|
|
|
|
|
|
51
|
4
|
100
|
100
|
|
|
28
|
if (%args && |
52
|
|
|
|
|
|
|
exists $args{into}) { |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
1
|
push @into, @{$args{into}}; |
|
1
|
|
|
|
|
3
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Import! |
58
|
4
|
|
|
|
|
11
|
foreach (@into) { |
59
|
|
|
|
|
|
|
|
60
|
5
|
|
|
|
|
83
|
my $class = $_; |
61
|
|
|
|
|
|
|
|
62
|
5
|
50
|
|
|
|
20
|
Class::Load::load_class($class) |
63
|
|
|
|
|
|
|
unless Class::Load::is_class_loaded($class); |
64
|
|
|
|
|
|
|
|
65
|
5
|
|
|
|
|
943
|
foreach (@import) { |
66
|
|
|
|
|
|
|
|
67
|
8
|
|
|
|
|
455
|
Sub::Install::install_sub({ |
68
|
|
|
|
|
|
|
code => Validate::Tiny->can($_), |
69
|
|
|
|
|
|
|
from => 'Validate::Tiny', |
70
|
|
|
|
|
|
|
into => $class, |
71
|
|
|
|
|
|
|
}); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Register a single method - self->validate |
76
|
|
|
|
|
|
|
$self->register( |
77
|
4
|
|
|
|
|
204
|
validate => \&_validate |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _validate { |
82
|
|
|
|
|
|
|
|
83
|
14
|
|
|
14
|
|
217874
|
my $self = shift; |
84
|
14
|
|
|
|
|
31
|
my $rules = shift; |
85
|
14
|
|
|
|
|
36
|
my %args = @_; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Combine all params |
88
|
|
|
|
|
|
|
# TODO: check if mixed can be avoided |
89
|
|
|
|
|
|
|
# on the Hash::Multivalue "parameters" |
90
|
|
|
|
|
|
|
|
91
|
14
|
|
|
|
|
51
|
my $input = { |
92
|
14
|
|
|
|
|
12172
|
%{$self->req->parameters->mixed}, |
93
|
14
|
|
|
|
|
33
|
%{$self->req->named} |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
|
96
|
14
|
|
|
|
|
316
|
my $result = Validate::Tiny->new($input, $rules); |
97
|
|
|
|
|
|
|
|
98
|
14
|
100
|
66
|
|
|
3611
|
return $result if ( |
|
|
|
100
|
|
|
|
|
99
|
|
|
|
|
|
|
$result->success || (!(%args && exists $args{on_error})) |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# There are errors and a template is passed |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
32
|
my $data = $result->data; |
105
|
1
|
|
|
|
|
21
|
$data->{error} = $result->error; |
106
|
|
|
|
|
|
|
|
107
|
1
|
50
|
|
|
|
28
|
if (exists $args{data}) { |
108
|
1
|
|
|
|
|
5
|
$data = { |
109
|
|
|
|
|
|
|
%$data, |
110
|
1
|
|
|
|
|
3
|
%{$args{data}}, |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
5
|
return Validate::Tiny::PlackResponse->new( |
115
|
|
|
|
|
|
|
$result, |
116
|
|
|
|
|
|
|
$self->res->template($args{on_error}, $data) |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
{ |
124
|
|
|
|
|
|
|
package Validate::Tiny::PlackResponse; |
125
|
|
|
|
|
|
|
|
126
|
5
|
|
|
5
|
|
4480
|
use parent Validate::Tiny; |
|
5
|
|
|
|
|
352
|
|
|
5
|
|
|
|
|
47
|
|
127
|
5
|
|
|
5
|
|
638
|
use Scalar::Util qw{blessed refaddr}; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
1811
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my %_response; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub new { |
132
|
|
|
|
|
|
|
|
133
|
1
|
|
|
1
|
|
560
|
my ($class, $obj, $response) = @_; |
134
|
|
|
|
|
|
|
|
135
|
1
|
50
|
|
|
|
10
|
die "Incorrect Parent Class. Not an instance of Validate::Tiny" |
136
|
|
|
|
|
|
|
unless blessed($obj) eq 'Validate::Tiny'; |
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
5
|
$_response{refaddr $obj} = $response; |
139
|
|
|
|
|
|
|
|
140
|
1
|
|
|
|
|
3
|
bless $obj, $class; |
141
|
|
|
|
|
|
|
|
142
|
1
|
|
|
|
|
6
|
return $obj; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub response { |
146
|
|
|
|
|
|
|
|
147
|
1
|
|
|
1
|
|
45
|
my $self = shift; |
148
|
1
|
50
|
|
|
|
6
|
die "Incorrect Parent Class. Not an instance of Validate::Tiny::PlackResponse" |
149
|
|
|
|
|
|
|
unless blessed($self) eq 'Validate::Tiny::PlackResponse'; |
150
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
6
|
return $_response{refaddr $self}; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub DESTROY { |
155
|
|
|
|
|
|
|
|
156
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
157
|
|
|
|
|
|
|
|
158
|
1
|
|
|
|
|
2
|
my $key = refaddr $self; |
159
|
1
|
50
|
|
|
|
5
|
delete $_response{$key} if exists $_response{$key}; |
160
|
|
|
|
|
|
|
|
161
|
1
|
50
|
|
|
|
13
|
$self->SUPER::DESTROY if $self->SUPER::can(DESTROY); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |
166
|
|
|
|
|
|
|
__END__ |