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