File Coverage

blib/lib/Catmandu/Validator/Simple.pm
Criterion Covered Total %
statement 14 14 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 22 24 91.6


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