Valiant/Validator/Object.pm

filter: Statementstmt 88.0% 44 / 50 Branchbran 66.6% 16 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 2 totaltotal 78.3% 76 / 97 CC 15 SCAR 21.0
CC
15
Cov
80%
validate_each
14.4
normalize_shortcut
5.5
BEGIN
1.0
CRAP 8.2
« Valiant/Validator/Length.pm ↑ Summary Valiant/Validator/Hash.pm »
1 package Valiant::Validator::Object;
2  
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 require Role::Tiny;
8  
9 3 with 'Valiant::Validator::Each';
10  
11 3 has type_constraint => (is=>'ro', required=>0, predicate=>'has_type_constraint');
12 3 has isa => (is=>'ro', required=>0, predicate=>'has_isa');
13 3 has role => (is=>'ro', required=>0, predicate=>'has_role');
14 3 has nested => (is=>'ro', required=>0, predicate=>'has_nested');
15  
16 7 ▶has not_blessed_msg => (is=>'ro', required=>1, default=>sub {_t 'not_blessed'});
Subroutine __ANON__: called CC=1 SCAR=0.0
17 7 ▶has type_constraint_violation_msg => (is=>'ro', required=>1, default=>sub {_t 'type_constraint_violation'});
Subroutine __ANON__: called CC=1 SCAR=0.0
18 7 ▶has wrong_inheritance_msg => (is=>'ro', required=>1, default=>sub {_t 'wrong_inheritance'});
Subroutine __ANON__: called CC=1 SCAR=0.0
19 7 ▶has not_role_msg => (is=>'ro', required=>1, default=>sub {_t 'not_role'});
Subroutine __ANON__: called CC=1 SCAR=0.0
20 7 ▶has invalid_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid'});
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22  
23 5 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=4 SCAR=17.1
Pod undocumented
24 5 my ($class, $arg) = @_;
25 5 ▶ if(($arg eq '1') || ($arg eq 'nested')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq '1' or $arg eq 'nested') { } 5 0 5
elsif (builtin::blessed($arg)) { } 0 0 0
Condition66%2/3
$arg eq 1 or $arg eq "nested"
l!l&&r!l&&!r
410
MC/DC0%0/2
$arg eq 1 or $arg eq "nested"
$arg eq 1 $arg eq "nested"
Truth table66%2/3
A or B
A= $arg eq 1B= $arg eq "nested"
ABresult
1X1
011
000
26 5 return { nested => 1 };
27 } elsif( blessed $arg) {
28 0 return { type_constraint => $arg };
29 }
30 }
31  
32 13 ▶sub validate_each {
Subroutine validate_each: called CC=12 SCAR=26.7
Pod undocumented
33 13 my ($self, $record, $attribute, $value, $opts) = @_;
34  
35 13 ▶ unless(blessed $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value)) 0 13 13
36 0 $record->errors->add($attribute, $self->not_blessed_msg, $opts);
37 0 return;
38 }
39  
40 13 ▶ if($self->has_type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_constraint) 0 13 13
41 0 my $possible_error = $self->type_constraint->validate($value);
42 0 ▶ if($possible_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_error) 0 0 0
43 0 $record->errors->add(
44 $attribute,
45 $self->type_constraint_violation_msg,
46 +{
47 %$opts,
48 display_name => $self->type_constraint->display_name,
49 error_message => $possible_error,
50 });
51  
52 }
53 }
54  
55 13 ▶ if($self->has_isa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_isa) 5 8 13
56 5 ▶ unless($value->isa($self->isa)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa($self->isa)) 1 4 5
57 1 $record->errors->add($attribute, $self->wrong_inheritance_msg, +{%$opts, parent=>$self->isa});
58 }
59 }
60  
61 13 ▶ if($self->has_role) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_role) 5 8 13
62 5 ▶ unless(Role::Tiny::does_role($value, $self->role)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Role::Tiny::does_role($value, $self->role)) 1 4 5
63 1 $record->errors->add($attribute, $self->not_role_msg, +{%$opts, role=>$self->role});
64 }
65 }
66  
67 13 ▶ if($self->has_nested && $self->nested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_nested and $self->nested) 12 1 13
Condition66%2/3
$self->has_nested and $self->nested
!ll&&!rl&&r
1012
MC/DC50%1/2
$self->has_nested and $self->nested
$self->has_nested $self->nested
Truth table66%2/3
A and B
A= $self->has_nestedB= $self->nested
ABresult
0X0
100
111
68 12 ▶ $value->validate(%$opts) unless $value->validated; # Don't validate again
Branch: 1 branch
BranchTrueFalseTotal
unless $value->validated 12 0 12
69 12 ▶ $record->errors->add($attribute, $self->invalid_msg, $opts) if $value->errors->size;
Branch: 1 branch
BranchTrueFalseTotal
if $value->errors->size 8 4 12
70  
71 19 ▶ $value->errors->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 19 my ($attr, $message) = @_;
73 #$attribute =~s/\[\d+\]$//;
74 19 $record->errors->add("${attribute}.${attr}", $message);
75 12 });
76 }
77 }
78  
79 3 1;
80  
81 =head1 NAME
82  
83 Valiant::Validator::Object - Verify a related object
84  
85 =head1 SYNOPSIS
86  
87 package Local::Test::Address;
88  
89 use Moo;
90 use Valiant::Validations;
91  
92 has street => (is=>'ro');
93 has city => (is=>'ro');
94 has country => (is=>'ro');
95  
96 validates ['street', 'city'],
97 presence => 1,
98 length => [3, 40];
99  
100 validates 'country',
101 presence => 1,
102 inclusion => [qw/usa uk canada japan/];
103  
104 package Local::Test::Person;
105  
106 use Moo;
107 use Valiant::Validations;
108  
109 has name => (is=>'ro');
110 has address => (is=>'ro');
111 has car => (is=>'ro');
112  
113 validates name => (
114 length => [2,30],
115 format => qr/[A-Za-z]+/, #yes no unicode names for this test...
116 );
117  
118 validates address => (
119 presence => 1,
120 object => {
121 nested => 1,
122 isa => 'Local::Test::Address',
123 }
124 );
125  
126 =head1 DESCRIPTION
127  
128 Runs validations on an object which is assigned as an attribute and
129 aggregates those errors (if any) onto the parent object.
130  
131 Useful when you need to validate an object graph or nested forms.
132  
133 If your nested object has a nested object it will follow all the way
134 down the rabbit hole Just don't make self referential nested objects;
135 that's not tested and likely to end poorly. Patches welcomed.
136  
137 =head1 ATTRIBUTES
138  
139 This validator supports the following attributes:
140  
141 =head2 nested
142  
143 A boolean that specifies if we should run 'validates' on the object. Default is false.
144  
145 =head2 type_constraint
146  
147 Reference to a L<Type::Tiny> style type constraint. If specified then the object must
148 pass the constraint.
149  
150 =head2 type_constraint_violation_msg
151  
152 The message we return when 'type_constraint' fails. We pass 'display_name' and 'error_message'
153 as options to the tag.
154  
155 =head2 isa
156  
157 The name of a class that the object should inherit from
158  
159 =head2 wrong_inheritance_msg
160  
161 The message we return when 'isa' fails. We pass 'parent' (the name of the class we require
162 inheritance from) as options to the tag.
163  
164 =head2 role
165  
166 A role that the object is expected to consume
167  
168 =head2 not_role_msg
169  
170 The message we return when 'role' fails. We pass 'rolw' (the name of the role we require
171 to consume from) as options to the tag.
172  
173 =head2 invalid_msg
174  
175 The error message returned when the object has nested validation errors.
176  
177 =head2 not_blessed_msg
178  
179 The error returned when the value is not an object
180  
181 =head1 SHORTCUT FORM
182  
183 This validator supports the follow shortcut forms:
184  
185 validates attribute => ( object => 1, ... );
186  
187 Which is the same as:
188  
189 validates attribute => (
190 object => {
191 nested => 1,
192 }
193 );
194  
195 B<Note>: you can use the 'nested' alias for '1' here if you want.
196  
197 You can also specify a type constraint:
198  
199 use Types::Standard 'Str';
200  
201 validates attribute => ( object => Str, ... );
202  
203 Which is the same as:
204  
205 use use Types::Standard 'Str';
206  
207 validates attribute => (
208 object => {
209 type_constraint => Str,
210 }
211 );
212  
213 =head1 AGGREGATED ERROR MESSAGES
214  
215 When you nest a object with validations as in the following example any error messages
216 in the nested object are imported into the parent object:
217  
218 package Local::Test::Address;
219  
220 use Moo;
221 use Valiant::Validations;
222  
223 has street => (is=>'ro');
224 has city => (is=>'ro');
225 has country => (is=>'ro');
226  
227 validates ['street', 'city'],
228 presence => 1,
229 length => [3, 40];
230  
231 validates 'country',
232 presence => 1,
233 inclusion => [qw/usa uk canada japan/];
234  
235 package Local::Test::Person;
236  
237 use Moo;
238 use Valiant::Validations;
239  
240 has name => (is=>'ro');
241 has address => (is=>'ro');
242  
243 validates name => (
244 length => [2,30],
245 format => qr/[A-Za-z]+/, #yes no unicode names for this test...
246 );
247  
248 validates address => (
249 presence => 1,
250 object => {
251 nested => 1,
252 }
253 );
254  
255 my $address = Local::Test::Address->new(
256 city => 'NY',
257 country => 'Russia'
258 );
259  
260 my $person = Local::Test::Person->new(
261 name => '12234',
262 address => $address,
263 );
264  
265 my $address_errors = +{ $person->address->errors->to_hash(full_messages=>1) };
266  
267 # $address_errors = +{
268 # 'country' => [
269 # 'Country is not in the list'
270 # ],
271 # 'city' => [
272 # 'City is too short (minimum is 3 characters)'
273 # ],
274 # 'street' => [
275 # 'Street can\'t be blank',
276 # 'Street is too short (minimum is 3 characters)'
277 # ],
278 # };
279  
280 my $person_errors = +{ $person->errors->to_hash(full_messages=>1) };
281  
282 # $person_errors = +{
283 # name => [
284 # "Name does not match the required pattern",
285 # ],
286 # address => [
287 # "Address Is Invalid",
288 # ],
289 # "address.city" => [
290 # "Address City is too short (minimum is 3 characters)",
291 # ],
292 # "address.country" => [
293 # "Address Country is not in the list",
294 # ],
295 # "address.street" => [
296 # "Address Street can't be blank",
297 # "Address Street is too short (minimum is 3 characters)",
298 # ],
299 # };
300  
301 When accessing errors for display you'll have to choose which access approach is
302 best for your application.
303  
304 Please note that you can have objects nested inside of objects so this can lead to
305 very complex error messaging.
306  
307 =head1 GLOBAL PARAMETERS
308  
309 This validator supports all the standard shared parameters: C<if>, C<unless>,
310 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
311  
312 =head1 SEE ALSO
313
314 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
315  
316 =head1 AUTHOR
317
318 See L<Valiant>
319
320 =head1 COPYRIGHT & LICENSE
321
322 See L<Valiant>
323  
324 =cut
« Valiant/Validator/Length.pm ↑ Summary Valiant/Validator/Hash.pm »