Valiant/Validator/Check.pm

filter: Statementstmt 100% 28 / 28 Branchbran 70.0% 7 / 10 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 77.5% 45 / 58 CC 11 SCAR 16.5
CC
11
Cov
84%
__ANON__
8.2
validate_each
5.1
BEGIN
1.0
CRAP 5.2
« Valiant/Validator/Date.pm ↑ Summary Valiant/Validator/Unique.pm »
1 package Valiant::Validator::Check;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 with 'Valiant::Validator::Each';
8  
9 4 ▶has constraint => (is=>'ro', required=>1, isa=>sub {
Subroutine __ANON__: called CC=7 SCAR=21.1
10 4 my $value = shift;
11 4 ▶ my $ref = ref($value) || '';
Condition50%1/2
ref $value || ''
l!l
40
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
12 4 ▶ return if $ref eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "CODE" 1 3 4
13 3 ▶ my @constraints = $ref eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
$ref eq 'ARRAY' ? : 0 3 3
14 3 foreach my $constraint (@constraints) {
15 3 ▶ die "constraint must be an object (or arrayref of objects) that can 'check'"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($constraint) and $constraint->can("check") 0 3 3
Condition33%1/3
builtin::blessed($constraint) and $constraint->can("check")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($constraint) and $constraint->can("check")
builtin::blessed($constraint) $constraint->can("check")
Truth table33%1/3
A and B
A= builtin::blessed($constraint)B= $constraint->can("check")
ABresult
0X0
100
111
16 unless blessed($constraint) && $constraint->can('check');
17 }
18 1 });
19 4 ▶has check => (is=>'ro', required=>1, default=>sub {_t 'check'});
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 1 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=1 SCAR=0.0
Pod undocumented
22 1 my ($class, $arg) = @_;
23 1 return {constraint => $arg};
24 }
25  
26 8 ▶sub validate_each {
Subroutine validate_each: called CC=5 SCAR=16.3
Pod undocumented
27 8 my ($self, $record, $attribute, $value, $opts) = @_;
28 8 my $check_constraint_proto = $self->_cb_value($record, $self->constraint);
29 8 ▶ my @check_constraints = (ref($check_constraint_proto)||'') eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
(ref $check_constraint_proto || '') eq 'ARRAY' ? : 0 8 8
Condition50%1/2
ref $check_constraint_proto || ''
l!l
80
MC/DC0%0/1
ref $check_constraint_proto || ''
ref $check_constraint_proto
Truth table50%1/2
A
A= ref $check_constraint_proto
Aresult
11
00
30 @$check_constraint_proto :
31 ($check_constraint_proto);
32  
33 8 foreach my $check_constraint (@check_constraints) {
34 8 ▶ unless($check_constraint->check($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check_constraint->check($value)) 4 4 8
35 4 $record->errors->add($attribute, $self->check, $opts)
36 }
37 }
38 }
39  
40 1 1;
41  
42 =head1 NAME
43  
44 Valiant::Validator::Check - Validate using a 'check' method
45  
46 =head1 SYNOPSIS
47  
48 package Local::Test::Check;
49  
50 use Moo;
51 use Valiant::Validations;
52 use Types::Standard 'Int';
53  
54 has retiree_age => (is=>'ro');
55  
56 validates retiree_age => (
57 check => {
58 constraint => Int->where('$_ >= 65')
59 }
60 );
61
62 my $object = Local::Test::Check->new(retiree_age=>40);
63 $object->validate;
64  
65 warn $object->errors->_dump;
66  
67 $VAR1 = {
68 'retiree_age' => [
69 'Retiree age is invalid'
70 ]
71 };
72  
73 =head1 DESCRIPTION
74  
75 Let's you use an object that does C<check> as the validation method. Basically
76 this exists to let you use or reuse a lot of existing type constraint validation
77 libraries on CPAN such as L<Type::Tiny>. You might already be making heavy use of
78 these in your code (or you might just be very familiar with them) so it it might
79 make sense to you to just reuse them rather than learn a bunch of the custom
80 validators that are packaged with L<Valiant>.
81  
82 You might also prefer the 'spellchecking' safety of something like L<Type::Tiny>
83 which uses imported methods and will result in a compile time error if you
84 mistype the constraint name. Its also possible some of the XS versions of
85 L<Type::Tiny> are faster then the built in validators that ship with L<Valiant>
86  
87 Please note this validator is also a available as a shortcut which is built into
88 the C<validates> method itself:
89  
90 validates retiree_age => (
91 Int->where('$_ >= 65'), +{
92 message => 'A retiree must be at least 65 years old,
93 },
94 ...
95 );
96  
97 This built in shortcut just wraps this validator under the hood. I saw no reason
98 to not expose it publically but its less typing to just use the short method.
99  
100 =head1 ATTRIBUTES
101  
102 This validator supports the following attributes
103  
104 =head2 constraint
105  
106 Takes an object or arrayref of objects that can provide a C<check> method which given
107 the value to be checked will return true if the value is valid and false otherwise.
108  
109 Supports coderef for dynamically providing a constraint.
110  
111 =head2 check
112  
113 Either a translation tag or a string message for the error given when the validation fails.
114 Defaults to "_t('check')".
115  
116 =head1 SHORTCUT FORM
117  
118 This validator supports the follow shortcut forms:
119  
120 validates attribute => ( check => Int->where('$_ >= 65'), ... );
121  
122 Which is the same as:
123  
124 validates attribute => (
125 check => {
126 constraint => Int->where('$_ >= 65'),
127 },
128 ...
129 );
130  
131 =head1 GLOBAL PARAMETERS
132  
133 This validator supports all the standard shared parameters: C<if>, C<unless>,
134 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
135  
136 =head1 SEE ALSO
137
138 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
139  
140 =head1 AUTHOR
141
142 See L<Valiant>
143
144 =head1 COPYRIGHT & LICENSE
145
146 See L<Valiant>
147  
148 =cut
« Valiant/Validator/Date.pm ↑ Summary Valiant/Validator/Unique.pm »