File Coverage

blib/lib/Catmandu/Fix/validate.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 2 100.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 42 42 100.0


line stmt bran cond sub pod time code
1              
2             use Catmandu::Sane;
3 1     1   90848  
  1         3  
  1         6  
4             our $VERSION = '1.2018';
5              
6             use Moo;
7 1     1   6 use Catmandu::Util qw(require_package);
  1         2  
  1         3  
8 1     1   294 use Catmandu::Util::Path qw(as_path);
  1         2  
  1         42  
9 1     1   382 use namespace::clean;
  1         2  
  1         44  
10 1     1   6 use Catmandu::Fix::Has;
  1         1  
  1         3  
11 1     1   577  
  1         2  
  1         5  
12             has path => (fix_arg => 1);
13             has name => (fix_arg => 1);
14             has error_field => (fix_opt => 1, default => 'errors');
15             has validator_opts => (fix_opt => 'collect');
16             has validator => (is => 'lazy', init_arg => undef);
17              
18             with 'Catmandu::Fix::Builder';
19              
20             my ($self) = @_;
21             require_package($self->name, 'Catmandu::Validator')
22 3     3   25 ->new($self->validator_opts);
23 3         14 }
24              
25             my ($self) = @_;
26              
27             my $validator = $self->validator;
28 3     3   28 my $get = as_path($self->path)->getter;
29             my $set_error = as_path($self->error_field)->creator;
30 3         43  
31 3         19 sub {
32 3         24 my ($data) = @_;
33             my $values = $get->($data);
34             for my $val (@$values) {
35 3     3   6 next if $validator->is_valid($val);
36 3         42 $set_error->($data, $validator->last_errors);
37 3         10 }
38 3 100       11 $data;
39 2         34 }
40             }
41 3         44  
42             1;
43 3         40  
44              
45             =pod
46              
47             =head1 NAME
48              
49             Catmandu::Fix::validate - validate data and keep errors
50              
51             =head1 SYNOPSIS
52              
53             # Check author field against a JSON Schema
54             validate('author', JSONSchema, schema: 'my/schema.json')
55             if exists(errors)
56             ... # do something
57             end
58              
59             # Check item against a custom validator, store in errors in 'warnings'
60             validate('author', MyValidatorClass, error_field: warnings)
61              
62             =head1 DESCRIPTION
63              
64             This L<Catmandu::Fix> validates data with a L<Catmandu::Validator> and stores
65             errors in field C<errors> for further inspection.
66              
67             =head1 CONFIGURATION
68              
69             =over
70              
71             =item error_field
72              
73             Path where to store errors. Set to C<errors> by default.
74              
75             =back
76              
77             Additional options are passed to the validator.
78              
79             =head1 SEE ALSO
80              
81             L<Catmandu::Fix::Condition::valid>
82              
83             =cut