line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Validator::Simple; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
106407
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
25
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
27
|
use Moo; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
19
|
|
8
|
3
|
|
|
3
|
|
1276
|
use namespace::clean; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
35
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Catmandu::Validator'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has handler => ( |
13
|
|
|
|
|
|
|
is => 'rw', |
14
|
|
|
|
|
|
|
required => 1, |
15
|
|
|
|
|
|
|
isa => sub { |
16
|
|
|
|
|
|
|
Catmandu::BadArg->throw("handler should be a CODE reference") |
17
|
|
|
|
|
|
|
unless ref $_[0] eq 'CODE'; |
18
|
|
|
|
|
|
|
}, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub validate_data { |
22
|
7
|
|
|
7
|
0
|
17
|
my ($self, $data) = @_; |
23
|
|
|
|
|
|
|
|
24
|
7
|
|
|
|
|
11
|
my $error_messages = &{$self->handler}($data); |
|
7
|
|
|
|
|
120
|
|
25
|
7
|
100
|
66
|
|
|
116
|
$error_messages = [$error_messages] |
26
|
|
|
|
|
|
|
unless !$error_messages || ref $error_messages eq 'ARRAY'; |
27
|
7
|
|
|
|
|
22
|
return $error_messages; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
1; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
__END__ |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=pod |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 NAME |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Catmandu::Validator::Simple - Simple Validator for Catmandu |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 SYNOPSIS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
use Catmandu::Validator::Simple; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $validator = Catmandu::Validator::Simple->new( |
45
|
|
|
|
|
|
|
handler => sub { |
46
|
|
|
|
|
|
|
$data = shift; |
47
|
|
|
|
|
|
|
return "error" unless $data->{title} =~ m/good title/; |
48
|
|
|
|
|
|
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if ( $validator->is_valid($hashref) ) { |
53
|
|
|
|
|
|
|
save_record_in_database($hashref); |
54
|
|
|
|
|
|
|
} else { |
55
|
|
|
|
|
|
|
reject_form($validator->last_errors); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 DESCRIPTION |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Catmandu::Validator::Simple can be used for doing simple data validation in |
62
|
|
|
|
|
|
|
Catmandu. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 CONFIGURATION |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=over |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item handler |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
A function that takes a hash reference item as argument. Should return undef if |
71
|
|
|
|
|
|
|
the record passes validation otherwise return an error or an arrayref of |
72
|
|
|
|
|
|
|
errors. Each error can be either a simple message string or a hashref to a |
73
|
|
|
|
|
|
|
more detailed error information. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=back |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 SEE ALSO |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
See L<Catmandu::Validator> for inherited methods, common configuration options, |
80
|
|
|
|
|
|
|
and usage. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |