| 1 | package Catmandu::Validator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Catmandu::Util qw(:is); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | requires 'validate_data'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | has 'last_errors' => | |||||||||||||||||||||||||||
| 14 | (is => 'rwp', clearer => '_clear_last_errors', init_arg => undef,); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | has 'after_callback' => (is => 'ro', clearer => 1,); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | has 'error_callback' => (is => 'ro', clearer => 1,); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | has 'error_field' => (is => 'ro', clearer => 1,); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has ['valid_count', 'invalid_count'] => | ||||||||||||||||||||||||||||
| 23 | 16 | ▶ | (is => 'rwp', init_arg => undef, default => sub {0},); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub is_valid { | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if (!is_hash_ref($data)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | Catmandu::BadArg->throw('Cannot validate data of this type'); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | $self->_clear_last_errors; | |||||||||||||||||||||||||||
| 33 | 2 | $self->_set_valid_count(0); | |||||||||||||||||||||||||||
| 34 | 2 | $self->_set_invalid_count(0); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | my $errors = $self->validate_data($data); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if ($errors) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 1 | $self->_set_last_errors($errors); | |||||||||||||||||||||||||||
| 40 | 1 | $self->_set_invalid_count(1); | |||||||||||||||||||||||||||
| 41 | 1 | return 0; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 1 | $self->_set_valid_count(1); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub validate { | ||||||||||||||||||||||||||||
| 51 | 12 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 12 | $self->_set_valid_count(0); | |||||||||||||||||||||||||||
| 54 | 12 | $self->_set_invalid_count(0); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # handle a single record | ||||||||||||||||||||||||||||
| 57 | 12 | ▶ | if (is_hash_ref($data)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 5 | return $self->_process_record($data); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # handle arrayref, returns a new arrayref | ||||||||||||||||||||||||||||
| 62 | 7 | ▶ | if (is_array_ref($data)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 5 | return [grep {defined} map {$self->_process_record($_)} @$data]; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # handle iterators, returns a new iterator | ||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if (is_invocant($data)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | return $data->select(sub {$self->_process_record($_[0])}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | Catmandu::BadArg->throw('Cannot validate data of this type'); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub _process_record { | ||||||||||||||||||||||||||||
| 75 | 24 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_process_record: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 24 | ▶ | my $error_field | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->error_field || 0
MC/DC100%1/1
$self->error_field || 0
$self->error_field
Truth table100%2/2
A
A= $self->error_field
| |||||||||||||||||||||||||||||
| 78 | = ($self->error_field || 0) eq '1' | ||||||||||||||||||||||||||||
| 79 | ? '_validation_errors' | ||||||||||||||||||||||||||||
| 80 | : $self->error_field; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 24 | $self->_clear_last_errors; | |||||||||||||||||||||||||||
| 83 | 24 | my $errors = $self->validate_data($data); | |||||||||||||||||||||||||||
| 84 | 24 | $self->_set_last_errors($errors); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 24 | ▶ | if ($errors) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 14 | $self->_set_invalid_count(1 + $self->invalid_count); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||
| 90 | 10 | $self->_set_valid_count(1 + $self->valid_count); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 24 | ▶ | if ($errors && $error_field) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$errors and $error_field
MC/DC100%2/2
$errors and $error_field
$errors
$error_field
Truth table100%3/3
A and B
A= $errorsB= $error_field
| |||||||||||||||||||||||||||||
| 94 | 4 | $data->{$error_field} = $errors; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 24 | ▶ | if ($self->after_callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 8 | return $self->after_callback->($data, $errors); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 16 | ▶ | if ($errors && $self->error_callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$errors and $self->error_callback
MC/DC100%2/2
$errors and $self->error_callback
$errors
$self->error_callback
Truth table100%3/3
A and B
A= $errorsB= $self->error_callback
| |||||||||||||||||||||||||||||
| 102 | 2 | $self->error_callback->($data, $errors); | |||||||||||||||||||||||||||
| 103 | 2 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 14 | ▶ | return if $errors && !$error_field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$errors and not $error_field
MC/DC100%2/2
$errors and not $error_field
$errors
not $error_field
Truth table100%3/3
A and not(B)
A= $errorsB= not $error_field
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 11 | $data; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =pod | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 NAME | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Catmandu::Validator - Namespace for packages that can validate records in Catmandu. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | use Catmandu::Validator::Simple; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | my $validator = Catmandu::Validator::Simple->new( | ||||||||||||||||||||||||||||
| 126 | handler => sub { | ||||||||||||||||||||||||||||
| 127 | $data = shift; | ||||||||||||||||||||||||||||
| 128 | return "error" unless $data->{title} =~ m/good title/; | ||||||||||||||||||||||||||||
| 129 | return; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | if ( $validator->is_valid($hashref) ) { | ||||||||||||||||||||||||||||
| 134 | save_record_in_database($hashref); | ||||||||||||||||||||||||||||
| 135 | } else { | ||||||||||||||||||||||||||||
| 136 | reject_form($validator->last_errors); | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | my $validator = Catmandu::Validator::Simple->new( | ||||||||||||||||||||||||||||
| 140 | handler => sub { ...}, | ||||||||||||||||||||||||||||
| 141 | error_callback => sub { | ||||||||||||||||||||||||||||
| 142 | my ($data, $errors) = @_; | ||||||||||||||||||||||||||||
| 143 | print "Validation errors for record $data->{_id}:\n"; | ||||||||||||||||||||||||||||
| 144 | print "$_\n" for @{$errors}; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | my $validated_arrayref = $validator->validate($arrayref); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $validator->validate($iterator, { | ||||||||||||||||||||||||||||
| 151 | after_callback => sub { | ||||||||||||||||||||||||||||
| 152 | my ($record, $errors) = @_; | ||||||||||||||||||||||||||||
| 153 | if ($errors) { | ||||||||||||||||||||||||||||
| 154 | add_to_failure_report($rec, $errors); | ||||||||||||||||||||||||||||
| 155 | #omit the invalid record from the result | ||||||||||||||||||||||||||||
| 156 | return undef; | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | return $rec; | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | })->each( sub { | ||||||||||||||||||||||||||||
| 161 | my $record = shift; | ||||||||||||||||||||||||||||
| 162 | publish_record($record); | ||||||||||||||||||||||||||||
| 163 | }); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | A Catmandu::Validator is a base class for Perl packages that can validate data. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 METHODS | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 new() | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Create a new Catmandu::Validator. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 new( after_callback => \&callback ) | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | The after_callback is called after each record has been validated. | ||||||||||||||||||||||||||||
| 178 | The callback function should take $hashref to each data record, and $arrayref to list of validation errors | ||||||||||||||||||||||||||||
| 179 | for the record as arguments. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 new( error_field => $field_name ) | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | If the error_field parameter is set, then during validation each record that | ||||||||||||||||||||||||||||
| 184 | fails validation will get an extra field added containing an | ||||||||||||||||||||||||||||
| 185 | arrayref to the validation errors. The name of the key will be the | ||||||||||||||||||||||||||||
| 186 | value passed or '_validation_errors' if 1 is passed. By default it is not set. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head2 is_valid( \%hash ) | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Validates a single record. Returns 1 success and 0 on failure. Information about the validation errors | ||||||||||||||||||||||||||||
| 191 | can be retrieved with the L</"last_errors()"> method. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 validate( \%hash ) | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 validate( $iterator ) | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 validate( \@array ) | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Validates a single record or multiple records in an iterator or an array. Returns validated records in the same type of | ||||||||||||||||||||||||||||
| 200 | container for multiple records or the record itself for a single record. The default behaviour is to return the records that passed validation unchanged and omit the invalid records. | ||||||||||||||||||||||||||||
| 201 | This behaviour can be changed by setting the I<after_callback> or the I<error_field> in the constructor. Returns undef on validation failure for single records. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 last_errors() | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Returns arrayref of errors from the record that was last validated if that record failed validation | ||||||||||||||||||||||||||||
| 206 | or undef if there were no errors. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head2 valid_count() | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Returns the number of valid records from last validate operation. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head2 invalid_count() | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Returns the number of invalid records from the last validate operation. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | L<Catmandu::Validator::Simple>, L<Catmandu::Iterable> | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||