line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use Moose::Role; |
3
|
1
|
|
|
1
|
|
925
|
use MooseX::Role::Validatable::Error; |
|
1
|
|
|
|
|
4242
|
|
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
5072
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
49
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Class::Load qw/load_class/; |
8
|
1
|
|
|
1
|
|
7
|
use Carp qw(confess); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
52
|
|
9
|
1
|
|
|
1
|
|
6
|
use Scalar::Util qw/blessed/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
10
|
1
|
|
|
1
|
|
7
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
733
|
|
11
|
|
|
|
|
|
|
has '_init_errors' => ( |
12
|
|
|
|
|
|
|
is => 'ro', |
13
|
|
|
|
|
|
|
isa => 'ArrayRef', |
14
|
|
|
|
|
|
|
init_arg => undef, |
15
|
|
|
|
|
|
|
default => sub { return [] }, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
has '_validation_errors' => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
isa => 'ArrayRef', |
20
|
|
|
|
|
|
|
init_arg => undef, |
21
|
|
|
|
|
|
|
default => sub { return [] }, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'error_class' => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
default => sub { 'MooseX::Role::Validatable::Error' }, |
27
|
|
|
|
|
|
|
trigger => sub { |
28
|
|
|
|
|
|
|
my $self = shift; |
29
|
|
|
|
|
|
|
load_class($self->error_class); |
30
|
|
|
|
|
|
|
}); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has validation_methods => ( |
33
|
|
|
|
|
|
|
is => 'ro', |
34
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
35
|
|
|
|
|
|
|
lazy_build => 1, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $self = shift; |
39
|
|
|
|
|
|
|
return [grep { $_ =~ /^_validate_/ } ($self->meta->get_all_method_names)]; |
40
|
1
|
|
|
1
|
|
2
|
} |
41
|
1
|
|
|
|
|
3
|
|
|
37
|
|
|
|
|
1844
|
|
42
|
|
|
|
|
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
return (@{$self->{_init_errors}}, @{$self->{_validation_errors}}); |
44
|
|
|
|
|
|
|
} |
45
|
3
|
|
|
3
|
1
|
5
|
|
46
|
3
|
|
|
|
|
3
|
return @{(shift)->{_init_errors}}; |
|
3
|
|
|
|
|
20
|
|
|
3
|
|
|
|
|
10
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
return @{(shift)->{_validation_errors}}; |
50
|
1
|
|
|
1
|
1
|
2
|
} |
|
1
|
|
|
|
|
36
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
my @all_errors = $self->all_errors; |
54
|
0
|
|
|
0
|
1
|
0
|
return (scalar @all_errors) ? 0 : 1; |
|
0
|
|
|
|
|
0
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
2
|
|
|
2
|
1
|
5
|
return (grep { $_->alert } ($self->all_errors)) ? 1 : 0; |
59
|
2
|
|
|
|
|
8
|
} |
60
|
2
|
50
|
|
|
|
16
|
|
61
|
|
|
|
|
|
|
my $self = shift; |
62
|
|
|
|
|
|
|
$self->{_validation_errors} = [ |
63
|
|
|
|
|
|
|
map { $self->_errfilter($_) } |
64
|
0
|
|
|
0
|
1
|
0
|
map { $self->$_ } @{$self->validation_methods}]; |
65
|
0
|
0
|
|
|
|
0
|
return $self->passes_validation; |
|
0
|
|
|
|
|
0
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my ($self, @errors) = @_; |
69
|
1
|
|
|
1
|
1
|
3
|
push @{$self->{_init_errors}}, map { $self->_errfilter($_) } @errors; |
70
|
|
|
|
|
|
|
return scalar @errors; |
71
|
2
|
|
|
|
|
8
|
} |
72
|
1
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
31
|
|
73
|
1
|
|
|
|
|
26
|
my $self = shift; |
74
|
|
|
|
|
|
|
return (@{$self->{_init_errors}}) ? 0 : 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
1
|
1
|
63
|
my $self = shift; |
78
|
1
|
|
|
|
|
2
|
return (sort { $b->severity <=> $a->severity } ($self->all_errors)); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
79
|
1
|
|
|
|
|
14
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $self = shift; |
82
|
|
|
|
|
|
|
|
83
|
2
|
|
|
2
|
1
|
676
|
my @errors = $self->all_errors_by_severity; |
84
|
2
|
100
|
|
|
|
3
|
return unless @errors; |
|
2
|
|
|
|
|
9
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# We may wish to do something with perm v. transient here at some point. |
87
|
|
|
|
|
|
|
return $errors[0]; |
88
|
0
|
|
|
0
|
1
|
0
|
} |
89
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
90
|
|
|
|
|
|
|
my ($self, $error) = @_; |
91
|
|
|
|
|
|
|
return $error if blessed($error); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
0
|
1
|
0
|
$error = {message => $error} unless ref($error); # when it's a string |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
0
|
confess "Cannot add validation error which is not blessed nor hashref" unless ref($error) eq 'HASH'; |
96
|
0
|
0
|
|
|
|
0
|
$error->{message_to_client} = $error->{message} unless exists $error->{message_to_client}; |
97
|
|
|
|
|
|
|
$error->{set_by} = caller(1) unless exists $error->{set_by}; |
98
|
|
|
|
|
|
|
return $self->error_class->new($error); |
99
|
0
|
|
|
|
|
0
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
no Moose::Role; |
102
|
|
|
|
|
|
|
|
103
|
3
|
|
|
3
|
|
7
|
1; |
104
|
3
|
50
|
|
|
|
8
|
|
105
|
|
|
|
|
|
|
=encoding utf-8 |
106
|
3
|
100
|
|
|
|
8
|
|
107
|
|
|
|
|
|
|
=head1 NAME |
108
|
3
|
50
|
|
|
|
6
|
|
109
|
3
|
100
|
|
|
|
23
|
MooseX::Role::Validatable - Role to add validation to a class |
110
|
3
|
50
|
|
|
|
11
|
|
111
|
3
|
|
|
|
|
81
|
=head1 SYNOPSIS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
package MyClass; |
114
|
1
|
|
|
1
|
|
7
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
115
|
|
|
|
|
|
|
use Moose; |
116
|
|
|
|
|
|
|
with 'MooseX::Role::Validatable'; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
has 'attr1' => (is => 'ro', lazy_build => 1); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _build_attr1 { |
121
|
|
|
|
|
|
|
my $self = shift; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Note initialization errors |
124
|
|
|
|
|
|
|
$self->add_errors( { |
125
|
|
|
|
|
|
|
message => 'Error: blabla', |
126
|
|
|
|
|
|
|
message_to_client => 'Something is wrong!' |
127
|
|
|
|
|
|
|
} ) if 'blabla'; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub _validate_some_other_errors { # _validate_* |
131
|
|
|
|
|
|
|
my $self = shift; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my @errors; |
134
|
|
|
|
|
|
|
push @errors, { |
135
|
|
|
|
|
|
|
message => '...', |
136
|
|
|
|
|
|
|
message_to_client => '...', |
137
|
|
|
|
|
|
|
}; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
return @errors; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
## use |
143
|
|
|
|
|
|
|
my $ex = MyClass->new(); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
if (not $ex->initialized_correctly) { |
146
|
|
|
|
|
|
|
my @errors = $ex->all_init_errors(); |
147
|
|
|
|
|
|
|
...; # We didn't even start with good data. |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
if (not $ex->confirm_validity) { # does not pass those _validate_* |
151
|
|
|
|
|
|
|
my @errors = $ex->all_errors(); |
152
|
|
|
|
|
|
|
...; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 DESCRIPTION |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
MooseX::Role::Validatable is a Moo/Moose role which provides a standard way to add validation to a class. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 METHODS |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 initialized_correctly |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
no error when init the object (no add_errors is called) |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 add_errors |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$self->add_errors(...) |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
add errors on those lazy attributes or sub BUILD |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 confirm_validity |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
run all those B<_validate_*> messages and returns true if no error found. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 all_errors |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
An array of the errors currently noted. combined with B<all_init_errors> and B<all_validation_errors> |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
all errors including below methods are instance of error_class, default to L<MooseX::Role::Validatable::Error> |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 all_init_errors |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
all errors on init |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head2 all_validation_errors |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
all errors on validation |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 all_errors_by_severity |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
order by severity |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 primary_validation_error |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
the first error of B<all_errors_by_severity> |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 validation_methods |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
A list of all validation methods available on this object. |
200
|
|
|
|
|
|
|
This can be auto-generated from all methods which begin with |
201
|
|
|
|
|
|
|
"_validate_" which is especially helpful in devleoping new validations. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
You may wish to set this list directly on the object, if |
204
|
|
|
|
|
|
|
you create and validate a lot of static objects. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 error_class |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
default to L<MooseX::Role::Validatable::Error>, override by |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
has '+error_class' => (is => 'ro', default => sub { 'My::Validatable::Error' }); |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
# or |
213
|
|
|
|
|
|
|
->new(error_class => 'My::Validatable::Error'); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 passes_validation |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 should_alert |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head1 AUTHOR |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Binary.com E<lt>fayland@binary.comE<gt> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 COPYRIGHT |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Copyright 2014- Binary.com |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 LICENSE |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
230
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 SEE ALSO |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=cut |