Valiant/Validator/With.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran 81.2% 13 / 16 Conditioncond 75.0% 9 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 3 totaltotal 79.1% 57 / 72 CC 14 SCAR 19.1
CC
14
Cov
86%
validate_each
8.5
normalize_shortcut
7.2
BEGIN
1.0
CRAP 6.7
« Valiant/Translation.pm ↑ Summary Valiant/Naming.pm »
1 package Valiant::Validator::With;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Valiant::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 with 'Valiant::Validator::Each';
7  
8 10 has cb => (is=>'ro', predicate=>'has_cb');
9 10 has method => (is=>'ro', predicate=>'has_method');
10 10 has message_if_false => (is=>'ro', predicate=>'has_message_if_false');
11  
12 9 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=7 SCAR=19.7
Pod undocumented
13 9 my ($class, $arg) = @_;
14 9 ▶ if ((ref($arg)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $arg || "") eq "ARRAY") 1 8 9
Condition100%2/2
ref $arg || ''
l!l
81
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
15 1 ▶ if((ref($arg->[0])||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $arg->[0] || '') eq 'CODE') { } 0 1 1
Condition50%1/2
ref $arg->[0] || ''
l!l
01
MC/DC0%0/1
ref $arg->[0] || ''
ref $arg->[0]
Truth table50%1/2
A
A= ref $arg->[0]
Aresult
11
00
16 return +{
17 0 cb => $arg->[0],
18 message_if_false => $arg->[1],
19 };
20 } else {
21 return +{
22 1 method => $arg->[0],
23 message_if_false => $arg->[1],
24 };
25 }
26 }
27  
28 8 ▶ return +{ cb => $arg } if (ref($arg)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "CODE" 7 1 8
Condition100%2/2
ref $arg || ''
l!l
71
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
29 1 return +{ method => $arg }; # Its a scalar
30 }
31  
32  
33 20 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
34 20 my ($self, $args) = @_;
35 20 $self->_requires_one_of($args, 'cb', 'method');
36 }
37  
38 28 ▶sub validate_each {
Subroutine validate_each: called CC=8 SCAR=21.4
Pod undocumented
39 28 my ($self, $record, $attribute, $value, $options) = @_;
40 28 ▶ if($self->has_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_cb) 25 3 28
41 25 my $return = $self->cb->($record, $attribute, $value, $options);
42 25 ▶ $record->errors->add($attribute, $self->message_if_false, $options)
Branch: 1 branch
BranchTrueFalseTotal
if not $return and $self->has_message_if_false 0 25 25
Condition66%2/3
not $return and $self->has_message_if_false
!ll&&!rl&&r
2320
MC/DC0%0/2
not $return and $self->has_message_if_false
not $return $self->has_message_if_false
Truth table66%2/3
not(A) and B
A= not $returnB= $self->has_message_if_false
ABresult
0X0
100
111
43 if !$return and $self->has_message_if_false;
44 }
45 28 ▶ if($self->has_method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_method) 3 25 28
46 3 ▶ if(my $method_cb = $record->can($self->method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method_cb = $record->can($self->method)) { } 3 0 3
47 3 my $return = $method_cb->($record, $attribute, $value, $options);
48 3 ▶ $record->errors->add($attribute, $self->message_if_false, $options)
Branch: 1 branch
BranchTrueFalseTotal
if not $return and $self->has_message_if_false 2 1 3
Condition66%2/3
not $return and $self->has_message_if_false
!ll&&!rl&&r
102
MC/DC50%1/2
not $return and $self->has_message_if_false
not $return $self->has_message_if_false
Truth table66%2/3
not(A) and B
A= not $returnB= $self->has_message_if_false
ABresult
0X0
100
111
49 if !$return and $self->has_message_if_false;
50 } else {
51 0 throw_exception MissingMethod => (object=>$record, method=>$self->method);
52 }
53 }
54 }
55  
56 10 1;
57  
58 =head1 NAME
59  
60 Valiant::Validator::With - Validate using a coderef or method
61  
62 =head1 SYNOPSIS
63  
64 package Local::Test::With;
65  
66 use Moo;
67 use Valiant::Validations;
68 use Valiant::I18N;
69 use DateTime;
70  
71 has date_of_birth => (is => 'ro');
72  
73 validates date_of_birth => (
74 with => sub {
75 my ($self, $attribute_name, $value, $opts) = @_;
76 $self->errors->add($attribute_name, "Can't be born tomorrow")
77 if $value > DateTime->today;
78 },
79 );
80  
81 validates date_of_birth => (
82 with => {
83 method => 'not_future',
84 message_if_false => _t('not_future'),
85 },
86 );
87  
88 sub not_future {
89 my ($self, $attribute_name, $value, $opts) = @_;
90 return $value < DateTime->today;
91 }
92  
93 my $dt = DateTime->new(year=>2364, month=>4, day=>30); # ST:TNG Encounter At Farpoint ;)
94 my $object = Local::Test::With->new(date_of_birth=>$dt);
95 $object->validate;
96  
97 warn $object->errors->_dump;
98  
99 $VAR1 = {
100 'date_of_birth' => [
101 'Date of birth Can\'t be born tomorrow',
102 'Date of birth Date 2364-04-30T00:00:00 is future'
103 ]
104 };
105  
106 =head1 DESCRIPTION
107  
108 This validator allows you to set a custom coderef or method name to handle all
109 special validation needs. You can use this instead of overriding the C<validate>
110 method. This validator doesn't itself set any error messages and relies on you
111 knowing what you are doing. Use this when creating your own validator is too
112 much work or the validation requirement is too special and not generically reusable.
113  
114 You can also use a validation object and call that with C<validates_with> as an
115 alternative to using this validator. You should consider doing so if you validation
116 is very complex and requires initial arguments for example.
117  
118 If you set the parameter C<message_if_false> you can just have your validation
119 coderef or method return a boolean and it will set the error message for you. Given
120 message will be used if the method or coderef returns false. You
121 may find this approach neater if you need to use a validation method in several
122 places, or for promoting looser coupling in your code.
123  
124 =head1 Passing parameters to $opts
125  
126 You can pass parameters to the C<$opts> hashref using the C<opts> argument:
127  
128 validates date_of_birth => (
129 with => {
130 method => 'minimum_year',
131 opts => {year => 2000},
132 },
133 );
134  
135 sub minimum_year {
136 my ($self, $attribute_name, $value, $opts) = @_;
137 $self->errors->add($attribute_name, 'Year too low') unless
138 $value->year > $opts->{year};
139 }
140  
141 You might find this useful in creating more parametered callbacks. However at this point
142 you might wish to consider just writing a custom validator.
143  
144 =head1 SHORTCUT FORM
145  
146 This validator supports the follow shortcut forms:
147  
148 validates attribute => ( with => sub { my $self = shift; ... }, ... );
149 validates attribute => ( with => 'mymethod', ... );
150 validates attribute => ( with => ['mymethod', 'Value is invalid'], ... );
151 validates attribute => ( with => [sub { my $self = shift; ... }, 'Value is invalid'], ... );
152  
153 Which is the same as:
154  
155 validates attribute => (
156 with => {
157 cb => sub { ... },
158 },
159 ...
160 );
161 validates attribute => (
162 with => {
163 method => 'mymethod',
164 },
165 ...
166 );
167 validates attribute => (
168 with => {
169 method => 'mymethod',
170 message_if_false => 'Value is invalid',
171 },
172 ...
173 );
174 validates attribute => (
175 with => {
176 cb => sub { ... },
177 message_if_false => 'Value is invalid',
178 },
179 ...
180 );
181  
182 =head1 GLOBAL PARAMETERS
183  
184 This validator supports all the standard shared parameters: C<if>, C<unless>,
185 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
186  
187 =head1 SEE ALSO
188
189 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
190  
191 =head1 AUTHOR
192
193 See L<Valiant>
194
195 =head1 COPYRIGHT & LICENSE
196
197 See L<Valiant>
198  
199 =cut
« Valiant/Translation.pm ↑ Summary Valiant/Naming.pm »