File Coverage

blib/lib/Data/DPath/Validator.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::DPath::Validator;
2             our $VERSION = '0.093411';
3              
4             #ABSTRACT: Validate data based on template data
5              
6 2     2   26518 use Moose;
  0            
  0            
7             use Data::DPath 'dpath';
8             use Data::DPath::Validator::Visitor;
9             use namespace::autoclean;
10              
11              
12             with 'MooseX::Role::BuildInstanceOf' =>
13             {
14             target => 'Data::DPath::Validator::Visitor',
15             prefix => 'visitor'
16             };
17              
18              
19             has '+visitor' =>
20             (
21             handles =>
22             {
23             'load' => 'visit',
24             'templates' => 'templates'
25             }
26             );
27              
28              
29             has 'strict_mode' =>
30             (
31             is => 'ro',
32             isa => 'Bool',
33             default => 0,
34             );
35              
36              
37             sub validate
38             {
39             my $self = shift;
40            
41             my $ret = [];
42             foreach my $data (@_)
43             {
44             my $pass = $self->strict_mode
45             ? $self->_strict_validation($data)
46             : $self->_loose_validation($data);
47            
48             push(@$ret, $pass);
49             }
50              
51             return $ret;
52             }
53              
54             sub _strict_validation
55             {
56             my ($self, $data) = @_;
57            
58             foreach my $template (@{$self->templates})
59             {
60             if(!dpath($template)->match($data))
61             {
62             return 0;
63             }
64             }
65              
66             return 1;
67             }
68              
69             sub _loose_validation
70             {
71             my ($self, $data) = @_;
72            
73             foreach my $template (@{$self->templates})
74             {
75             if(dpath($template)->match($data))
76             {
77             return 1;
78             }
79             }
80              
81             return 0;
82             }
83              
84             __PACKAGE__->meta->make_immutable();
85             1;
86              
87              
88              
89             =pod
90              
91             =head1 NAME
92              
93             Data::DPath::Validator - Validate data based on template data
94              
95             =head1 VERSION
96              
97             version 0.093411
98              
99             =head1 SYNOPSIS
100              
101             use strict;
102             use warnings;
103             use Data::DPath::Validator;
104              
105             my $template = { foo => '*' };
106             my $data = [{ foo => [1,2,3] }, { foo => { bar => 'mtfnpy' } } ];
107              
108             my $v = Data::DPath::Validator->new();
109             $v->load($template);
110              
111             my $ret = $v->validate(@$data);
112              
113             if($ret->[0] && $ret->[1])
114             {
115             print "Hooray!\n";
116             }
117              
118             =head1 DESCRIPTION
119              
120             Data::DPath::Validator is a simple data validator using normal Perl data
121             structures as templates. It accomplishes this by translating your template into
122             a series Data::DPath paths (one for each "branch") using Data::Visitor to
123             traverse the data structure like a SAX parser. Then when calling validate(),
124             each path is then attempted against the supplied data.
125              
126             =head1 NOTES
127              
128             A template is defined by using a normal Perl data structure like in the
129             synopsis, with the parts where 'any' data is acceptable being replaced with an
130             asterisk ('*').
131              
132             By default, the validator is in loose validation mode, meaning as long as one
133             path matches, the data structure passes. To instead require strict validation
134             do this:
135              
136             my $v = Data::DPath::Validator->new(strict_mode => 1);
137              
138             =cut
139              
140             =pod
141              
142             =head1 ATTRIBUTES
143              
144             =head2 visitor
145              
146             This contains our Data::DPath::Validator::Visitor instance constructed via
147             MooseX::Role::BuildInstanceOf.
148              
149             It handles the following methods:
150              
151             =over 4
152              
153             =item load -> visit
154              
155             load() takes any number of data structures and visit()s via Data::Visitor to
156             generate Data::DPath paths, storing a different path for each "branch" in each
157             data structure.
158              
159             =item templates
160              
161             templates() is the accessor from the Visitor to return an ArrayRef[Str]
162             containing all of the parsed templates.
163              
164             =back
165              
166             =cut
167              
168             =pod
169              
170             =head2 strict_mode is: ro, isa: Bool, default: 0
171              
172             strict_mode determines how strict the validation is. By default, only a single
173             template path needs to match for the data structure to be okay. With
174             strict_mode on, all paths must pass on the supplied data or it fails
175              
176             =cut
177              
178             =pod
179              
180             =head1 METHODS
181              
182             =head2 validate
183              
184             Validate takes any number of data structures and verifies that each matches at
185             least one of the DPaths generated from the template data structures.
186              
187             Returns ArrayRef[Bool] which each indice corresponding to order in which the
188             data was supplied.
189              
190             =head1 AUTHOR
191              
192             Nicholas Perez <nperez@cpan.org>
193              
194             =head1 COPYRIGHT AND LICENSE
195              
196             This software is copyright (c) 2009 by Infinity Interactive.
197              
198             This is free software; you can redistribute it and/or modify it under
199             the same terms as the Perl 5 programming language system itself.
200              
201             =cut
202              
203              
204             __END__