Validator.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 94.0% 47 / 50 CC 7 SCAR 8.8
CC
7
Cov
93%
_strict_validation
3.1
_loose_validation
3.0
validate
3.0
CRAP 2.4
« Validator/Visitor.pm ↑ Summary
1 package Data::DPath::Validator;
2 2 our $VERSION = '0.093411';
3  
4 #ABSTRACT: Validate data based on template data
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::DPath 'dpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::DPath::Validator::Visitor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 with 'MooseX::Role::BuildInstanceOf' =>
13 {
14 target => 'Data::DPath::Validator::Visitor',
15 prefix => 'visitor'
16 };
17  
18  
19 2 has '+visitor' =>
20 (
21 handles =>
22 {
23 'load' => 'visit',
24 'templates' => 'templates'
25 }
26 );
27  
28  
29 2 has 'strict_mode' =>
30 (
31 is => 'ro',
32 isa => 'Bool',
33 default => 0,
34 );
35  
36  
37 sub validate
38 4 ▶{
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
39 4 my $self = shift;
40
41 4 my $ret = [];
42 4 foreach my $data (@_)
43 {
44 7 ▶ my $pass = $self->strict_mode
Branch: 1 branch
BranchTrueFalseTotal
$self->strict_mode ? : 1 6 7
45 ? $self->_strict_validation($data)
46 : $self->_loose_validation($data);
47
48 7 push(@$ret, $pass);
49 }
50  
51 4 return $ret;
52 }
53  
54 sub _strict_validation
55 1 ▶{
Subroutine _strict_validation: called CC=3 SCAR=11.4
56 1 my ($self, $data) = @_;
57
58 1 foreach my $template (@{$self->templates})
59 {
60 22 ▶ if(!dpath($template)->match($data))
Branch: 1 branch
BranchTrueFalseTotal
unless (&dpath($template)->match($data)) 0 22 22
61 {
62 0 return 0;
63 }
64 }
65  
66 1 return 1;
67 }
68  
69 sub _loose_validation
70 6 ▶{
Subroutine _loose_validation: called CC=3 SCAR=11.0
71 6 my ($self, $data) = @_;
72
73 6 foreach my $template (@{$self->templates})
74 {
75 42 ▶ if(dpath($template)->match($data))
Branch: 1 branch
BranchTrueFalseTotal
if (&dpath($template)->match($data)) 6 36 42
76 {
77 6 return 1;
78 }
79 }
80  
81 0 return 0;
82 }
83  
84 2 __PACKAGE__->meta->make_immutable();
85 2 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__
« Validator/Visitor.pm ↑ Summary