line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Validator::Custom::Validation; |
2
|
5
|
|
|
5
|
|
24
|
use Object::Simple -base; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
69
|
|
3
|
|
|
|
|
|
|
|
4
|
5
|
|
|
5
|
|
448
|
use Carp 'croak'; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
3471
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new { |
7
|
18
|
|
|
18
|
1
|
73
|
my $self = shift->SUPER::new(@_); |
8
|
|
|
|
|
|
|
|
9
|
18
|
|
|
|
|
131
|
$self->{_failed_infos} = {}; |
10
|
|
|
|
|
|
|
|
11
|
18
|
|
|
|
|
47
|
return $self; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub is_valid { |
15
|
14
|
|
|
14
|
1
|
47
|
my ($self, $name) = @_; |
16
|
|
|
|
|
|
|
|
17
|
14
|
100
|
|
|
|
36
|
if (defined $name) { |
18
|
9
|
100
|
|
|
|
65
|
return exists $self->{_failed_infos}->{$name} ? 0 : 1; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
else { |
21
|
5
|
100
|
|
|
|
8
|
return !(keys %{$self->{_failed_infos}}) ? 1 : 0; |
|
5
|
|
|
|
|
41
|
|
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub add_failed { |
26
|
32
|
|
|
32
|
1
|
146
|
my ($self, $name, $message) = @_; |
27
|
|
|
|
|
|
|
|
28
|
32
|
|
|
|
|
53
|
my $failed_infos = $self->{_failed_infos}; |
29
|
|
|
|
|
|
|
|
30
|
32
|
50
|
|
|
|
82
|
if ($failed_infos->{$name}) { |
31
|
0
|
|
|
|
|
0
|
croak "\"$name\" is already exists"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
32
|
|
|
|
|
84
|
my @failed_names = keys %$failed_infos; |
35
|
32
|
|
|
|
|
42
|
my $pos; |
36
|
32
|
100
|
|
|
|
68
|
if (@failed_names) { |
37
|
20
|
|
|
|
|
29
|
my $max_pos = 0; |
38
|
20
|
|
|
|
|
75
|
for my $failed_name (@failed_names) { |
39
|
47
|
|
|
|
|
68
|
my $pos = $failed_infos->{$failed_name}{pos}; |
40
|
47
|
100
|
|
|
|
107
|
if ($pos > $max_pos) { |
41
|
18
|
|
|
|
|
29
|
$max_pos = $pos; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
20
|
|
|
|
|
32
|
$pos = $max_pos + 1; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
12
|
|
|
|
|
19
|
$pos = 0; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
32
|
|
|
|
|
92
|
$failed_infos->{$name}{pos} = $pos; |
51
|
|
|
|
|
|
|
|
52
|
32
|
100
|
|
|
|
75
|
unless (defined $message) { |
53
|
28
|
|
|
|
|
52
|
$message = "$name is invalid"; |
54
|
|
|
|
|
|
|
} |
55
|
32
|
|
|
|
|
62
|
$failed_infos->{$name}{message} = $message; |
56
|
32
|
|
|
|
|
53
|
$failed_infos->{$name}{pos} = $pos; |
57
|
|
|
|
|
|
|
|
58
|
32
|
|
|
|
|
89
|
return $self; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub failed { |
62
|
17
|
|
|
17
|
1
|
46
|
my $self = shift; |
63
|
|
|
|
|
|
|
|
64
|
17
|
|
|
|
|
30
|
my $failed_infos = $self->{_failed_infos}; |
65
|
17
|
|
|
|
|
69
|
my @failed = sort { $failed_infos->{$a}{pos} <=> |
66
|
41
|
|
|
|
|
92
|
$failed_infos->{$b}{pos} } keys %$failed_infos; |
67
|
|
|
|
|
|
|
|
68
|
17
|
|
|
|
|
125
|
return \@failed; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub message { |
72
|
8
|
|
|
8
|
1
|
20
|
my ($self, $name) = @_; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Parameter name not specified |
75
|
8
|
50
|
|
|
|
26
|
croak 'Parameter name must be specified' |
76
|
|
|
|
|
|
|
unless $name; |
77
|
|
|
|
|
|
|
|
78
|
8
|
|
|
|
|
46
|
return $self->{_failed_infos}{$name}{message}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub messages { |
82
|
5
|
|
|
5
|
1
|
9
|
my $self = shift; |
83
|
|
|
|
|
|
|
|
84
|
5
|
|
|
|
|
12
|
my $failed_infos = $self->{_failed_infos}; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Messages |
87
|
5
|
|
|
|
|
11
|
my $messages = []; |
88
|
5
|
|
|
|
|
8
|
for my $name (@{$self->failed}) { |
|
5
|
|
|
|
|
14
|
|
89
|
8
|
|
|
|
|
15
|
my $message = $failed_infos->{$name}{message}; |
90
|
8
|
|
|
|
|
21
|
push @$messages, $message; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
5
|
|
|
|
|
35
|
return $messages; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub messages_to_hash { |
97
|
5
|
|
|
5
|
1
|
11
|
my $self = shift; |
98
|
|
|
|
|
|
|
|
99
|
5
|
|
|
|
|
12
|
my $failed_infos = $self->{_failed_infos}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Name and message hash |
102
|
5
|
|
|
|
|
23
|
my $messages = {}; |
103
|
5
|
|
|
|
|
20
|
for my $name (keys %$failed_infos) { |
104
|
8
|
|
|
|
|
26
|
$messages->{$name} = $failed_infos->{$name}{message}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
5
|
|
|
|
|
30
|
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
|
|
|
|
|
|
|
# Failed key 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
|
|
|
|
|
|
|
my $validation = $vc->validation; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 is_valid |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $is_valid = $validation->is_valid; |
152
|
|
|
|
|
|
|
my $is_valid = $validation->is_valid('title'); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Check if the result of validation is valid. |
155
|
|
|
|
|
|
|
If name is specified, check if the parameter corresponding to the name is valid. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 add_failed |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$validation->add_failed('title' => 'title is invalid value'); |
160
|
|
|
|
|
|
|
$validation->add_failed('title'); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Add a failed name and message. |
163
|
|
|
|
|
|
|
If message is omitted, default message is set automatically. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 failed |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $failed = $validation->failed; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Get all failed names. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 message |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
my $message = $validation->message('title'); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Get a failed message corresponding to the name. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 messages |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my $messgaes = $validation->messages; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Get all failed messages. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 messages_to_hash |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $messages_h = $validation->messages_to_hash; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Get all failed names and messages as hash reference. |