Valiant/Validates.pm

filter: Statementstmt 93.5% 247 / 264 Branchbran 75.7% 100 / 132 Conditioncond 83.3% 55 / 66 MC/DCmcdc 65.7% 23 / 35 Subroutinesub 88.6% 39 / 44 Podpod 57.1% 12 / 21 totaltotal 84.7% 476 / 562 CC 139 SCAR 26.9
CC
139
Cov
87%
__validates_for_exporter
31.5
validates
31.5
validate_only
14.1
CRAP 14.7
« Valiant/HTML/Util/Form.pm ↑ Summary Valiant/HTML/Util/TagBuilder.pm »
1 package Valiant::Validates;
2  
3 40 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use String::CamelCase 'camelize';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Valiant::Util 'throw_exception', 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use namespace::autoclean -also => ['throw_exception', 'debug'];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Valiant::Validations ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 with 'Valiant::Translation';
12  
13 40 has _instance_validations => (is=>'rw', init_arg=>undef);
14  
15 1 ▶sub push_to_i18n_lookup {
Subroutine __push_to_i18n_lookup_for_exporter: called CC=3 SCAR=16.5
Subroutine push_to_i18n_lookup: not called CC=3 SCAR=16.5
Pod undocumented
16 0 my ($class_or_self, @args) = @_;
17 0 ▶ my $class = ref($class_or_self) ? ref($class_or_self) : $class_or_self;
Branch: 2 branches
BranchTrueFalseTotal
ref $class_or_self ? : 0 1 1
ref $class_or_self ? : 0 0 0
18 0 ▶ @args = $class unless @args;
Branch: 2 branches
BranchTrueFalseTotal
unless @args 1 0 1
unless @args 0 0 0
19 0 Valiant::Validations::_add_metadata($class, 'i18n', @args);
20 }
21  
22 1006 ▶sub i18n_lookup {
Subroutine i18n_lookup: called CC=3 SCAR=11.0
Pod undocumented
23 1006 my ($class_or_self, $arg) = @_;
24 1006 ▶ my $class = ref($class_or_self) ? ref($class_or_self) : $class_or_self;
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 848 158 1006
25 40 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1006 my @proposed = @{"${class}::ISA"};
27 1006 ▶ push @proposed, $class_or_self->i18n_metadata if $class_or_self->can('i18n_metadata');
Branch: 1 branch
BranchTrueFalseTotal
if $class_or_self->can("i18n_metadata") 104 902 1006
28 1006 return grep { $_->can('model_name') } ($class, @proposed);
29 }
30  
31 216 ▶sub validations {
Subroutine validations: called CC=8 SCAR=20.8
Pod undocumented
32 216 my ($class_or_self, $arg) = @_;
33 216 ▶ my $class = ref($class_or_self) ? ref($class_or_self) : $class_or_self;
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 113 103 216
34  
35 216 my @existing = ();
36 216 ▶ if(defined($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 104 112 216
37 104 ▶ if(ref($class_or_self)) { # its $self
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_self) { } 1 103 104
38 1 ▶ my @existing = @{ $class_or_self->_instance_validations||[] };
Condition50%1/2
$class_or_self->_instance_validations || []
l!l
01
MC/DC0%0/1
$class_or_self->_instance_validations || []
$class_or_self->_instance_validations
Truth table50%1/2
A
A= $class_or_self->_instance_validations
Aresult
11
00
39 1 $class_or_self->_instance_validations([$arg, @existing]);
40 } else {
41 103 Valiant::Validations::_add_metadata($class_or_self, 'validations', $arg);
42 }
43 }
44 216 ▶ @existing = @{ $class_or_self->_instance_validations||[] } if ref $class_or_self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class_or_self 113 103 216
Condition100%2/2
$class_or_self->_instance_validations || []
l!l
2111
MC/DC100%1/1
$class_or_self->_instance_validations || []
$class_or_self->_instance_validations
Truth table100%2/2
A
A= $class_or_self->_instance_validations
Aresult
11
00
45 216 ▶ my @validations = $class_or_self->validations_metadata if $class_or_self->can('validations_metadata');
Branch: 1 branch
BranchTrueFalseTotal
if $class_or_self->can("validations_metadata") 211 5 216
46 216 return @validations, @existing;
47 }
48  
49 121 ▶sub errors_class { 'Valiant::Errors' }
Subroutine errors_class: called CC=1 SCAR=0.0
Pod undocumented
50  
51 has 'errors' => (
52 is => 'ro',
53 init_arg => undef,
54 lazy => 1,
55 121 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 121 return use_module($_[0]->errors_class)
57 ->new(object=>$_[0], i18n=>$_[0]->i18n);
58 },
59 40 );
60  
61 7 ▶sub has_errors {
Subroutine has_errors: called CC=2 SCAR=7.6
Pod documented
62 7 ▶ return shift->errors->size ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->errors->size ? : 7 0 7
63 }
64  
65 0 ▶sub no_errors {
Subroutine no_errors: not called CC=1 SCAR=6.9
Pod documented
66 0 return !shift->has_errors;
67 }
68  
69 40 has '_context' => (is=>'rw', required=>0, predicate=>'has_context');
70  
71 0 ▶sub get_context { shift->_context }
Subroutine get_context: not called CC=1 SCAR=6.9
Pod documented
72  
73 1 ▶sub context {
Subroutine context: called CC=2 SCAR=7.0
Pod documented
74 1 my ($self, $args) = @_;
75 1 ▶ $args = ref($args) ? $args : [$args];
Branch: 1 branch
BranchTrueFalseTotal
ref $args ? : 1 0 1
76 1 $self->_context($args);
77 1 return $self;
78 }
79  
80 40 has '_csrf_token' => (is=>'rw', required=>0, init_arg=>'csrf_token', predicate=>'has_csrf_token');
81  
82 0 ▶sub csrf_token {
Subroutine csrf_token: not called CC=3 SCAR=24.8
Pod documented
83 0 my ($self, $arg) = @_;
84 0 ▶ $self->_csrf_token($arg) if $arg;
Branch: 1 branch
BranchTrueFalseTotal
if $arg 0 0 0
85 0 ▶ return $arg ? $self : $self->_csrf_token;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 0 0 0
86 }
87  
88 40 has 'validated' => (is=>'rw', required=>1, init_arg=>undef, default=>0);
89 40 has 'skip_validation' => (is=>'rw', required=>1, init_arg=>undef, default=>0);
90  
91 1 ▶sub skip_validate {
Subroutine skip_validate: called CC=1 SCAR=0.0
Pod documented
92 1 my ($self) = @_;
93 1 $self->skip_validation(1);
94 1 return $self;
95 }
96 0 ▶sub do_validate {
Subroutine do_validate: not called CC=1 SCAR=6.9
Pod documented
97 0 my ($self) = @_;
98 0 $self->skip_validation(0);
99 0 return $self;
100 }
101  
102 633 ▶sub default_validator_namepart { 'Validator' }
Subroutine default_validator_namepart: called CC=1 SCAR=0.0
Pod undocumented
103 15 ▶sub default_collection_class { 'Valiant::Validator::Collection' }
Subroutine default_collection_class: called CC=1 SCAR=0.0
Pod undocumented
104  
105 677 ▶sub read_attribute_for_validation {
Subroutine read_attribute_for_validation: called CC=3 SCAR=11.1
Pod undocumented
106 677 my ($self, $attribute) = @_;
107 677 ▶ return unless defined $attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 677 677
108 677 ▶ return my $value = $self->$attribute
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attribute) 640 37 677
109 if $self->can($attribute);
110 }
111  
112 118 ▶sub _validates_coderef {
Subroutine _validates_coderef: called CC=1 SCAR=0.0
113 118 my ($self, $coderef, $attributes, %options) = @_;
114 118 $self->validations([$coderef, \%options, $attributes]);
115 118 return $self;
116 }
117  
118 165 ▶sub _is_reserved_option_key {
Subroutine _is_reserved_option_key: called CC=9 SCAR=22.0
119 165 my ($key) = @_;
120 165 ▶ return 1 if $key eq 'if' || $key eq 'unless' || $key eq 'on'
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict" or $key eq "allow_blank" or $key eq "allow_undef" or $key eq "message" or $key eq "list" 27 138 165
Condition90%19/21
$key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict" or $key eq "allow_blank" or $key eq "allow_undef" or $key eq "message" or $key eq "list"
l!l&&r!l&&!r
270138
$key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict" or $key eq "allow_blank" or $key eq "allow_undef" or $key eq "message"
l!l&&r!l&&!r
225138
$key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict" or $key eq "allow_blank" or $key eq "allow_undef"
l!l&&r!l&&!r
202143
$key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict" or $key eq "allow_blank"
l!l&&r!l&&!r
119145
$key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict"
l!l&&r!l&&!r
101154
$key eq "if" or $key eq "unless" or $key eq "on"
l!l&&r!l&&!r
46155
$key eq "if" or $key eq "unless"
l!l&&r!l&&!r
40161
MC/DC75%6/8
$key eq "if" or $key eq "unless" or $key eq "on" or $key eq "strict" or $key eq "allow_blank" or $key eq "allow_undef" or $key eq "message" or $key eq "list"
$key eq "if" $key eq "unless" $key eq "on" $key eq "strict" $key eq "allow_blank" $key eq "allow_undef" $key eq "message" $key eq "list"
Truth table77%7/9
A or B or C or D or E or F or G or H
A= $key eq "if"B= $key eq "unless"C= $key eq "on"D= $key eq "strict"E= $key eq "allow_blank"F= $key eq "allow_undef"G= $key eq "message"H= $key eq "list"
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
121 || $key eq 'strict' || $key eq 'allow_blank' || $key eq 'allow_undef'
122 || $key eq 'message' || $key eq 'list';
123 138 return 0;
124 }
125  
126 128 ▶sub _prepare_validator_packages {
Subroutine _prepare_validator_packages: called CC=2 SCAR=7.0
127 128 my ($class, $key) = @_;
128 128 my $camel = camelize($key);
129 128 my @packages = $class->_normalize_validator_package($camel);
130  
131 128 ▶ return @packages if $camel =~/^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $camel =~ /^\+/ 0 128 128
132  
133 push @packages, map {
134 128 "${_}::${camel}";
135 } $class->default_validator_namespaces;
136  
137 128 return @packages;
138 }
139  
140 128 ▶sub default_validator_namespaces {
Subroutine default_validator_namespaces: called CC=1 SCAR=0.0
Pod undocumented
141 128 my ($self) = @_;
142 128 return ('Valiant::ValidatorX', 'Valiant::Validator');
143 }
144  
145 128 ▶sub _validator_package {
Subroutine _validator_package: called CC=4 SCAR=14.1
146 128 my ($self, $key) = @_;
147 128 my @validator_packages = $self->_prepare_validator_packages($key);
148 my ($validator_package, @rest) = grep {
149 128 my $package_to_test = $_;
150 868 ▶ eval { use_module $package_to_test } || do {
Condition66%2/3
eval { do { &use_module($package_to_test) } } || do { my $notional_filename = &Module::Runtime::module_notional_filename($package_to_test); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package_to_test}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package_to_test, 'err', $@)); } }
l!l&&r!l&&!r
1460722
MC/DC50%1/2
eval { do { &use_module($package_to_test) } } || do { my $notional_filename = &Module::Runtime::module_notional_filename($package_to_test); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package_to_test}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package_to_test, 'err', $@)); } }
eval { do { &use_module($package_to_test) } } do { my $notional_filename = &Module::Runtime::module_notional_filename($package_to_test); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package_to_test}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package_to_test, 'err', $@)); } }
Truth table66%2/3
A || B
A= eval { do { &use_module($package_to_test) } }B= do { my $notional_filename = &Module::Runtime::module_notional_filename($package_to_test); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package_to_test}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package_to_test, 'err', $@)); } }
ABresult
1X1
011
000
151 # This regexp matches too much... We need to add the package
152 # path here just the path delim will vary from platform to platform
153 722 my $notional_filename = Module::Runtime::module_notional_filename($package_to_test);
154 722 ▶ if($@=~m/^Can't locate $notional_filename/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Can't locate $notional_filename/) { } 722 0 722
155 722 debug 1, "Can't find '$package_to_test' in \@INC";
156 722 0;
157 } else {
158 0 throw_exception UnexpectedUseModuleError => (package => $package_to_test, err => $@);
159 }
160 }
161 } @validator_packages;
162 128 ▶ throw_exception('NameNotValidator', name => $key, packages => \@validator_packages)
Branch: 1 branch
BranchTrueFalseTotal
unless $validator_package 0 128 128
163 unless $validator_package;
164 128 debug 1, "Found $validator_package in \@INC";
165 128 return $validator_package;
166 }
167  
168 128 ▶sub _create_validator {
Subroutine _create_validator: called CC=1 SCAR=0.0
169 128 my ($self, $validator_package, $args) = @_;
170 128 debug 1, "Trying to create validator from $validator_package";
171 128 my $validator = $validator_package->new($args);
172 128 return $validator;
173 }
174  
175 103 ▶sub validates {
Subroutine __validates_for_exporter: called CC=31 SCAR=34.5
176 103 my ($self, @validation_proto) = @_;
177  
178 # handle a list of attributes with validations
179 103 my $attributes = shift @validation_proto;
180 103 ▶ $attributes = [$attributes] unless ref $attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attributes 91 12 103
181 103 my @options = @validation_proto;
182  
183 # We want to preserve the order of validators while stripping out global_options
184 103 my (@validator_info, %global_options) = ();
185 103 ▶ while(@options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options) 150 103 253
186 150 my $args;
187 150 my $key = shift(@options);
188 150 ▶ if(blessed($key) && $key->can('check')) { # This bit allows for Type::Tiny instead of a validator => \%params setup
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($key) and $key->can('check')) { } 1 149 150
elsif ((ref $key || '') eq 'CODE') { } 4 145 149
Condition66%2/3
builtin::blessed($key) and $key->can("check")
!ll&&!rl&&r
14901
MC/DC50%1/2
builtin::blessed($key) and $key->can("check")
builtin::blessed($key) $key->can("check")
Truth table66%2/3
A and B
A= builtin::blessed($key)B= $key->can("check")
ABresult
0X0
100
111
189 1 $args = { constraint => $key };
190 1 $key = 'check';
191 1 ▶ if((ref($options[0])||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $options[0] || "") eq "HASH") 1 0 1
Condition50%1/2
ref $options[0] || ''
l!l
10
MC/DC0%0/1
ref $options[0] || ''
ref $options[0]
Truth table50%1/2
A
A= ref $options[0]
Aresult
11
00
192 1 my $base_args = shift(@options);
193 1 $args = +{ %$args, %$base_args };
194 }
195 ▶ } elsif((ref($key)||'') eq 'CODE') { # This bit allows for callbacks instead of a validator => \%params setup
Condition100%2/2
ref $key || ''
l!l
4145
MC/DC100%1/1
ref $key || ''
ref $key
Truth table100%2/2
A
A= ref $key
Aresult
11
00
196 4 $args = { cb => $key };
197 4 $key = 'with';
198 4 ▶ if((ref($options[0])||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $options[0] || "") eq "HASH") 2 2 4
Condition100%2/2
ref $options[0] || ''
l!l
22
MC/DC100%1/1
ref $options[0] || ''
ref $options[0]
Truth table100%2/2
A
A= ref $options[0]
Aresult
11
00
199 2 my $base_args = shift(@options);
200 2 $args = +{ %$args, %$base_args };
201 }
202 } else { # Otherwise its a normal validator with params
203 145 $args = shift(@options);
204 }
205  
206 150 ▶ if(_is_reserved_option_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_reserved_option_key($key)) { } 22 128 150
207 22 $global_options{$key} = $args;
208 } else {
209 128 push @validator_info, [$key, $args];
210 }
211 }
212  
213 103 my @validators = ();
214 103 foreach my $info(@validator_info) {
215 128 my ($package_part, $args) = @$info;
216 128 my $validator_package = $self->_validator_package($package_part);
217  
218 128 ▶ unless((ref($args)||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((ref $args || "") eq "HASH") 73 55 128
Condition100%2/2
ref $args || ''
l!l
9434
MC/DC100%1/1
ref $args || ''
ref $args
Truth table100%2/2
A
A= ref $args
Aresult
11
00
219 73 $args = $validator_package->normalize_shortcut($args);
220 73 ▶ throw_exception InvalidValidatorArgs => ( args => $args) unless ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 73 73
221 }
222  
223 # so strip out the reserved and if any wrap in a conditional that
224 # way we can remove all that stuff from each
225  
226 # merge global options into args
227 128 ▶ $args->{strict} = 1 if $global_options{strict} and !exists $args->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $global_options{"strict"} and not exists $args->{"strict"} 1 127 128
Condition66%2/3
$global_options{"strict"} and not exists $args->{"strict"}
!ll&&!rl&&r
12701
MC/DC50%1/2
$global_options{"strict"} and not exists $args->{"strict"}
$global_options{"strict"} not exists $args->{"strict"}
Truth table66%2/3
A and not(B)
A= $global_options{"strict"}B= not exists $args->{"strict"}
ABresult
0X0
100
111
228 128 ▶ $args->{allow_undef} = 1 if $global_options{allow_undef} and !exists $args->{allow_undef};
Branch: 1 branch
BranchTrueFalseTotal
if $global_options{"allow_undef"} and not exists $args->{"allow_undef"} 2 126 128
Condition66%2/3
$global_options{"allow_undef"} and not exists $args->{"allow_undef"}
!ll&&!rl&&r
12602
MC/DC50%1/2
$global_options{"allow_undef"} and not exists $args->{"allow_undef"}
$global_options{"allow_undef"} not exists $args->{"allow_undef"}
Truth table66%2/3
A and not(B)
A= $global_options{"allow_undef"}B= not exists $args->{"allow_undef"}
ABresult
0X0
100
111
229 128 ▶ $args->{allow_blank} = 1 if $global_options{allow_blank} and !exists $args->{allow_blank};
Branch: 1 branch
BranchTrueFalseTotal
if $global_options{"allow_blank"} and not exists $args->{"allow_blank"} 9 119 128
Condition66%2/3
$global_options{"allow_blank"} and not exists $args->{"allow_blank"}
!ll&&!rl&&r
11909
MC/DC50%1/2
$global_options{"allow_blank"} and not exists $args->{"allow_blank"}
$global_options{"allow_blank"} not exists $args->{"allow_blank"}
Truth table66%2/3
A and not(B)
A= $global_options{"allow_blank"}B= not exists $args->{"allow_blank"}
ABresult
0X0
100
111
230 128 ▶ $args->{message} = $global_options{message} if exists $global_options{message} and !exists $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
if exists $global_options{"message"} and not exists $args->{"message"} 3 125 128
Condition100%3/3
exists $global_options{"message"} and not exists $args->{"message"}
!ll&&!rl&&r
12323
MC/DC100%2/2
exists $global_options{"message"} and not exists $args->{"message"}
exists $global_options{"message"} not exists $args->{"message"}
Truth table100%3/3
A and not(B)
A= exists $global_options{"message"}B= not exists $args->{"message"}
ABresult
0X0
100
111
231  
232 128 foreach my $opt(qw(if unless on)) {
233 384 ▶ next unless my $val = $global_options{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless my $val = $global_options{$opt} 376 8 384
234 8 ▶ my @val = (ref($val)||'') eq 'ARRAY' ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
(ref $val || '') eq 'ARRAY' ? : 0 8 8
Condition100%2/2
ref $val || ''
l!l
44
MC/DC100%1/1
ref $val || ''
ref $val
Truth table100%2/2
A
A= ref $val
Aresult
11
00
235 8 ▶ if(exists $args->{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$opt}) 2 6 8
236 2 my $current = $args->{$opt};
237 2 ▶ my @current = (ref($current)||'') eq 'ARRAY' ? @$current : ($current);
Branch: 1 branch
BranchTrueFalseTotal
(ref $current || '') eq 'ARRAY' ? : 0 2 2
Condition100%2/2
ref $current || ''
l!l
11
MC/DC100%1/1
ref $current || ''
ref $current
Truth table100%2/2
A
A= ref $current
Aresult
11
00
238 2 @val = (@current, @val);
239 }
240 8 $args->{$opt} = \@val;
241 }
242
243 128 $args->{attributes} = $attributes;
244 128 $args->{model_class} = $self;
245  
246 128 my $new_validator = $self->_create_validator($validator_package, $args);
247 128 push @validators, $new_validator;
248 }
249 103 ▶ my $coderef = sub { $_->validate(@_) foreach @validators };
Subroutine __ANON__: called CC=2 SCAR=6.9
250 103 $self->_validates_coderef($coderef, $attributes, %global_options);
251 }
252  
253 133 ▶sub _normalize_validator_package {
Subroutine _normalize_validator_package: called CC=4 SCAR=13.9
254 133 my ($self, $with) = @_;
255 133 my ($prefix, $package) = ($with =~m/^(\+?)(.+)$/);
256 133 ▶ return $package if $prefix eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "+" 0 133 133
257  
258 133 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
191140
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
259 133 my @parts = ((split '::', $class), $package);
260 133 my @project_inc = ();
261 133 ▶ while(@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 500 133 633
262 500 push @project_inc, join '::', (@parts, $class->default_validator_namepart, $package);
263 500 pop @parts;
264 }
265 133 push @project_inc, join '::', $class->default_validator_namepart, $package; # Not sure we should allow (add flag?)
266 133 return @project_inc;
267 }
268  
269 15 ▶sub _strip_reserved_options {
Subroutine _strip_reserved_options: called CC=3 SCAR=11.0
270 15 my (%options) = @_;
271 15 my %reserved = ();
272 15 foreach my $key (keys %options) {
273 15 ▶ if(_is_reserved_option_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_reserved_option_key($key)) 5 10 15
274 5 $reserved{$key} = delete $options{$key};
275 }
276 }
277 15 return %reserved;
278 }
279  
280 15 ▶sub validates_with {
Subroutine __validates_with_for_exporter: called CC=9 SCAR=22.3
281 15 my ($self, $validators_proto, %options) = @_;
282 15 my %reserved = _strip_reserved_options(%options);
283 my @with = ref($validators_proto) eq 'ARRAY' ?
284 0 ▶ @{$validators_proto} : ($validators_proto);
Branch: 1 branch
BranchTrueFalseTotal
ref $validators_proto eq 'ARRAY' ? : 0 15 15
285  
286 15 my @validators = ();
287 15 VALIDATOR_WITHS: foreach my $with (@with) {
288 15 ▶ if( (ref($with)||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $with || "") eq "CODE") 10 5 15
Condition100%2/2
ref $with || ''
l!l
105
MC/DC100%1/1
ref $with || ''
ref $with
Truth table100%2/2
A
A= ref $with
Aresult
11
00
289 10 push @validators, [$with, \%options];
290 10 next VALIDATOR_WITHS;
291 }
292 5 debug 1, "Trying to find a validator for '$with'";
293 5 my @possible_packages = $self->_normalize_validator_package($with);
294 5 foreach my $package(@possible_packages) {
295 my $found_package = eval {
296 14 use_module($package);
297 14 ▶ } || do {
Condition66%2/3
eval { do { &use_module($package) } } || do { my $notional_filename = &Module::Runtime::module_notional_filename($package); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package, 'err', $@)); } }
l!l&&r!l&&!r
509
MC/DC50%1/2
eval { do { &use_module($package) } } || do { my $notional_filename = &Module::Runtime::module_notional_filename($package); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package, 'err', $@)); } }
eval { do { &use_module($package) } } do { my $notional_filename = &Module::Runtime::module_notional_filename($package); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package, 'err', $@)); } }
Truth table66%2/3
A || B
A= eval { do { &use_module($package) } }B= do { my $notional_filename = &Module::Runtime::module_notional_filename($package); if ($@ =~ /^Can't locate $notional_filename/) { debug(1, "Can't find '${package}' in \@INC"); 0; } else { throw_exception('UnexpectedUseModuleError', ('package', $package, 'err', $@)); } }
ABresult
1X1
011
000
298 9 my $notional_filename = Module::Runtime::module_notional_filename($package);
299 9 ▶ if($@=~m/^Can't locate $notional_filename/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Can't locate $notional_filename/) { } 9 0 9
300 9 debug 1, "Can't find '$package' in \@INC";
301 9 0;
302 } else {
303 # Probably a syntax error in the code of $package
304 0 throw_exception UnexpectedUseModuleError => (package => $package, err => $@);
305 }
306 };
307 14 ▶ if($found_package) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_package) 5 9 14
308 5 debug 1, "Found '$found_package' in \@INC";
309 5 push @validators, $package->new(%options);
310 5 next VALIDATOR_WITHS; # Only load the first one found
311 }
312 }
313 0 throw_exception General => (msg => "Failed to find validator for '$with' in \@INC");
314 }
315 15 my $collection = use_module($self->default_collection_class)
316 ->new(validators=>\@validators, %reserved);
317 15 ▶ $self->_validates_coderef(sub { $collection->validate(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
318 }
319  
320 105 ▶sub valid {
Subroutine valid: called CC=4 SCAR=13.9
Pod documented
321 105 my $self = shift;
322 105 ▶ $self->validate(@_) if @_ || !$self->validated;
Branch: 1 branch
BranchTrueFalseTotal
if @_ or not $self->validated 11 94 105
Condition100%3/3
@_ or not $self->validated
l!l&&r!l&&!r
11094
MC/DC100%2/2
@_ or not $self->validated
@_ not $self->validated
Truth table100%3/3
A or not(B)
A= @_B= not $self->validated
ABresult
1X1
011
000
323 105 ▶ return $self->errors->size ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
$self->errors->size ? : 80 25 105
324 }
325  
326 79 ▶sub invalid { shift->valid(@_) ? 0:1 }
Subroutine invalid: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->valid(@_) ? : 3 76 79
327  
328 8 ▶sub clear_validated {
Subroutine clear_validated: called CC=1 SCAR=0.0
Pod documented
329 8 my $self = shift;
330 8 $self->errors->clear;
331 8 $self->validated(0);
332 }
333  
334 122 ▶sub validate {
Subroutine validate: called CC=8 SCAR=20.8
Pod documented
335 122 my ($self, %args) = @_;
336  
337 122 ▶ my $existing_context = $args{context} ? $args{context} : [];
Branch: 1 branch
BranchTrueFalseTotal
$args{'context'} ? : 8 114 122
338 122 ▶ my @existing_context = ref($existing_context) ? @$existing_context : ($existing_context);
Branch: 1 branch
BranchTrueFalseTotal
ref $existing_context ? : 116 6 122
339 122 ▶ push @existing_context, @{$self->_context} if $self->has_context;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_context 1 121 122
340  
341 122 ▶ $args{context} = \@existing_context if @existing_context;
Branch: 1 branch
BranchTrueFalseTotal
if @existing_context 9 113 122
342  
343 122 ▶ return $self if $self->skip_validation;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_validation 1 121 122
344 121 ▶ return $self if $self->{_inprocess};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_inprocess"} 0 121 121
345 121 $self->{_inprocess} =1;
346  
347 121 ▶ $self->clear_validated if $self->validated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated 5 116 121
348 121 $self->_run_validations(%args);
349 118 $self->_run_post_validations(%args);
350 118 $self->validated(1);
351 118 delete $self->{_inprocess};
352 118 return $self;
353 }
354  
355 3 ▶sub validate_only {
Subroutine validate_only: called CC=13 SCAR=26.5
Pod documented
356 3 my ($self, $attribute, %args) = @_;
357 3 ▶ my @attributes = ref($attribute) ? @$attribute : ($attribute);
Branch: 1 branch
BranchTrueFalseTotal
ref $attribute ? : 1 2 3
358  
359 3 ▶ my $existing_context = $args{context} ? $args{context} : [];
Branch: 1 branch
BranchTrueFalseTotal
$args{'context'} ? : 0 3 3
360 3 ▶ my @existing_context = ref($existing_context) ? @$existing_context : ($existing_context);
Branch: 1 branch
BranchTrueFalseTotal
ref $existing_context ? : 3 0 3
361 0 ▶ push @existing_context, @{$self->_context} if $self->has_context;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_context 0 3 3
362  
363 3 ▶ $args{context} = \@existing_context if @existing_context;
Branch: 1 branch
BranchTrueFalseTotal
if @existing_context 0 3 3
364  
365 3 ▶ return $self if $self->skip_validation;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_validation 0 3 3
366 3 ▶ return $self if $self->{_inprocess};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_inprocess"} 0 3 3
367 3 $self->{_inprocess} =1;
368  
369 3 ▶ $self->clear_validated if $self->validated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validated 2 1 3
370  
371 3 foreach my $validation ($self->validations) {
372 9 ▶ next unless $validation->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $validation->[2] 0 9 9
373 9 my @attrs_for_validation = @{$validation->[2]};
374 9 foreach my $attr (@attrs_for_validation) {
375 9 ▶ next unless grep { $_ eq $attr } @attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $attr;} @attributes 5 4 9
376 4 $validation->[0]($self, +{%{$validation->[1]}, %args});
377 }
378 }
379  
380 3 $self->validated(1);
381 3 delete $self->{_inprocess};
382 3 return $self;
383 }
384  
385 121 ▶sub _run_validations {
Subroutine _run_validations: called CC=2 SCAR=6.9
386 121 my ($self, %args) = @_;
387 121 foreach my $validation ($self->validations) {
388 246 my %validation_args = (%{$validation->[1]}, %args);
389 246 $validation->[0]($self, \%validation_args);
390 }
391 }
392  
393 118 ▶sub _run_post_validations {
Subroutine _run_post_validations: called CC=1 SCAR=0.0
394 118 my ($self, %args) = @_;
395 118 return;
396 }
397  
398 1 ▶sub inject_attribute {
Subroutine inject_attribute: called CC=2 SCAR=7.1
Pod undocumented
399 1 my ($class, $attribute_to_inject) = @_;
400 1 eval "package $class; has $attribute_to_inject => (is=>'ro');";
401 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
402 }
403  
404 40 1;
405  
406 =head1 NAME
407  
408 Valiant::Validates - Role that adds class and instance methods supporting validations
409  
410 =head1 SYNOPSIS
411  
412 See L<Valiant>.
413  
414 =head1 DESCRIPTION
415  
416 This is a role that adds class level validations to you L<Moo> or L<Moose> classes.
417 The main point of entry for use and documentation currently is L<Valiant>. Here
418 we have API level documentation without details or examples. You should read L<Valiant>
419 first and then you can refer to documentation her for further details.
420  
421 In addition to methods this class provides, it also provides all methods from L<Valiant::Translation>
422  
423 =head1 CLASS METHODS
424  
425 =head2 validates
426  
427 Used to declare validations on an attribute. The first argument is either a scalar or arrayref of
428 scalars which should be attributes on your object:
429  
430 __PACKAGE__->validates( name => (...) );
431 __PACKAGE__->validates( ['name', 'age'] => (...));
432  
433 Following arguments should be in one of three forms: a coderef or subroutine reference that contains
434 validation rules, a key - value pair which is a validator class and its arguments or lastly you can
435 pass in a L<Type::Tiny> constraint directly. You may also have key value pairs which are global
436 arguments for the validate set as a whole:
437  
438 package Local::Model::User;
439  
440 use Moo;
441 use Valiant::Validations; # Importer that wraps Valiant::Validates
442 use Types::Standard 'Int';
443  
444 has ['name', 'age'] => (is=>'ro);
445  
446 validates name => (
447 length => { minimum => 5, maximum => 25 },
448 format => { match => 'words' },
449 sub {
450 my ($self, $attribute, $value, $opts) = @_;
451 $self->errors->add($attribute, "can't be Joe. We hate Joe :)" ,$opts) if $value eq 'Joe';
452 }, +{ arg1=>'1', arg2=>2 }, # args are optional for coderefs but are passed into $opts
453 \&must_be_unique,
454 );
455  
456 validates age => (
457 Int->where('$_ >= 65'), +{
458 message => 'A retiree must be at least 65 years old,
459 },
460 ..., # additional validations
461 );
462  
463 sub must_be_unique {
464 my ($self, $attribute, $value, $opts) = @_;
465 # Some local validation to make sure the name is unique in storage (like a database).
466 }
467  
468 If you use a validator class name then the hashref of arguments that follows is not optional. If you pass
469 an options hashref it should contain arguments that are defined for the validation type you are passing
470 or one of the global arguments C<on>, C<message>, C<if> and C<unless> (the shared parameters accepted by every validator).
471  
472 For subroutine reference and L<Type::Tiny> objects you can or not pass an options hashref depending on your
473 needs. Additionally the three types can be mixed and matched within a single C<validates> clause.
474  
475 When you use a validator class (such as C<length => { minimum => 5, maximum => 25 }>) we resolve the class
476 name C<length> in the following way. We first camel case the name and then look for a 'Validator' package
477 in the current class namespace. If we don't find a match we check each namespace up the hierarchy and
478 then check the two global namespaces C<Valiant::ValidatorX> and C<Validate::Validator>. For example if
479 you declare validators as in the example class above C<Local::Model::User> we would look for the following:
480  
481 Local::Model::User::Validator::Length
482 Local::Model:::Validator::Length
483 Local::Validator::Length
484 Validator::Length
485 Valiant::ValidatorX::Length
486 Valiant::Validator::Length
487  
488 These get checked in the order above and loaded and instantiated once at setup time.
489  
490 B<NOTE:> The namespace C<Valiant::Validator> is reserved for validators that ship with L<Valiant>. The
491 C<Valiant::ValidatorX> namespace is reserved for additional validators on CPAN that are packaged separately
492 from L<Valiant>. If you wish to share a custom validator that you wrote the proper namespace to use on
493 CPAN is C<Valiant::ValidatorX>.
494  
495 You can also prepend your validator name with '+' which will cause L<Valiant> to ignore the namespace
496 resolution and try to load the class directly. For example:
497  
498 validates_with '+App::MyValidator';
499  
500 Will try to load the class C<App::MyValidator> and use it as a validator directly (or throw an exception if
501 it fails to load).
502  
503 =head2 validates_with
504  
505 C<validates_with> is intended to process validations that are on the class as a whole, or which are very
506 complex and can't easily be assigned to a single attribute. It accepts either a subroutine reference
507 with an optional hash of key value pair options (which are passed to C<$opts>) or a scalar name which
508 should be a stand alone validator class (basically a class that does the C<validates> method although
509 you should consume the L<Validate::Validator> role to enforce the contract).
510  
511 __PACKAGE__->validates_with(sub {
512 my ($self, $opts) = @_;
513 ...
514 });
515  
516 __PACKAGE__->validates_with(\&check_object => (arg1=>'foo', arg2=>'bar'));
517  
518 sub check_object {
519 my ($self, $opts) = @_;
520 ...
521 }
522  
523 __PACKAGE__->validates with('Custom' => (arg1=>'foo', arg2=>'bar'));
524  
525 If you pass a string that is a validator class we resolve its namespace using the same approach as
526 detailed above for C<validates>. Any arguments are passed to the C<new> method of the found class
527 excluding global options.
528  
529 =head1 INSTANCE METHODS
530  
531 =head2 validate
532  
533 Run validation rules on the current object, optionally with arguments hash. If validation has already
534 been run on this object, we clear existing errors and run validations again. Currently the return
535 value of this method is not defined in the API. Example:
536  
537 $object->validate(%args);
538  
539 Currently the only arguments with defined meaning is C<context>, which is used to defined a validation
540 context. All other arguments will be passed down to the C<$opts> hashref.
541  
542 =head2 validate_only
543  
544 $obj->validate_only('name', context=>'create');
545 $obj->validate_only(['name', 'age'], context=>'update');
546  
547 Similar to C<validate> but will run only those validations that are associated with the named
548 attribute(s). Access either a single string or arrayref of strings. Please keep in mind that
549 fields or custom validations can add errors to other fields so you may still need to check all
550 fields for errors after running this method in order to get a full picture of the object validation
551 state. However this method can be of use if you are doing AJAX per field form validation, for
552 example.
553  
554 =head2 valid
555  
556 =head2 invalid
557  
558 Return true or false depending on if the current object state is valid or not. If you call this method and
559 validations have not been run (via C<validate>) then we will first run validations and pass any arguments
560 to L</validates>. If validations have already been run we just return true or false directly UNLESS you
561 pass arguments in which case we clear errors first and then rerun validations with the arguments before
562 returning true or false.
563  
564 =head2 has_errors
565  
566 Returns a boolean indicating if the object currently has errors. This does not run a validation
567 check first (unlike C<valid> or C<invalid>). So if you just want to check the current state of
568 the errors list and not tamper with that state you can use this.
569  
570 =head2 no_errors
571  
572 The opposite of L</has_errors>: returns true if the object currently has no errors. Like
573 C<has_errors> this does not run a validation check first.
574  
575 =head2 get_context
576  
577 Returns the current validation context (an arrayref) as set via C<context>, or undef if
578 no context has been set.
579  
580 =head2 csrf_token
581  
582 $object->csrf_token($token); # set (returns $object for chaining)
583 my $token = $object->csrf_token; # get
584  
585 Get or set a CSRF token on the object. This is glue for web frameworks (used for example
586 by the form generation code) and has no effect on validation itself.
587  
588 =head2 clear_validated
589  
590 Clears any errors and sets the object as though validations hd never been run.
591  
592 =head2 do_validate
593  
594 Sets C<skip_validation> to false and returns C<$self>
595  
596 =head2 skip_validate
597  
598 Sets C<skip_validation> to true and returns C<$self>
599  
600 =head2 context
601  
602 Set a validation context (or arrayref of contexts) that will be used on any following validations.
603  
604 =head1 ATTRIBUTES
605  
606 =head2 errors
607  
608 An instance of L<Valiant::Errors>.
609  
610 =head2 validated
611  
612 This attribute will be true if validations have already been been run on the current instance. It
613 merely says if validations have been run or not, it does not indicate if validations have been passed
614 or failed see L</valid> pr L</invalid>
615  
616 =head2 skip_validation
617  
618 When true do not run validations, even if you call ->validate
619  
620 =head1 AUTHOR
621
622 John Napiorkowski L<email:jjnapiork@cpan.org>
623
624 =head1 SEE ALSO
625
626 L<Valiant::Validations>
627  
628 =head1 COPYRIGHT & LICENSE
629
630 Copyright 2020, John Napiorkowski L<email:jjnapiork@cpan.org>
631
632 This library is free software; you can redistribute it and/or modify it under
633 the same terms as Perl itself.
634  
635 =cut
636  
« Valiant/HTML/Util/Form.pm ↑ Summary Valiant/HTML/Util/TagBuilder.pm »