line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::Role::Validatable; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
11003
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
22
|
use 5.008_005; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
66
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
11
|
use Moo::Role; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
8
|
1
|
|
|
1
|
|
898
|
use MooX::Role::Validatable::Error; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
9
|
1
|
|
|
1
|
|
6
|
use Types::Standard qw( Str Int Bool ArrayRef ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
564
|
use Carp qw(confess); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
12
|
1
|
|
|
1
|
|
4
|
use Scalar::Util qw/blessed/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
553
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has '_init_errors' => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
isa => ArrayRef, |
17
|
|
|
|
|
|
|
init_arg => undef, |
18
|
|
|
|
|
|
|
default => sub { return [] }, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
has '_validation_errors' => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
isa => ArrayRef, |
23
|
|
|
|
|
|
|
init_arg => undef, |
24
|
|
|
|
|
|
|
default => sub { return [] }, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has 'error_class' => (is => 'ro', default => sub { 'MooX::Role::Validatable::Error' }, trigger => sub { |
28
|
|
|
|
|
|
|
my $self = shift; my $error_class = $self->error_class; |
29
|
|
|
|
|
|
|
eval "require $error_class;"; |
30
|
|
|
|
|
|
|
confess $@ if $@; |
31
|
|
|
|
|
|
|
} ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has validation_methods => ( |
34
|
|
|
|
|
|
|
is => 'lazy', |
35
|
|
|
|
|
|
|
isa => ArrayRef[Str] |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _build_validation_methods { |
39
|
1
|
|
|
1
|
|
514
|
my $self = shift; |
40
|
1
|
|
|
|
|
10
|
return [grep { $_ =~ /^_validate_/ } ($self->meta->get_all_method_names)]; |
|
0
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub all_errors { |
44
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
45
|
0
|
|
|
|
|
|
return (@{$self->{_init_errors}}, @{$self->{_validation_errors}}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub all_init_errors { |
49
|
0
|
|
|
0
|
1
|
|
return @{(shift)->{_init_errors}}; |
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub all_validation_errors { |
53
|
0
|
|
|
0
|
1
|
|
return @{(shift)->{_validation_errors}}; |
|
0
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub passes_validation { |
57
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
58
|
0
|
|
|
|
|
|
my @all_errors = $self->all_errors; |
59
|
0
|
0
|
|
|
|
|
return (scalar @all_errors) ? 0 : 1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub should_alert { |
63
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
64
|
0
|
0
|
|
|
|
|
return (grep { $_->alert } ($self->all_errors)) ? 1 : 0; |
|
0
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub confirm_validity { |
68
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
69
|
0
|
|
|
|
|
|
$self->{_validation_errors} = [ |
70
|
0
|
|
|
|
|
|
map { $self->_errfilter($_) } |
71
|
0
|
|
|
|
|
|
map { $self->$_ } @{$self->validation_methods} |
|
0
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
]; |
73
|
0
|
|
|
|
|
|
return $self->passes_validation; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub add_errors { |
77
|
0
|
|
|
0
|
1
|
|
my ($self, @errors) = @_; |
78
|
0
|
|
|
|
|
|
push @{ $self->{_init_errors} }, map { $self->_errfilter($_) } @errors; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
return scalar @errors; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub initialized_correctly { |
83
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
84
|
0
|
0
|
|
|
|
|
return (@{$self->{_init_errors}}) ? 0 : 1; |
|
0
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub all_errors_by_severity { |
88
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
89
|
0
|
|
|
|
|
|
return (sort { $b->severity <=> $a->severity } ($self->all_errors)); |
|
0
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub primary_validation_error { |
93
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my @errors = $self->all_errors_by_severity; |
96
|
0
|
0
|
|
|
|
|
return unless @errors; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# We may wish to do something with perm v. transient here at some point. |
99
|
0
|
|
|
|
|
|
return $errors[0]; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _errfilter { |
103
|
0
|
|
|
0
|
|
|
my ($self, $error) = @_; |
104
|
0
|
0
|
|
|
|
|
return $error if blessed($error); |
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
$error = { message => $error } unless ref($error); # when it's a string |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
confess "Cannot add validation error which is not blessed nor hashref" unless ref($error) eq 'HASH'; |
109
|
0
|
0
|
|
|
|
|
$error->{message_to_client} = $error->{message} unless exists $error->{message_to_client}; |
110
|
0
|
0
|
|
|
|
|
$error->{set_by} = caller(1) unless exists $error->{set_by}; |
111
|
0
|
|
|
|
|
|
return $self->error_class->new($error); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
1
|
|
5
|
no Moo::Role; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |
117
|
|
|
|
|
|
|
__END__ |