Valiant/Validator/Check.pm

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