line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Verifier::Nested; |
2
|
|
|
|
|
|
|
$Data::Verifier::Nested::VERSION = '0.63'; |
3
|
1
|
|
|
1
|
|
66975
|
use Moose; |
|
1
|
|
|
|
|
466136
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: Nested profile based data verification with Moose type constraints. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
extends 'Data::Verifier'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
## private helper functions |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $_is_profile_spec = sub { |
13
|
|
|
|
|
|
|
my $spec = shift; |
14
|
|
|
|
|
|
|
return 0 unless ref $spec eq 'HASH'; |
15
|
|
|
|
|
|
|
my @keys = grep { ref $spec->{ $_ } eq 'HASH' } keys %$spec; |
16
|
|
|
|
|
|
|
($_ eq 'dependent') && return 1 foreach @keys; |
17
|
|
|
|
|
|
|
return 0 if @keys; |
18
|
|
|
|
|
|
|
return 1; |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $_collapse_profile; |
22
|
|
|
|
|
|
|
$_collapse_profile = sub { |
23
|
|
|
|
|
|
|
my ($profile, $acc, $prefix) = @_; |
24
|
|
|
|
|
|
|
foreach my $k ( keys %$profile ) { |
25
|
|
|
|
|
|
|
my $full_k = join '.' => ($prefix || (), $k); |
26
|
|
|
|
|
|
|
if ( $_is_profile_spec->( $profile->{ $k } ) ) { |
27
|
|
|
|
|
|
|
$acc->{ $full_k } = $profile->{ $k } |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
else { |
30
|
|
|
|
|
|
|
(ref $profile->{ $k } eq 'HASH') |
31
|
|
|
|
|
|
|
|| die "Can only collapse HASH refs"; |
32
|
|
|
|
|
|
|
$_collapse_profile->( $profile->{ $k }, $acc, $full_k ); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $collapse_profile = sub { |
38
|
|
|
|
|
|
|
my ($profile) = @_; |
39
|
|
|
|
|
|
|
my $acc = {}; |
40
|
|
|
|
|
|
|
$_collapse_profile->( $profile, $acc ); |
41
|
|
|
|
|
|
|
$acc; |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $_collapse_data_for_profile; |
45
|
|
|
|
|
|
|
$_collapse_data_for_profile = sub { |
46
|
|
|
|
|
|
|
my ($profile, $data, $acc, $prefix) = @_; |
47
|
|
|
|
|
|
|
foreach my $k ( keys %$data ) { |
48
|
|
|
|
|
|
|
my $full_k = join '.' => ($prefix || (), $k); |
49
|
|
|
|
|
|
|
if ( exists $profile->{ $full_k } ) { |
50
|
|
|
|
|
|
|
$acc->{ $full_k } = $data->{ $k } |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
|
|
|
|
|
|
(ref $data->{ $k } eq 'HASH') |
54
|
|
|
|
|
|
|
|| die "Can only collapse HASH refs"; |
55
|
|
|
|
|
|
|
$_collapse_data_for_profile->( $profile, $data->{ $k }, $acc, $full_k ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $collapse_data_for_profile = sub { |
61
|
|
|
|
|
|
|
my ($profile, $data) = @_; |
62
|
|
|
|
|
|
|
my $acc = {}; |
63
|
|
|
|
|
|
|
$_collapse_data_for_profile->( $profile, $data, $acc ); |
64
|
|
|
|
|
|
|
$acc; |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# now to the subclass ... |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub BUILDARGS { |
70
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
71
|
1
|
|
|
|
|
16
|
my $params = $self->SUPER::BUILDARGS( @_ ); |
72
|
|
|
|
|
|
|
# NOTE: |
73
|
|
|
|
|
|
|
# this is required, but we don't want to |
74
|
|
|
|
|
|
|
# make assumptions, if it is not here, then |
75
|
|
|
|
|
|
|
# it will die later on with the right error |
76
|
|
|
|
|
|
|
# so we can just process it if we have it. |
77
|
|
|
|
|
|
|
# - SL |
78
|
|
|
|
|
|
|
$params->{'profile'} = $collapse_profile->( $params->{'profile'} ) |
79
|
1
|
50
|
|
|
|
18
|
if exists $params->{'profile'}; |
80
|
1
|
|
|
|
|
34
|
$params; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub verify { |
84
|
2
|
|
|
2
|
1
|
29
|
my ($self, $params, $members) = @_; |
85
|
2
|
|
|
|
|
83
|
$self->SUPER::verify( |
86
|
|
|
|
|
|
|
$collapse_data_for_profile->( |
87
|
|
|
|
|
|
|
$self->profile, |
88
|
|
|
|
|
|
|
$params |
89
|
|
|
|
|
|
|
), |
90
|
|
|
|
|
|
|
$members |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=pod |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 NAME |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Data::Verifier::Nested - Nested profile based data verification with Moose type constraints. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 VERSION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
version 0.63 |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SYNOPSIS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
use Data::Verifier::Nested; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my $dv = Data::Verifier::Nested->new( |
116
|
|
|
|
|
|
|
filters => [ qw(trim) ], |
117
|
|
|
|
|
|
|
profile => { |
118
|
|
|
|
|
|
|
name => { |
119
|
|
|
|
|
|
|
first_name => { type => 'Str', required => 1 }, |
120
|
|
|
|
|
|
|
last_name => { type => 'Str', required => 1 }, |
121
|
|
|
|
|
|
|
}, |
122
|
|
|
|
|
|
|
age => { type => 'Int' }, |
123
|
|
|
|
|
|
|
sign => { required => 1 }, |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Pass in a hash of data |
128
|
|
|
|
|
|
|
my $results = $dv->verify({ |
129
|
|
|
|
|
|
|
name => { first_name => 'Cory', last_name => 'Watson' }, age => 'foobar' |
130
|
|
|
|
|
|
|
}); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$results->success; # no |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
$results->is_invalid('name.first_name'); # no |
135
|
|
|
|
|
|
|
$results->is_invalid('name.last_name'); # no |
136
|
|
|
|
|
|
|
$results->is_invalid('age'); # yes |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$results->is_missing('name.first_name'); # no |
139
|
|
|
|
|
|
|
$results->is_invalid('name.last_name'); # no |
140
|
|
|
|
|
|
|
$results->is_missing('sign'); # yes |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$results->get_original_value('name.first_name'); # Unchanged, original value |
143
|
|
|
|
|
|
|
$results->get_value('name.first_name'); # Filtered, valid value |
144
|
|
|
|
|
|
|
$results->get_value('age'); # undefined, as it's invalid |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Data::Verifier allows you verify data that is in a flat hash, but sometimes |
149
|
|
|
|
|
|
|
this is not enough, this is where Data::Verifier::Nested comes in. It is a |
150
|
|
|
|
|
|
|
subclass of Data::Verifier that can work with nested data structures. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Stevan Little |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 AUTHOR |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Cory G Watson <gphat@cpan.org> |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This software is copyright (c) 2019 by Cold Hard Code, LLC. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
165
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |