line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Validator::Custom::Validation; |
2
|
5
|
|
|
5
|
|
15
|
use Object::Simple -base; |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
24
|
|
3
|
|
|
|
|
|
|
|
4
|
5
|
|
|
5
|
|
283
|
use Carp 'croak'; |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
2237
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new { |
7
|
18
|
|
|
18
|
1
|
43
|
my $self = shift->SUPER::new(@_); |
8
|
|
|
|
|
|
|
|
9
|
18
|
|
|
|
|
79
|
$self->{_failed_infos} = {}; |
10
|
|
|
|
|
|
|
|
11
|
18
|
|
|
|
|
23
|
return $self; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub is_valid { |
15
|
14
|
|
|
14
|
1
|
29
|
my ($self, $name) = @_; |
16
|
|
|
|
|
|
|
|
17
|
14
|
100
|
|
|
|
24
|
if (defined $name) { |
18
|
9
|
100
|
|
|
|
47
|
return exists $self->{_failed_infos}->{$name} ? 0 : 1; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
else { |
21
|
5
|
100
|
|
|
|
3
|
return !(keys %{$self->{_failed_infos}}) ? 1 : 0; |
|
5
|
|
|
|
|
21
|
|
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub add_failed { |
26
|
32
|
|
|
32
|
1
|
90
|
my ($self, $name, $message) = @_; |
27
|
|
|
|
|
|
|
|
28
|
32
|
|
|
|
|
40
|
my $failed_infos = $self->{_failed_infos}; |
29
|
|
|
|
|
|
|
|
30
|
32
|
50
|
|
|
|
45
|
if ($failed_infos->{$name}) { |
31
|
0
|
|
|
|
|
0
|
croak "\"$name\" is already exists"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
32
|
|
|
|
|
49
|
my @failed_names = keys %$failed_infos; |
35
|
32
|
|
|
|
|
21
|
my $pos; |
36
|
32
|
100
|
|
|
|
39
|
if (@failed_names) { |
37
|
20
|
|
|
|
|
14
|
my $max_pos = 0; |
38
|
20
|
|
|
|
|
39
|
for my $failed_name (@failed_names) { |
39
|
47
|
|
|
|
|
34
|
my $pos = $failed_infos->{$failed_name}{pos}; |
40
|
47
|
100
|
|
|
|
59
|
if ($pos > $max_pos) { |
41
|
22
|
|
|
|
|
19
|
$max_pos = $pos; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
20
|
|
|
|
|
21
|
$pos = $max_pos + 1; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
12
|
|
|
|
|
12
|
$pos = 0; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
32
|
|
|
|
|
46
|
$failed_infos->{$name}{pos} = $pos; |
51
|
|
|
|
|
|
|
|
52
|
32
|
100
|
|
|
|
40
|
unless (defined $message) { |
53
|
28
|
|
|
|
|
32
|
$message = "$name is invalid"; |
54
|
|
|
|
|
|
|
} |
55
|
32
|
|
|
|
|
34
|
$failed_infos->{$name}{message} = $message; |
56
|
32
|
|
|
|
|
27
|
$failed_infos->{$name}{pos} = $pos; |
57
|
|
|
|
|
|
|
|
58
|
32
|
|
|
|
|
55
|
return $self; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub failed { |
62
|
17
|
|
|
17
|
1
|
24
|
my $self = shift; |
63
|
|
|
|
|
|
|
|
64
|
17
|
|
|
|
|
15
|
my $failed_infos = $self->{_failed_infos}; |
65
|
17
|
|
|
|
|
44
|
my @failed = sort { $failed_infos->{$a}{pos} <=> |
66
|
41
|
|
|
|
|
55
|
$failed_infos->{$b}{pos} } keys %$failed_infos; |
67
|
|
|
|
|
|
|
|
68
|
17
|
|
|
|
|
63
|
return \@failed; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub message { |
72
|
8
|
|
|
8
|
1
|
13
|
my ($self, $name) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Parameter name not specified |
75
|
8
|
50
|
|
|
|
14
|
croak 'Parameter name must be specified' |
76
|
|
|
|
|
|
|
unless $name; |
77
|
|
|
|
|
|
|
|
78
|
8
|
|
|
|
|
26
|
return $self->{_failed_infos}{$name}{message}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub messages { |
82
|
5
|
|
|
5
|
1
|
6
|
my $self = shift; |
83
|
|
|
|
|
|
|
|
84
|
5
|
|
|
|
|
5
|
my $failed_infos = $self->{_failed_infos}; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Messages |
87
|
5
|
|
|
|
|
7
|
my $messages = []; |
88
|
5
|
|
|
|
|
3
|
for my $name (@{$self->failed}) { |
|
5
|
|
|
|
|
9
|
|
89
|
8
|
|
|
|
|
8
|
my $message = $failed_infos->{$name}{message}; |
90
|
8
|
|
|
|
|
10
|
push @$messages, $message; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
5
|
|
|
|
|
21
|
return $messages; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub messages_to_hash { |
97
|
5
|
|
|
5
|
1
|
7
|
my $self = shift; |
98
|
|
|
|
|
|
|
|
99
|
5
|
|
|
|
|
5
|
my $failed_infos = $self->{_failed_infos}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Name and message hash |
102
|
5
|
|
|
|
|
6
|
my $messages = {}; |
103
|
5
|
|
|
|
|
16
|
for my $name (keys %$failed_infos) { |
104
|
8
|
|
|
|
|
15
|
$messages->{$name} = $failed_infos->{$name}{message}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
5
|
|
|
|
|
18
|
return $messages; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 NAME |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Validator::Custom::Validation - a result of validation |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SYNOPSYS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $validation = $vc->validation; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$validation->add_failed(title => 'title is invalid'); |
121
|
|
|
|
|
|
|
$validation->add_failed(name => 'name is invalid'); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Is valid |
124
|
|
|
|
|
|
|
my $is_valid = $validation->is_valid; |
125
|
|
|
|
|
|
|
my $title_is_valid = $validation->is_valid('title'); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Get all failed parameter names |
128
|
|
|
|
|
|
|
my $failed = $validation->failed; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Message |
131
|
|
|
|
|
|
|
my $messages = $validation->messages; |
132
|
|
|
|
|
|
|
my $title_message = $validation->message('title'); |
133
|
|
|
|
|
|
|
my $messages_h = $validation->messages_to_hash; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 METHODS |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L inherits all methods from L |
138
|
|
|
|
|
|
|
and implements the following new ones. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 new |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my $validation = Validator::Custom::Validation->new; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Create a L object. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Generally this method is not used. You should use C method of L. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $validation = $vc->validation; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 is_valid |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
my $is_valid = $validation->is_valid; |
153
|
|
|
|
|
|
|
my $is_valid = $validation->is_valid('title'); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Check if the result of validation is valid. |
156
|
|
|
|
|
|
|
If name is specified, check if the parameter corresponding to the name is valid. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 add_failed |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$validation->add_failed('title' => 'title is invalid value'); |
161
|
|
|
|
|
|
|
$validation->add_failed('title'); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Add a failed parameter name and message. |
164
|
|
|
|
|
|
|
If message is omitted, default message is set automatically. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 failed |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $failed = $validation->failed; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Get all failed parameter names. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 message |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $message = $validation->message('title'); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Get a failed message corresponding to the name. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 messages |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
my $messgaes = $validation->messages; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Get all failed messages. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 messages_to_hash |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
my $messages_h = $validation->messages_to_hash; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Get all failed parameter names and messages as hash reference. |