Valiant/Validator/Confirmation.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran 80.0% 8 / 10 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 83.1% 59 / 71 CC 11 SCAR 14.7
CC
11
Cov
89%
validate_each
7.1
BUILD
4.0
normalize_shortcut
2.1
CRAP 4.4
« Valiant/Validator/Array.pm ↑ Summary Valiant/Validator/Unique.pm »
1 package Valiant::Validator::Confirmation;
2  
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Valiant::Util 'throw_exception', 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 with 'Valiant::Validator::Each';
8  
9 8 ▶has confirmation => (is=>'ro', required=>1, default=>sub {_t 'confirmation'});
Subroutine __ANON__: called CC=1 SCAR=0.0
10 5 has suffix => (is=>'ro', required=>1, default=>'_confirmation');
11  
12 sub BUILD {
13 8 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
14 8 my $model_class = $self->model_class;
15 8 ▶ foreach my $attribute (@{$self->attributes||[]}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->attributes or [] 0 8 8
16 8 my $confirmation_attribute = "${attribute}${\$self->suffix}";
17 8 ▶ next if $model_class->can($confirmation_attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $model_class->can($confirmation_attribute) 3 5 8
18 5 $model_class->inject_attribute($confirmation_attribute);
19 }
20 }
21  
22 sub normalize_shortcut {
23 1 ▶ my ($class, $arg) = @_;
Subroutine normalize_shortcut: called CC=2 SCAR=7.6
Pod undocumented
24 1 ▶ if($arg eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq '1') { } 1 0 1
25 1 return +{};
26 } else {
27 0 return +{ suffix=>$arg };
28 }
29 }
30  
31 sub validate_each {
32 31 ▶ my ($self, $record, $attribute, $value, $opts) = @_;
Subroutine validate_each: called CC=7 SCAR=19.6
Pod undocumented
33 31 my $confirmation_attribute = "${attribute}${\$self->suffix}";
34 31 ▶ my $confirmation = $record->can($confirmation_attribute) ||
Condition33%1/3
$record->can($confirmation_attribute) || throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
l!l&&r!l&&!r
3100
MC/DC0%0/2
$record->can($confirmation_attribute) || throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
$record->can($confirmation_attribute) throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
Truth table33%1/3
A || B
A= $record->can($confirmation_attribute)B= throw_exception('MissingMethod', ('object', $record, 'method', $confirmation_attribute))
ABresult
1X1
011
000
35 throw_exception MissingMethod => (object=>$record, method=>$confirmation_attribute);
36  
37 31 my $confirmation_value = $confirmation->($record);
38  
39 # If confirmation is not defined then skip this validation. If the user wants to have
40 # this defined they can use the presence validator.
41  
42 31 ▶ return unless defined($confirmation_value) || defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $confirmation_value or defined $value 2 29 31
Condition100%3/3
defined $confirmation_value or defined $value
l!l&&r!l&&!r
2452
MC/DC100%2/2
defined $confirmation_value or defined $value
defined $confirmation_value defined $value
Truth table100%3/3
A or B
A= defined $confirmation_valueB= defined $value
ABresult
1X1
011
000
43  
44 29 ▶ if( ($value||'') ne ($confirmation_value||'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($value || "") ne ($confirmation_value || "")) 8 21 29
Condition75%3/4
$value || ''
l!l
290
$confirmation_value || ''
l!l
245
MC/DC0%0/1
$value || ''
$value
MC/DC100%1/1
$confirmation_value || ''
$confirmation_value
Truth table50%1/2
A
A= $value
Aresult
11
00
Truth table100%2/2
A
A= $confirmation_value
Aresult
11
00
45 8 my $human_attribute_name = $record->human_attribute_name($attribute);
46 8 $record->errors->add($confirmation_attribute, $self->confirmation, +{%$opts, attribute=>"$human_attribute_name"})
47 }
48 }
49  
50 5 1;
51  
52 =head1 NAME
53  
54 Valiant::Validator::Confirmation - Checks for a 'confirming' attributes equality.
55  
56 =head1 SYNOPSIS
57  
58 package Local::Test::Confirmation;
59  
60 use Moo;
61 use Valiant::Validations;
62  
63 has email=> (is=>'ro');
64  
65 validates email => ( confirmation => 1 );
66  
67 my $object = Local::Test::Confirmation->new(
68 email => 'AAA@example.com',
69 email_confirmation => 'ZZZ@example.com'
70 );
71 $object->validate;
72  
73 warn $object->errors->_dump;
74  
75 $VAR1 = {
76 'email_confirmation' => [
77 "Email confirmation doesn't match 'Email'",
78 ]
79 };
80  
81 =head1 DESCRIPTION
82  
83 Use this when you have two attributes which should be set to the same value
84 (for example to confirm someone entered the correct email address or changed
85 their password to the same value).
86  
87 The error message (if any) will appear associated with the confirmation attribute.
88 Error message uses tag C<confirmation> and you can override that with an init arg
89 of the same name. You can also change the prefix used to identify the confirming
90 attribute with the C<prefix> init arg (default value is '_confirmation').
91  
92 B<NOTE:> You don't need to add the confirmation attribute, we inject it for you during
93 validation setup.
94  
95 =head1 SHORTCUT FORM
96  
97 This validator supports the follow shortcut forms:
98  
99 validates attribute => ( confirmation => 1, ... );
100  
101 Which is the same as:
102  
103 validates attribute => (
104 confirmation => +{},
105 );
106  
107 Not a lot of saved typing but it seems to read better.
108
109 =head1 GLOBAL PARAMETERS
110  
111 This validator supports all the standard shared parameters: C<if>, C<unless>,
112 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
113  
114 =head1 SEE ALSO
115
116 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
117  
118 =head1 AUTHOR
119
120 See L<Valiant>
121
122 =head1 COPYRIGHT & LICENSE
123
124 See L<Valiant>
125  
126 =cut
« Valiant/Validator/Array.pm ↑ Summary Valiant/Validator/Unique.pm »