Valiant/Validator/Boolean.pm

filter: Statementstmt 90.9% 20 / 22 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 2 totaltotal 86.1% 31 / 36 CC 4 SCAR 8.9
CC
4
Cov
93%
validate_each
4.0
normalize_shortcut
2.0
BEGIN
1.0
CRAP 2.4
« Valiant/Proxy.pm ↑ Summary Valiant/Filter/Template.pm »
1 package Valiant::Validator::Boolean;
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 1 has state => (is=>'ro', required=>1);
9 2 ▶has is_not_true => (is=>'ro', required=>1, default=>sub {_t 'is_not_true'});
Subroutine __ANON__: called CC=1 SCAR=0.0
10 2 ▶has is_not_false => (is=>'ro', required=>1, default=>sub {_t 'is_not_false'});
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 0 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: not called CC=1 SCAR=6.9
Pod undocumented
13 0 my ($class, $arg) = @_;
14 0 return +{ state => $arg };
15 }
16  
17 4 ▶sub validate_each {
Subroutine validate_each: called CC=4 SCAR=13.9
Pod undocumented
18 4 my ($self, $record, $attribute, $value, $opts) = @_;
19 4 my $state = $self->_cb_value($record, $self->state);
20  
21 4 ▶ if($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) { } 2 2 4
22 # value must be true
23 2 ▶ unless($value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 1 1 2
24 1 $record->errors->add($attribute, $self->is_not_true, $opts);
25 }
26 } else {
27 # value must be false
28 2 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 1 1 2
29 1 $record->errors->add($attribute, $self->is_not_false, $opts);
30 }
31 }
32 }
33  
34 1 1;
35  
36 =head1 NAME
37  
38 Valiant::Validator::Boolean - Verify that a value is either true or false
39  
40 =head1 SYNOPSIS
41  
42 package Local::Test::Boolean;
43  
44 use Moo;
45 use Valiant::Validations;
46  
47 has active => (is=>'ro');
48  
49 validates active => (
50 boolean => {
51 state => 1, # valid values are 1 (must be true) or 0 (must be false)
52 }
53 );
54  
55 my $object = Local::Test::Boolean->new(active=>0);
56 $object->validate;
57  
58 warn $object->errors->_dump;
59  
60 $VAR1 = {
61 'active' => [
62 'Active must be true',
63 ]
64 };
65  
66 =head1 DESCRIPTION
67  
68 Checks a value to see if it is true or false based on Perl's notion of
69 truthiness. This will not limit the value. For example values of 1, 'one'
70 and [1,2,3] are considered true while values of 0, undef are false.
71  
72 =head1 ATTRIBUTES
73  
74 This validator supports the following attributes:
75  
76 =head2 state
77  
78 The required boolean value of the given value for the validator to pass.
79 Allowed values are 1 (which requires true) and 0 (which requires false).
80  
81 Value may also be a coderef.
82  
83 =head2 is_not_true
84  
85 The error message / tag used when the value is not true
86  
87 =head2 is_not_false
88  
89 The error message / tag used when the value is not false
90  
91 =head1 SHORTCUT FORM
92  
93 This validator supports the follow shortcut forms:
94  
95 validates attribute => ( boolean => 1, ... );
96  
97 Which is the same as:
98  
99 validates attribute => (
100 boolean => {
101 state => 1,
102 }
103 );
104  
105 The negation of this also works
106  
107 validates attribute => ( boolean => 0, ... );
108  
109 Which is the same as:
110  
111 validates attribute => (
112 boolean => {
113 state => 0,
114 }
115 );
116  
117 =head1 GLOBAL PARAMETERS
118  
119 This validator supports all the standard shared parameters: C<if>, C<unless>,
120 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
121  
122 =head1 SEE ALSO
123
124 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
125  
126 =head1 AUTHOR
127
128 See L<Valiant>
129
130 =head1 COPYRIGHT & LICENSE
131
132 See L<Valiant>
133  
134 =cut
« Valiant/Proxy.pm ↑ Summary Valiant/Filter/Template.pm »