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