| 1 | package Catmandu::Validator::Simple; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | with 'Catmandu::Validator'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | has handler => ( | ||||||||||||||||||||||||||||
| 13 | is => 'rw', | ||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||
| 15 | isa => sub { | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | Catmandu::BadArg->throw("handler should be a CODE reference") | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | unless ref $_[0] eq 'CODE'; | ||||||||||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||||||||||
| 19 | 1 | ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub validate_data { | ||||||||||||||||||||||||||||
| 22 | 2 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_data: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | my $error_messages = &{$self->handler}($data); | |||||||||||||||||||||||||||
| 25 | 2 | ▶ | $error_messages = [$error_messages] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $error_messages or ref $error_messages eq "ARRAY"
MC/DC50%1/2
not $error_messages or ref $error_messages eq "ARRAY"
not $error_messages
ref $error_messages eq "ARRAY"
Truth table66%2/3
not(A) or B
A= not $error_messagesB= ref $error_messages eq "ARRAY"
| |||||||||||||||||||||||||||||
| 26 | unless !$error_messages || ref $error_messages eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 27 | 2 | return $error_messages; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | 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 METHODS | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head2 new(handler => \&callback, %options) | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | The I<callback> function should take $hashref to a data record as argument. | ||||||||||||||||||||||||||||
| 69 | Should return undef if the record passes validation otherwise return an error | ||||||||||||||||||||||||||||
| 70 | or an arrayref of errors. Each error can be either a simple message string or | ||||||||||||||||||||||||||||
| 71 | a hashref to a more detailed error information. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | The constructor also accepts the common options for L<Catmandu::Validator>. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 is_valid(...) | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 validate(...) | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 last_errors(...) | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 valid_count() | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 invalid_count() | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | These are methods are inherited from L<Catmandu::Validator>. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | L<Catmandu::Validator> | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||