blib/lib/Venus/Validate.pm

filter: Statementstmt 92.0% 255 / 277 Branchbran 82.8% 121 / 146 Conditioncond 60.4% 61 / 101 MC/DCmcdc 37.2% 22 / 59 Subroutinesub 100% 44 / 44 Podpod 90.0% 36 / 40 totaltotal 80.8% 539 / 667 CC 110 SCAR 20.5
CC
110
Cov
83%
max_number
18.8
min_number
18.8
max_length
15.2
CRAP 7.8
« blib/lib/Venus/Run.pm ↑ Summary blib/lib/Venus/Error.pm »
1 package Venus::Validate;
2  
3 7 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 7 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 7 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 7 with 'Venus::Role::Buildable';
19 7 with 'Venus::Role::Encaseable';
20  
21 # ATTRIBUTES
22  
23 7 attr 'input';
24 7 attr 'issue';
25 7 attr 'path';
26  
27 # BUILDERS
28  
29 89 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
30 89 my ($self, $data) = @_;
31  
32 return {
33 89 input => [$data],
34 };
35 }
36  
37 375 ▶sub build_args {
Subroutine build_args: called CC=6 SCAR=17.9
Pod undocumented
38 375 my ($self, $data) = @_;
39  
40 375 ▶ if (!grep CORE::exists $data->{$_}, qw(input issue path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep exists $data->{$_}, ("input", "issue", "path")) 18 357 375
41 18 ▶ $data = {input => [$data]} if keys %{$data};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$data;} 15 3 18
42 }
43  
44 375 ▶ if (CORE::exists $data->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'input'}) { } 372 3 375
45 $data->{input} = ref $data->{input} eq 'ARRAY'
46 341 ? ((@{$data->{input}} > 1) ? [$data->{input}] : $data->{input})
47 372 ▶ : [$data->{input}];
Branch: 2 branches
BranchTrueFalseTotal
ref $data->{'input'} eq 'ARRAY' ? : 341 31 372
@{$data->{'input'};} > 1 ? : 3 338 341
48 }
49 else {
50 3 $data->{input} = [];
51 }
52  
53 375 return $data;
54 }
55  
56 # METHODS
57  
58 3 ▶sub arrayref {
Subroutine arrayref: called CC=1 SCAR=0.0
Pod documented
59 3 my ($self) = @_;
60  
61 3 $self->type('arrayref');
62  
63 3 return $self;
64 }
65  
66 4 ▶sub boolean {
Subroutine boolean: called CC=1 SCAR=0.0
Pod documented
67 4 my ($self) = @_;
68  
69 4 $self->type('boolean');
70  
71 4 return $self;
72 }
73  
74 4 ▶sub check {
Subroutine check: called CC=9 SCAR=24.0
Pod documented
75 4 my ($self, $type, $name, $code, @args) = @_;
76  
77 4 ▶ return $self if $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue 0 4 4
78  
79 4 ▶ return $self if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 3 4
80  
81 3 $self->type($type);
82  
83 3 ▶ return $self if $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue 0 3 3
84  
85 3 ▶ return $self if !$code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 1 2 3
86  
87 2 my $value = $self->value;
88  
89 2 ▶ if ((!CORE::defined($value) || $value eq '') && $self->presence eq 'required') {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($value) || $value eq "" and $self->presence eq "required") 0 2 2
Condition33%2/6
!defined($value) || $value eq "" and $self->presence eq "required"
!ll&&!rl&&r
200
not defined $value or $value eq ""
l!l&&r!l&&!r
002
MC/DC0%0/3
!defined($value) || $value eq "" and $self->presence eq "required"
not defined $value $value eq "" $self->presence eq "required"
Truth table20%1/5
not(A) or B and C
A= not defined $valueB= $value eq ""C= $self->presence eq "required"
ABCresult
00X0
1X00
0100
1X11
0111
90 0 $self->issue_info($name, @args);
91  
92 0 return $self;
93 }
94  
95 2 local $_ = $value;
96  
97 2 ▶ $self->issue_info($name, @args) if !$self->$code($value, @args);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$code($value, @args) 1 1 2
98  
99 2 return $self;
100 }
101  
102 3 ▶sub defined {
Subroutine defined: called CC=1 SCAR=0.0
Pod documented
103 3 my ($self) = @_;
104  
105 3 $self->type('defined');
106  
107 3 return $self;
108 }
109  
110 156 ▶sub each {
Subroutine each: called CC=9 SCAR=22.0
Pod documented
111 156 my ($self, $method, $path) = @_;
112  
113 156 my $result = [];
114  
115 156 ▶ $method ||= 'optional';
Condition100%2/2
$method ||= "optional"
l!l
1542
MC/DC100%1/1
$method ||= "optional"
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
116  
117 156 ▶ my $node = (
Branch: 1 branch
BranchTrueFalseTotal
defined $path && $method eq 'optional' || $method eq 'present' || $method eq 'required' ? : 18 138 156
Condition88%8/9
defined $path and $method eq "optional" or $method eq "present" or $method eq "required"
l!l&&r!l&&!r
513138
defined $path and $method eq "optional" or $method eq "present"
l!l&&r!l&&!r
50151
defined $path and $method eq "optional"
!ll&&!rl&&r
14475
MC/DC50%2/4
defined $path and $method eq "optional" or $method eq "present" or $method eq "required"
defined $path $method eq "optional" $method eq "present" $method eq "required"
Truth table57%4/7
A and B or C or D
A= defined $pathB= $method eq "optional"C= $method eq "present"D= $method eq "required"
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
118 (CORE::defined($path))
119 && $method eq 'optional'
120 || $method eq 'present'
121 || $method eq 'required'
122 ) ? $self->$method($path) : $self;
123  
124 156 my $value = $node->value;
125  
126 156 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 9 147 156
127 9 push @{$result}, $node->$method($_) for 0..$#${value};
128 }
129 else {
130 147 push @{$result}, $node;
131 }
132  
133 156 ▶ return wantarray ? @{$result} : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 144 156
134 }
135  
136 429 ▶sub errors {
Subroutine errors: called CC=4 SCAR=14.1
Pod documented
137 429 my ($self, $data) = @_;
138  
139 429 my $errors = $self->encased('errors');
140  
141 429 ▶ $errors = $self->recase('errors', ref $data eq 'ARRAY' ? $data : []) if !$errors;
Branch: 2 branches
BranchTrueFalseTotal
unless $errors 179 250 429
ref $data eq 'ARRAY' ? : 129 50 179
142  
143 0 ▶ return wantarray ? @{$errors} : $errors;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 429 429
144 }
145  
146 3 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
147 3 my ($self) = @_;
148  
149 3 my @value = $self->value;
150  
151 3 ▶ return @value ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
@value ? : 2 1 3
152 }
153  
154 3 ▶sub float {
Subroutine float: called CC=1 SCAR=0.0
Pod documented
155 3 my ($self) = @_;
156  
157 3 $self->type('float');
158  
159 3 return $self;
160 }
161  
162 3 ▶sub hashref {
Subroutine hashref: called CC=1 SCAR=0.0
Pod documented
163 3 my ($self) = @_;
164  
165 3 $self->type('hashref');
166  
167 3 return $self;
168 }
169  
170 4 ▶sub is_invalid {
Subroutine is_invalid: called CC=2 SCAR=6.9
Pod documented
171 4 my ($self) = @_;
172  
173 4 ▶ my $invalid = $self->is_valid ? false : true;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid ? : 1 3 4
174  
175 4 return $invalid;
176 }
177  
178 75 ▶sub is_valid {
Subroutine is_valid: called CC=2 SCAR=6.9
Pod documented
179 75 my ($self) = @_;
180  
181 75 ▶ my $valid = $self->issue ? false : true;
Branch: 1 branch
BranchTrueFalseTotal
$self->issue ? : 25 50 75
182  
183 75 return $valid;
184 }
185  
186 4 ▶sub issue_args {
Subroutine issue_args: called CC=2 SCAR=6.9
Pod documented
187 4 my ($self) = @_;
188  
189 4 my $issue = $self->issue;
190  
191 4 ▶ my $args = ref $issue eq 'ARRAY' ? $issue->[1] : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $issue eq 'ARRAY' ? : 3 1 4
192  
193 4 return $args;
194 }
195  
196 55 ▶sub issue_info {
Subroutine issue_info: called CC=4 SCAR=13.9
Pod documented
197 55 my ($self, $type, @args) = @_;
198  
199 55 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 52 3 55
200 52 $self->issue([$type, [@args]]);
201  
202 52 ▶ push @{$self->errors}, [$self->path, $self->issue] if ref $self->errors eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->errors eq "ARRAY" 52 0 52
203 }
204 else {
205 3 ($type, @args) = ($self->issue_type, @{$self->issue_args});
206 }
207  
208 55 ▶ return wantarray ? ($type, @args) : $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 54 55
209 }
210  
211 4 ▶sub issue_type {
Subroutine issue_type: called CC=2 SCAR=6.9
Pod documented
212 4 my ($self) = @_;
213  
214 4 my $issue = $self->issue;
215  
216 4 ▶ my $type = ref $issue eq 'ARRAY' ? $issue->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $issue eq 'ARRAY' ? : 3 1 4
217  
218 4 return $type;
219 }
220  
221 2 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
222 2 my ($self, $min_length, $max_length) = @_;
223  
224 2 $self->min_length($min_length);
225  
226 2 $self->max_length($max_length);
227  
228 2 return $self;
229 }
230  
231 1 ▶sub lowercase {
Subroutine lowercase: called CC=3 SCAR=11.8
Pod documented
232 1 my ($self) = @_;
233  
234 1 my $value = $self->value;
235  
236 1 ▶ if (CORE::defined($value) && !ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 1 0 1
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
237 1 $self->value(lc $value);
238 }
239  
240 1 return $self;
241 }
242  
243 4 ▶sub max_length {
Subroutine max_length: called CC=9 SCAR=27.2
Pod documented
244 4 my ($self, $length) = @_;
245  
246 4 ▶ return $self if $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue 1 3 4
247  
248 3 my $value = $self->value;
249  
250 3 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 3 3
251 0 $self->issue_info('max_length', $length);
252  
253 0 return $self;
254 }
255  
256 3 ▶ if (!CORE::defined($value) && $self->presence eq 'optional') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $self->presence eq "optional") 0 3 3
Condition33%1/3
not defined $value and $self->presence eq "optional"
!ll&&!rl&&r
300
MC/DC0%0/2
not defined $value and $self->presence eq "optional"
not defined $value $self->presence eq "optional"
Truth table33%1/3
not(A) and B
A= not defined $valueB= $self->presence eq "optional"
ABresult
0X0
100
111
257 0 return $self;
258 }
259  
260 3 ▶ if ((!CORE::defined($value) || $value eq '') && $self->presence eq 'required') {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($value) || $value eq "" and $self->presence eq "required") 0 3 3
Condition33%2/6
!defined($value) || $value eq "" and $self->presence eq "required"
!ll&&!rl&&r
300
not defined $value or $value eq ""
l!l&&r!l&&!r
003
MC/DC0%0/3
!defined($value) || $value eq "" and $self->presence eq "required"
not defined $value $value eq "" $self->presence eq "required"
Truth table20%1/5
not(A) or B and C
A= not defined $valueB= $value eq ""C= $self->presence eq "required"
ABCresult
00X0
1X00
0100
1X11
0111
261 0 $self->issue_info('max_length', $length);
262  
263 0 return $self;
264 }
265  
266 3 ▶ if (CORE::length($value) > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $value > $length) 2 1 3
267 2 $self->issue_info('max_length', $length);
268 }
269  
270 3 return $self;
271 }
272  
273 2 ▶sub max_number {
Subroutine max_number: called CC=10 SCAR=29.3
Pod documented
274 2 my ($self, $maximum) = @_;
275  
276 2 ▶ return $self if $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue 0 2 2
277  
278 2 my $value = $self->value;
279  
280 2 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 2 2
281 0 $self->issue_info('max_number', $maximum);
282  
283 0 return $self;
284 }
285  
286 2 ▶ if (!CORE::defined($value) && $self->presence eq 'optional') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $self->presence eq "optional") 0 2 2
Condition33%1/3
not defined $value and $self->presence eq "optional"
!ll&&!rl&&r
200
MC/DC0%0/2
not defined $value and $self->presence eq "optional"
not defined $value $self->presence eq "optional"
Truth table33%1/3
not(A) and B
A= not defined $valueB= $self->presence eq "optional"
ABresult
0X0
100
111
287 0 return $self;
288 }
289  
290 2 ▶ if ((!CORE::defined($value) || $value eq '') && $self->presence eq 'required') {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($value) || $value eq "" and $self->presence eq "required") 0 2 2
Condition33%2/6
!defined($value) || $value eq "" and $self->presence eq "required"
!ll&&!rl&&r
200
not defined $value or $value eq ""
l!l&&r!l&&!r
002
MC/DC0%0/3
!defined($value) || $value eq "" and $self->presence eq "required"
not defined $value $value eq "" $self->presence eq "required"
Truth table20%1/5
not(A) or B and C
A= not defined $valueB= $value eq ""C= $self->presence eq "required"
ABCresult
00X0
1X00
0100
1X11
0111
291 0 $self->issue_info('max_number', $maximum);
292  
293 0 return $self;
294 }
295  
296 2 ▶ if ($value !~ /^-?\d+\.?\d*$/ || $value > $maximum) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value =~ /^-?\d+\.?\d*$/u or $value > $maximum) 1 1 2
Condition66%2/3
not $value =~ /^-?\d+\.?\d*$/u or $value > $maximum
l!l&&r!l&&!r
011
MC/DC50%1/2
not $value =~ /^-?\d+\.?\d*$/u or $value > $maximum
not $value =~ /^-?\d+\.?\d*$/u $value > $maximum
Truth table66%2/3
not(A) or B
A= not $value =~ /^-?\d+\.?\d*$/uB= $value > $maximum
ABresult
1X1
011
000
297 1 $self->issue_info('max_number', $maximum);
298 }
299  
300 2 return $self;
301 }
302  
303 7 ▶sub min_length {
Subroutine min_length: called CC=9 SCAR=24.2
Pod documented
304 7 my ($self, $length) = @_;
305  
306 7 ▶ return $self if $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue 0 7 7
307  
308 7 my $value = $self->value;
309  
310 7 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 7 7
311 0 $self->issue_info('min_length', $length);
312  
313 0 return $self;
314 }
315  
316 7 ▶ if (!CORE::defined($value) && $self->presence eq 'optional') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $self->presence eq "optional") 0 7 7
Condition33%1/3
not defined $value and $self->presence eq "optional"
!ll&&!rl&&r
700
MC/DC0%0/2
not defined $value and $self->presence eq "optional"
not defined $value $self->presence eq "optional"
Truth table33%1/3
not(A) and B
A= not defined $valueB= $self->presence eq "optional"
ABresult
0X0
100
111
317 0 return $self;
318 }
319  
320 7 ▶ if ((!CORE::defined($value) || $value eq '') && $self->presence eq 'required') {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($value) || $value eq "" and $self->presence eq "required") 2 5 7
Condition66%4/6
!defined($value) || $value eq "" and $self->presence eq "required"
!ll&&!rl&&r
502
not defined $value or $value eq ""
l!l&&r!l&&!r
025
MC/DC33%1/3
!defined($value) || $value eq "" and $self->presence eq "required"
not defined $value $value eq "" $self->presence eq "required"
Truth table40%2/5
not(A) or B and C
A= not defined $valueB= $value eq ""C= $self->presence eq "required"
ABCresult
00X0
1X00
0100
1X11
0111
321 2 $self->issue_info('min_length', $length);
322  
323 2 return $self;
324 }
325  
326 5 ▶ if (CORE::length($value) < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $value < $length) 3 2 5
327 3 $self->issue_info('min_length', $length);
328 }
329  
330 5 return $self;
331 }
332  
333 2 ▶sub min_number {
Subroutine min_number: called CC=10 SCAR=29.3
Pod documented
334 2 my ($self, $minimum) = @_;
335  
336 2 ▶ return $self if $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue 0 2 2
337  
338 2 my $value = $self->value;
339  
340 2 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 2 2
341 0 $self->issue_info('min_number', $minimum);
342  
343 0 return $self;
344 }
345  
346 2 ▶ if (!CORE::defined($value) && $self->presence eq 'optional') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $self->presence eq "optional") 0 2 2
Condition33%1/3
not defined $value and $self->presence eq "optional"
!ll&&!rl&&r
200
MC/DC0%0/2
not defined $value and $self->presence eq "optional"
not defined $value $self->presence eq "optional"
Truth table33%1/3
not(A) and B
A= not defined $valueB= $self->presence eq "optional"
ABresult
0X0
100
111
347 0 return $self;
348 }
349  
350 2 ▶ if ((!CORE::defined($value) || $value eq '') && $self->presence eq 'required') {
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($value) || $value eq "" and $self->presence eq "required") 0 2 2
Condition33%2/6
!defined($value) || $value eq "" and $self->presence eq "required"
!ll&&!rl&&r
200
not defined $value or $value eq ""
l!l&&r!l&&!r
002
MC/DC0%0/3
!defined($value) || $value eq "" and $self->presence eq "required"
not defined $value $value eq "" $self->presence eq "required"
Truth table20%1/5
not(A) or B and C
A= not defined $valueB= $value eq ""C= $self->presence eq "required"
ABCresult
00X0
1X00
0100
1X11
0111
351 0 $self->issue_info('min_number', $minimum);
352  
353 0 return $self;
354 }
355  
356 2 ▶ if ($value !~ /^-?\d+\.?\d*$/ || $value < $minimum) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value =~ /^-?\d+\.?\d*$/u or $value < $minimum) 1 1 2
Condition66%2/3
not $value =~ /^-?\d+\.?\d*$/u or $value < $minimum
l!l&&r!l&&!r
011
MC/DC50%1/2
not $value =~ /^-?\d+\.?\d*$/u or $value < $minimum
not $value =~ /^-?\d+\.?\d*$/u $value < $minimum
Truth table66%2/3
not(A) or B
A= not $value =~ /^-?\d+\.?\d*$/uB= $value < $minimum
ABresult
1X1
011
000
357 1 $self->issue_info('min_number', $minimum);
358 }
359  
360 2 return $self;
361 }
362  
363 12 ▶sub number {
Subroutine number: called CC=1 SCAR=0.0
Pod documented
364 12 my ($self) = @_;
365  
366 12 $self->type('number');
367  
368 12 return $self;
369 }
370  
371 2 ▶sub on_invalid {
Subroutine on_invalid: called CC=3 SCAR=11.0
Pod documented
372 2 my ($self, $code, @args) = @_;
373  
374 2 ▶ return $self if !$code || !$self->issue;
Branch: 1 branch
BranchTrueFalseTotal
unless $code and $self->issue 1 1 2
Condition66%2/3
$code and $self->issue
!ll&&!rl&&r
101
MC/DC50%1/2
$code and $self->issue
$code $self->issue
Truth table66%2/3
A and B
A= $codeB= $self->issue
ABresult
0X0
100
111
375  
376 1 local $_ = $self;
377  
378 1 return $self->$code(@args);
379 }
380  
381 2 ▶sub on_valid {
Subroutine on_valid: called CC=3 SCAR=11.0
Pod documented
382 2 my ($self, $code, @args) = @_;
383  
384 2 ▶ return $self if !$code || $self->issue;
Branch: 1 branch
BranchTrueFalseTotal
if not $code or $self->issue 1 1 2
Condition66%2/3
not $code or $self->issue
l!l&&r!l&&!r
101
MC/DC50%1/2
not $code or $self->issue
not $code $self->issue
Truth table66%2/3
not(A) or B
A= not $codeB= $self->issue
ABresult
1X1
011
000
385  
386 1 local $_ = $self;
387  
388 1 return $self->$code(@args);
389 }
390  
391 26 ▶sub optional {
Subroutine optional: called CC=2 SCAR=6.9
Pod documented
392 26 my ($self, $path) = @_;
393  
394 26 ▶ if (!CORE::defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 1 25 26
395 1 $self->recase('presence', 'optional');
396  
397 1 return $self;
398 }
399  
400 25 my $node = $self->select($path);
401  
402 25 $node->recase('presence', 'optional');
403  
404 25 return $node;
405 }
406  
407 96 ▶sub pointer {
Subroutine pointer: called CC=1 SCAR=0.0
Pod undocumented
408 96 my ($self, @path) = @_;
409  
410 96 my $pointer = join '.', grep defined, $self->path, @path;
411  
412 96 return $pointer;
413 }
414  
415 8 ▶sub presence {
Subroutine presence: called CC=2 SCAR=6.9
Pod undocumented
416 8 my ($self) = @_;
417  
418 8 my $presence = $self->encased('presence');
419  
420 8 ▶ return $presence || 'required';
Condition100%2/2
$presence || 'required'
l!l
35
MC/DC100%1/1
$presence || 'required'
$presence
Truth table100%2/2
A
A= $presence
Aresult
11
00
421 }
422  
423 4 ▶sub present {
Subroutine present: called CC=3 SCAR=11.0
Pod documented
424 4 my ($self, $path) = @_;
425  
426 4 ▶ my $node = CORE::defined($path) ? $self->select($path) : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 4 0 4
427  
428 4 $node->recase('presence', 'present');
429  
430 4 my @value = $node->value;
431  
432 4 ▶ $node->issue_info('present') if !@value;
Branch: 1 branch
BranchTrueFalseTotal
unless @value 2 2 4
433  
434 4 return $node;
435 }
436  
437 64 ▶sub required {
Subroutine required: called CC=4 SCAR=13.9
Pod documented
438 64 my ($self, $path) = @_;
439  
440 64 ▶ my $node = CORE::defined($path) ? $self->select($path) : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 58 6 64
441  
442 64 $node->recase('presence', 'required');
443  
444 64 my @value = $node->value;
445  
446 64 ▶ return $node if @value && scalar grep defined, grep CORE::length, @value;
Branch: 1 branch
BranchTrueFalseTotal
if @value and scalar grep(defined($_), grep(CORE::length($_), @value)) 49 15 64
Condition100%2/2
@value and scalar grep(defined($_), grep(CORE::length($_), @value))
!ll
856
MC/DC100%1/1
@value and scalar grep(defined($_), grep(CORE::length($_), @value))
@value
Truth table100%2/2
A
A= @value
Aresult
00
11
447  
448 15 $node->issue_info('required');
449  
450 15 return $node;
451 }
452  
453 97 ▶sub select {
Subroutine select: called CC=9 SCAR=22.0
Pod documented
454 97 my ($self, $path) = @_;
455  
456 97 ▶ if (!CORE::defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 1 96 97
457 1 return $self;
458 }
459  
460 96 my @value = $self->value;
461  
462 96 my $object;
463  
464 96 ▶ if (!$object && ref $value[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $object and ref $value[0] eq "ARRAY") 26 70 96
Condition66%2/3
not $object and ref $value[0] eq "ARRAY"
!ll&&!rl&&r
07026
MC/DC50%1/2
not $object and ref $value[0] eq "ARRAY"
not $object ref $value[0] eq "ARRAY"
Truth table66%2/3
not(A) and B
A= not $objectB= ref $value[0] eq "ARRAY"
ABresult
0X0
100
111
465 26 require Venus::Array;
466 26 $object = Venus::Array->new(value => $value[0]);
467 }
468  
469 96 ▶ if (!$object && ref $value[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $object and ref $value[0] eq "HASH") 67 29 96
Condition100%3/3
not $object and ref $value[0] eq "HASH"
!ll&&!rl&&r
26367
MC/DC100%2/2
not $object and ref $value[0] eq "HASH"
not $object ref $value[0] eq "HASH"
Truth table100%3/3
not(A) and B
A= not $objectB= ref $value[0] eq "HASH"
ABresult
0X0
100
111
470 67 require Venus::Hash;
471 67 $object = Venus::Hash->new(value => $value[0]);
472 }
473  
474 96 ▶ my ($data, $okay) = ($object ? ($object->path($path)) : (undef, 0));
Branch: 1 branch
BranchTrueFalseTotal
$object ? : 93 3 96
475  
476 96 ▶ my $node = $self->class->new(path => $self->pointer($path), input => [$okay ? $data : ()]);
Branch: 1 branch
BranchTrueFalseTotal
$okay ? : 81 15 96
477  
478 96 ▶ $node->errors(scalar $self->errors) if $self->errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->errors 96 0 96
479  
480 96 return $node;
481 }
482  
483 47 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
484 47 my ($self) = @_;
485  
486 47 $self->type('string');
487  
488 47 return $self;
489 }
490  
491 14 ▶sub strip {
Subroutine strip: called CC=3 SCAR=11.0
Pod documented
492 14 my ($self) = @_;
493  
494 14 my $value = $self->value;
495  
496 14 ▶ if (CORE::defined($value) && !ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 10 4 14
Condition100%3/3
defined $value and not ref $value
!ll&&!rl&&r
3110
MC/DC100%2/2
defined $value and not ref $value
defined $value not ref $value
Truth table100%3/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
497 10 $value =~ s/\s{2,}/ /g;
498 10 $self->value($value);
499 }
500  
501 14 return $self;
502 }
503  
504 57 ▶sub sync {
Subroutine sync: called CC=8 SCAR=20.9
Pod documented
505 57 my ($self, $node) = @_;
506  
507 57 ▶ return $self if !$node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 1 56 57
508  
509 56 my @value = $node->value;
510  
511 56 ▶ return $self if !@value;
Branch: 1 branch
BranchTrueFalseTotal
unless @value 9 47 56
512  
513 47 @value = $self->value;
514  
515 47 my $object;
516  
517 47 ▶ if (!$object && ref $value[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $object and ref $value[0] eq "ARRAY") 1 46 47
Condition66%2/3
not $object and ref $value[0] eq "ARRAY"
!ll&&!rl&&r
0461
MC/DC50%1/2
not $object and ref $value[0] eq "ARRAY"
not $object ref $value[0] eq "ARRAY"
Truth table66%2/3
not(A) and B
A= not $objectB= ref $value[0] eq "ARRAY"
ABresult
0X0
100
111
518 1 require Venus::Array;
519 1 $object = Venus::Array->new(value => $value[0]);
520 }
521  
522 47 ▶ if (!$object && ref $value[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $object and ref $value[0] eq "HASH") 46 1 47
Condition66%2/3
not $object and ref $value[0] eq "HASH"
!ll&&!rl&&r
1046
MC/DC50%1/2
not $object and ref $value[0] eq "HASH"
not $object ref $value[0] eq "HASH"
Truth table66%2/3
not(A) and B
A= not $objectB= ref $value[0] eq "HASH"
ABresult
0X0
100
111
523 46 require Venus::Hash;
524 46 $object = Venus::Hash->new(value => $value[0]);
525 }
526  
527 47 ▶ if (!$object) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object) 0 47 47
528 0 return $self;
529 }
530  
531 47 $object->sets($node->path, $node->value);
532  
533 47 return $self;
534 }
535  
536 1 ▶sub titlecase {
Subroutine titlecase: called CC=3 SCAR=11.6
Pod documented
537 1 my ($self) = @_;
538  
539 1 my $value = $self->value;
540  
541 1 ▶ if (CORE::defined($value) && !ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 1 0 1
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
542 1 $value =~ s/\b(\w)/\U$1/g;
543 1 $self->value($value);
544 }
545  
546 1 return $self;
547 }
548  
549 15 ▶sub trim {
Subroutine trim: called CC=3 SCAR=11.0
Pod documented
550 15 my ($self) = @_;
551  
552 15 my $value = $self->value;
553  
554 15 ▶ if (CORE::defined($value) && !ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 11 4 15
Condition100%3/3
defined $value and not ref $value
!ll&&!rl&&r
3111
MC/DC100%2/2
defined $value and not ref $value
defined $value not ref $value
Truth table100%3/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
555 11 $value =~ s/^\s+|\s+$//g;
556 11 $self->value($value);
557 }
558  
559 15 return $self;
560 }
561  
562 117 ▶sub type {
Subroutine type: called CC=6 SCAR=17.9
Pod documented
563 117 my ($self, $expr) = @_;
564  
565 117 ▶ if ($self->issue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->issue) 12 105 117
566 12 return $self
567 }
568  
569 105 my $value = $self->value;
570  
571 105 ▶ if (!CORE::defined($value) && $self->presence eq 'optional') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $self->presence eq "optional") 3 102 105
Condition100%3/3
not defined $value and $self->presence eq "optional"
!ll&&!rl&&r
9933
MC/DC100%2/2
not defined $value and $self->presence eq "optional"
not defined $value $self->presence eq "optional"
Truth table100%3/3
not(A) and B
A= not defined $valueB= $self->presence eq "optional"
ABresult
0X0
100
111
572 3 return $self;
573 }
574  
575 102 ▶ $expr ||= 'any';
Condition100%2/2
$expr ||= "any"
l!l
1011
MC/DC100%1/1
$expr ||= "any"
$expr
Truth table100%2/2
A
A= $expr
Aresult
11
00
576  
577 102 require Venus::Type;
578  
579 102 my $type = Venus::Type->new;
580  
581 102 my $passed = $type->check($expr)->eval($value);
582  
583 102 ▶ $self->issue_info('type', $expr) if !$passed;
Branch: 1 branch
BranchTrueFalseTotal
unless $passed 22 80 102
584  
585 102 return $self;
586 }
587  
588 1 ▶sub uppercase {
Subroutine uppercase: called CC=3 SCAR=11.8
Pod documented
589 1 my ($self) = @_;
590  
591 1 my $value = $self->value;
592  
593 1 ▶ if (CORE::defined($value) && !ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value) 1 0 1
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
594 1 $self->value(uc $value);
595 }
596  
597 1 return $self;
598 }
599  
600 766 ▶sub value {
Subroutine value: called CC=3 SCAR=11.0
Pod documented
601 766 my ($self, @args) = @_;
602  
603 766 ▶ my $input = @args ? $self->input([@args]) : $self->input;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 28 738 766
604  
605 766 ▶ return wantarray ? (@{$input}) : $input->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 326 440 766
606 }
607  
608 5 ▶sub yesno {
Subroutine yesno: called CC=1 SCAR=0.0
Pod documented
609 5 my ($self) = @_;
610  
611 5 $self->type('yesno');
612  
613 5 return $self;
614 }
615  
616 7 1;
617  
618  
619  
620 =head1 NAME
621  
622 Venus::Validate - Validate Class
623  
624 =cut
625  
626 =head1 ABSTRACT
627  
628 Validate Class for Perl 5
629  
630 =cut
631  
632 =head1 SYNOPSIS
633  
634 package main;
635  
636 use Venus::Validate;
637  
638 my $validate = Venus::Validate->new('hello');
639  
640 # $validate->string->trim->strip;
641  
642 # bless(..., "Venus::Validate")
643  
644 # $validate->is_valid;
645  
646 # true
647  
648 =cut
649  
650 =head1 DESCRIPTION
651  
652 This package provides a mechanism for performing data validation of simple and
653 hierarchal data at runtime.
654  
655 =cut
656  
657 =head1 ATTRIBUTES
658  
659 This package has the following attributes:
660  
661 =cut
662  
663 =head2 issue
664  
665 issue(arrayref $issue) (arrayref)
666  
667 The issue attribute is read/write, accepts C<(arrayref)> values, and is
668 optional.
669  
670 I<Since C<4.15>>
671  
672 =over 4
673  
674 =item issue example 1
675  
676 # given: synopsis;
677  
678 my $issue = $validate->issue([]);
679  
680 # []
681  
682 =back
683  
684 =over 4
685  
686 =item issue example 2
687  
688 # given: synopsis;
689  
690 # given: example-1 issue;
691  
692 $issue = $validate->issue;
693  
694 # []
695  
696 =back
697  
698 =cut
699  
700 =head2 path
701  
702 path(string $path) (string)
703  
704 The path attribute is read/write, accepts C<(string)> values, is optional, and
705 defaults to C<".">.
706  
707 I<Since C<4.15>>
708  
709 =over 4
710  
711 =item path example 1
712  
713 # given: synopsis;
714  
715 my $path = $validate->path('name');
716  
717 # "name"
718  
719 =back
720  
721 =over 4
722  
723 =item path example 2
724  
725 # given: synopsis;
726  
727 # given: example-1 path;
728  
729 $path = $validate->path;
730  
731 # "name"
732  
733 =back
734  
735 =cut
736  
737 =head2 input
738  
739 input(arrayref $input) (arrayref)
740  
741 The input attribute is read/write, accepts C<(arrayref)> values, and is
742 optional.
743  
744 I<Since C<4.15>>
745  
746 =over 4
747  
748 =item input example 1
749  
750 # given: synopsis;
751  
752 my $input = $validate->input([]);
753  
754 # []
755  
756 =back
757  
758 =over 4
759  
760 =item input example 2
761  
762 # given: synopsis;
763  
764 # given: example-1 input;
765  
766 $input = $validate->input;
767  
768 # []
769  
770 =back
771  
772 =cut
773  
774 =head1 INHERITS
775  
776 This package inherits behaviors from:
777  
778 L<Venus::Kind::Utility>
779  
780 =cut
781  
782 =head1 INTEGRATES
783  
784 This package integrates behaviors from:
785  
786 L<Venus::Role::Buildable>
787  
788 L<Venus::Role::Encaseable>
789  
790 =cut
791  
792 =head1 METHODS
793  
794 This package provides the following methods:
795  
796 =cut
797  
798 =head2 arrayref
799  
800 arrayref() (Venus::Validate)
801  
802 The arrayref method is shorthand for calling L</type> with C<"arrayref">. This
803 method is a validator and uses L</issue_info> to capture validation errors.
804  
805 I<Since C<4.15>>
806  
807 =over 4
808  
809 =item arrayref example 1
810  
811 package main;
812  
813 use Venus::Validate;
814  
815 my $validate = Venus::Validate->new([1,2]);
816  
817 my $arrayref = $validate->arrayref;
818  
819 # bless(..., "Venus::Validate")
820  
821 # $validate->is_valid;
822  
823 # true
824  
825 =back
826  
827 =over 4
828  
829 =item arrayref example 2
830  
831 package main;
832  
833 use Venus::Validate;
834  
835 my $validate = Venus::Validate->new({1..4});
836  
837 my $arrayref = $validate->arrayref;
838  
839 # bless(..., "Venus::Validate")
840  
841 # $validate->is_valid;
842  
843 # false
844  
845 =back
846  
847 =over 4
848  
849 =item arrayref example 3
850  
851 package main;
852  
853 use Venus::Validate;
854  
855 my $validate = Venus::Validate->new([1..4]);
856  
857 my $arrayref = $validate->arrayref;
858  
859 # bless(..., "Venus::Validate")
860  
861 # $validate->is_valid;
862  
863 # true
864  
865 =back
866  
867 =cut
868  
869 =head2 boolean
870  
871 boolean() (Venus::Validate)
872  
873 The boolean method is shorthand for calling L</type> with C<"boolean">. This
874 method is a validator and uses L</issue_info> to capture validation errors.
875  
876 I<Since C<4.15>>
877  
878 =over 4
879  
880 =item boolean example 1
881  
882 package main;
883  
884 use Venus::Validate;
885  
886 my $validate = Venus::Validate->new(true);
887  
888 my $boolean = $validate->boolean;
889  
890 # bless(..., "Venus::Validate")
891  
892 # $validate->is_valid;
893  
894 # true
895  
896 =back
897  
898 =over 4
899  
900 =item boolean example 2
901  
902 package main;
903  
904 use Venus::Validate;
905  
906 my $validate = Venus::Validate->new(false);
907  
908 my $boolean = $validate->boolean;
909  
910 # bless(..., "Venus::Validate")
911  
912 # $validate->is_valid;
913  
914 # true
915  
916 =back
917  
918 =over 4
919  
920 =item boolean example 3
921  
922 package main;
923  
924 use Venus::Validate;
925  
926 my $validate = Venus::Validate->new(1);
927  
928 my $boolean = $validate->boolean;
929  
930 # bless(..., "Venus::Validate")
931  
932 # $validate->is_valid;
933  
934 # false
935  
936 =back
937  
938 =over 4
939  
940 =item boolean example 4
941  
942 package main;
943  
944 use Venus::Validate;
945  
946 my $validate = Venus::Validate->new(0);
947  
948 my $boolean = $validate->boolean;
949  
950 # bless(..., "Venus::Validate")
951  
952 # $validate->is_valid;
953  
954 # false
955  
956 =back
957  
958 =cut
959  
960 =head2 check
961  
962 check(string $type, string $name, string | coderef $callback, any @args) (Venus::Validate)
963  
964 The check method provides a mechanism for performing a custom data validation
965 check. The first argument enables a data type check via L</type> based on the
966 type expression provided. The second argument is the name of the check being
967 performed, which will be used by L</issue_info> if the validation fails. The
968 remaining arguments are used in the callback provided which performs the custom
969 data validation.
970  
971 I<Since C<4.15>>
972  
973 =over 4
974  
975 =item check example 1
976  
977 # given: synopsis
978  
979 package main;
980  
981 my $check = $validate->check;
982  
983 # bless(..., "Venus::Validate")
984  
985 # $check->is_valid;
986  
987 # true
988  
989 =back
990  
991 =over 4
992  
993 =item check example 2
994  
995 # given: synopsis
996  
997 package main;
998  
999 my $check = $validate->check('string');
1000  
1001 # bless(..., "Venus::Validate")
1002  
1003 # $check->is_valid;
1004  
1005 # true
1006  
1007 =back
1008  
1009 =over 4
1010  
1011 =item check example 3
1012  
1013 # given: synopsis
1014  
1015 package main;
1016  
1017 my $check = $validate->check('string', 'is_email', sub{
1018 /\w\@\w/
1019 });
1020  
1021 # bless(..., "Venus::Validate")
1022  
1023 # $check->is_valid;
1024  
1025 # false
1026  
1027 # $check->issue;
1028  
1029 # ['is_email', []]
1030  
1031 =back
1032  
1033 =over 4
1034  
1035 =item check example 4
1036  
1037 # given: synopsis
1038  
1039 package main;
1040  
1041 $validate->value('hello@example.com');
1042  
1043 my $check = $validate->check('string', 'is_email', sub{
1044 /\w\@\w/
1045 });
1046  
1047 # bless(..., "Venus::Validate")
1048  
1049 # $check->is_valid;
1050  
1051 # true
1052  
1053 # $check->issue;
1054  
1055 # undef
1056  
1057 =back
1058  
1059 =cut
1060  
1061 =head2 defined
1062  
1063 defined() (Venus::Validate)
1064  
1065 The defined method is shorthand for calling L</type> with C<"defined">. This
1066 method is a validator and uses L</issue_info> to capture validation errors.
1067  
1068 I<Since C<4.15>>
1069  
1070 =over 4
1071  
1072 =item defined example 1
1073  
1074 package main;
1075  
1076 use Venus::Validate;
1077  
1078 my $validate = Venus::Validate->new('');
1079  
1080 my $defined = $validate->defined;
1081  
1082 # bless(..., "Venus::Validate")
1083  
1084 # $validate->is_valid;
1085  
1086 # true
1087  
1088 =back
1089  
1090 =over 4
1091  
1092 =item defined example 2
1093  
1094 package main;
1095  
1096 use Venus::Validate;
1097  
1098 my $validate = Venus::Validate->new(undef);
1099  
1100 my $defined = $validate->defined;
1101  
1102 # bless(..., "Venus::Validate")
1103  
1104 # $validate->is_valid;
1105  
1106 # false
1107  
1108 =back
1109  
1110 =over 4
1111  
1112 =item defined example 3
1113  
1114 package main;
1115  
1116 use Venus::Validate;
1117  
1118 my $validate = Venus::Validate->new;
1119  
1120 my $defined = $validate->defined;
1121  
1122 # bless(..., "Venus::Validate")
1123  
1124 # $validate->is_valid;
1125  
1126 # false
1127  
1128 =back
1129  
1130 =cut
1131  
1132 =head2 each
1133  
1134 each(string $type, string $path) (within[arrayref, Venus::Validate])
1135  
1136 The each method uses L</select> to retrieve data and for each item, builds a
1137 L<Venus::Validate> object for the value, settings the object to C<"present">,
1138 C<"required"> or C<"optional"> based on the argument provided, executing the
1139 callback provided for each object, and returns list of objects created.
1140 Defaults to C<"optional"> if no argument is provided. Returns a list in list
1141 context.
1142  
1143 I<Since C<4.15>>
1144  
1145 =over 4
1146  
1147 =item each example 1
1148  
1149 package main;
1150  
1151 use Venus::Validate;
1152  
1153 my $validate = Venus::Validate->new(['hello', 'bonjour']);
1154  
1155 my $each = $validate->each;
1156  
1157 # [bless(..., "Venus::Validate"), ...]
1158  
1159 =back
1160  
1161 =over 4
1162  
1163 =item each example 2
1164  
1165 package main;
1166  
1167 use Venus::Validate;
1168  
1169 my $validate = Venus::Validate->new(['hello', 'bonjour']);
1170  
1171 my @each = $validate->each;
1172  
1173 # (bless(..., "Venus::Validate"), ...)
1174  
1175 =back
1176  
1177 =over 4
1178  
1179 =item each example 3
1180  
1181 package main;
1182  
1183 use Venus::Validate;
1184  
1185 my $validate = Venus::Validate->new(['hello', 'bonjour']);
1186  
1187 my $each = $validate->each('required');
1188  
1189 # [bless(..., "Venus::Validate"), ...]
1190  
1191 =back
1192  
1193 =over 4
1194  
1195 =item each example 4
1196  
1197 package main;
1198  
1199 use Venus::Validate;
1200  
1201 my $validate = Venus::Validate->new({
1202 greetings => ['hello', 'bonjour'],
1203 });
1204  
1205 my $each = $validate->each('optional', 'greetings');
1206  
1207 # [bless(..., "Venus::Validate"), ...]
1208  
1209 =back
1210  
1211 =cut
1212  
1213 =head2 errors
1214  
1215 errors(arrayref $data) (arrayref)
1216  
1217 The errors method gets and sets the arrayref used by the current object and all
1218 subsequent nodes to capture errors/issues encountered. Each element of the
1219 arrayref will be an arrayref consisting of the node's L</path> and the
1220 L</issue>. This method returns a list in list context.
1221  
1222 I<Since C<4.15>>
1223  
1224 =over 4
1225  
1226 =item errors example 1
1227  
1228 # given: synopsis
1229  
1230 package main;
1231  
1232 my $errors = $validate->errors;
1233  
1234 # []
1235  
1236 =back
1237  
1238 =over 4
1239  
1240 =item errors example 2
1241  
1242 # given: synopsis
1243  
1244 package main;
1245  
1246 my $errors = $validate->errors([]);
1247  
1248 # []
1249  
1250 =back
1251  
1252 =over 4
1253  
1254 =item errors example 3
1255  
1256 package main;
1257  
1258 use Venus::Validate;
1259  
1260 my $validate = Venus::Validate->new([
1261 {
1262 name => 'john',
1263 },
1264 {
1265 name => 'jane',
1266 },
1267 ]);
1268  
1269 $validate->errors([]);
1270  
1271 # []
1272  
1273 my $required = $validate->required('2.name');
1274  
1275 # bless(..., "Venus::Validate")
1276  
1277 my $errors = $validate->errors;
1278  
1279 # [['2.name', ['required', []]]]
1280  
1281 =back
1282  
1283 =over 4
1284  
1285 =item errors example 4
1286  
1287 package main;
1288  
1289 use Venus::Validate;
1290  
1291 my $validate = Venus::Validate->new([
1292 {
1293 name => 'john',
1294 },
1295 {
1296 name => 'jane',
1297 },
1298 ]);
1299  
1300 $validate->errors([]);
1301  
1302 # []
1303  
1304 my $required = $validate->required('1.name');
1305  
1306 # bless(..., "Venus::Validate")
1307  
1308 $required->min_length(10);
1309  
1310 # bless(..., "Venus::Validate")
1311  
1312 my $errors = $validate->errors;
1313  
1314 # [['1.name', ['min_length', [10]]]]
1315  
1316 =back
1317  
1318 =over 4
1319  
1320 =item errors example 5
1321  
1322 package main;
1323  
1324 use Venus::Validate;
1325  
1326 my $validate = Venus::Validate->new([
1327 {
1328 name => 'john',
1329 },
1330 {
1331 name => 'jane',
1332 },
1333 ]);
1334  
1335 $validate->errors([]);
1336  
1337 # []
1338  
1339 my $name_0 = $validate->required('0.name');
1340  
1341 # bless(..., "Venus::Validate")
1342  
1343 $name_0->min_length(10);
1344  
1345 # bless(..., "Venus::Validate")
1346  
1347 my $name_1 = $validate->required('1.name');
1348  
1349 # bless(..., "Venus::Validate")
1350  
1351 $name_1->min_length(10);
1352  
1353 # bless(..., "Venus::Validate")
1354  
1355 my $errors = $validate->errors;
1356  
1357 # [['0.name', ['min_length', [10]]], ['1.name', ['min_length', [10]]]]
1358  
1359 =back
1360  
1361 =cut
1362  
1363 =head2 exists
1364  
1365 exists() (boolean)
1366  
1367 The exists method returns true if a value exists for the current object, and
1368 otherwise returns false.
1369  
1370 I<Since C<4.15>>
1371  
1372 =over 4
1373  
1374 =item exists example 1
1375  
1376 # given: synopsis
1377  
1378 package main;
1379  
1380 my $exists = $validate->exists;
1381  
1382 # true
1383  
1384 =back
1385  
1386 =over 4
1387  
1388 =item exists example 2
1389  
1390 # given: synopsis
1391  
1392 package main;
1393  
1394 $validate->value(undef);
1395  
1396 my $exists = $validate->exists;
1397  
1398 # true
1399  
1400 =back
1401  
1402 =over 4
1403  
1404 =item exists example 3
1405  
1406 package main;
1407  
1408 use Venus::Validate;
1409  
1410 my $validate = Venus::Validate->new;
1411  
1412 my $exists = $validate->exists;
1413  
1414 # false
1415  
1416 =back
1417  
1418 =cut
1419  
1420 =head2 float
1421  
1422 float() (Venus::Validate)
1423  
1424 The float method is shorthand for calling L</type> with C<"float">. This method
1425 is a validator and uses L</issue_info> to capture validation errors.
1426  
1427 I<Since C<4.15>>
1428  
1429 =over 4
1430  
1431 =item float example 1
1432  
1433 package main;
1434  
1435 use Venus::Validate;
1436  
1437 my $validate = Venus::Validate->new(1.23);
1438  
1439 my $float = $validate->float;
1440  
1441 # bless(..., "Venus::Validate")
1442  
1443 # $validate->is_valid;
1444  
1445 # true
1446  
1447 =back
1448  
1449 =over 4
1450  
1451 =item float example 2
1452  
1453 package main;
1454  
1455 use Venus::Validate;
1456  
1457 my $validate = Venus::Validate->new(1_23);
1458  
1459 my $float = $validate->float;
1460  
1461 # bless(..., "Venus::Validate")
1462  
1463 # $validate->is_valid;
1464  
1465 # false
1466  
1467 =back
1468  
1469 =over 4
1470  
1471 =item float example 3
1472  
1473 package main;
1474  
1475 use Venus::Validate;
1476  
1477 my $validate = Venus::Validate->new("1.23");
1478  
1479 my $float = $validate->float;
1480  
1481 # bless(..., "Venus::Validate")
1482  
1483 # $validate->is_valid;
1484  
1485 # true
1486  
1487 =back
1488  
1489 =cut
1490  
1491 =head2 hashref
1492  
1493 hashref() (Venus::Validate)
1494  
1495 The hashref method is shorthand for calling L</type> with C<"hashref">. This
1496 method is a validator and uses L</issue_info> to capture validation errors.
1497  
1498 I<Since C<4.15>>
1499  
1500 =over 4
1501  
1502 =item hashref example 1
1503  
1504 package main;
1505  
1506 use Venus::Validate;
1507  
1508 my $validate = Venus::Validate->new({1,2});
1509  
1510 my $hashref = $validate->hashref;
1511  
1512 # bless(..., "Venus::Validate")
1513  
1514 # $validate->is_valid;
1515  
1516 # true
1517  
1518 =back
1519  
1520 =over 4
1521  
1522 =item hashref example 2
1523  
1524 package main;
1525  
1526 use Venus::Validate;
1527  
1528 my $validate = Venus::Validate->new([1..4]);
1529  
1530 my $hashref = $validate->hashref;
1531  
1532 # bless(..., "Venus::Validate")
1533  
1534 # $validate->is_valid;
1535  
1536 # false
1537  
1538 =back
1539  
1540 =over 4
1541  
1542 =item hashref example 3
1543  
1544 package main;
1545  
1546 use Venus::Validate;
1547  
1548 my $validate = Venus::Validate->new({1..4});
1549  
1550 my $hashref = $validate->hashref;
1551  
1552 # bless(..., "Venus::Validate")
1553  
1554 # $validate->is_valid;
1555  
1556 # true
1557  
1558 =back
1559  
1560 =cut
1561  
1562 =head2 is_invalid
1563  
1564 is_invalid() (boolean)
1565  
1566 The is_invalid method returns true if an issue exists, and false otherwise.
1567  
1568 I<Since C<4.15>>
1569  
1570 =over 4
1571  
1572 =item is_invalid example 1
1573  
1574 package main;
1575  
1576 use Venus::Validate;
1577  
1578 my $validate = Venus::Validate->new('hello')->string;
1579  
1580 my $is_invalid = $validate->is_invalid;
1581  
1582 # false
1583  
1584 =back
1585  
1586 =over 4
1587  
1588 =item is_invalid example 2
1589  
1590 package main;
1591  
1592 use Venus::Validate;
1593  
1594 my $validate = Venus::Validate->new('hello')->number;
1595  
1596 my $is_invalid = $validate->is_invalid;
1597  
1598 # true
1599  
1600 =back
1601  
1602 =cut
1603  
1604 =head2 is_valid
1605  
1606 is_valid() (boolean)
1607  
1608 The is_valid method returns true if no issue exists, and false otherwise.
1609  
1610 I<Since C<4.15>>
1611  
1612 =over 4
1613  
1614 =item is_valid example 1
1615  
1616 package main;
1617  
1618 use Venus::Validate;
1619  
1620 my $validate = Venus::Validate->new('hello')->string;
1621  
1622 my $is_valid = $validate->is_valid;
1623  
1624 # true
1625  
1626 =back
1627  
1628 =over 4
1629  
1630 =item is_valid example 2
1631  
1632 package main;
1633  
1634 use Venus::Validate;
1635  
1636 my $validate = Venus::Validate->new('hello')->number;
1637  
1638 my $is_valid = $validate->is_valid;
1639  
1640 # false
1641  
1642 =back
1643  
1644 =cut
1645  
1646 =head2 issue_args
1647  
1648 issue_args() (arrayref)
1649  
1650 The issue_args method returns the arguments provided as part of the issue
1651 context. Returns a list in list context.
1652  
1653 I<Since C<4.15>>
1654  
1655 =over 4
1656  
1657 =item issue_args example 1
1658  
1659 # given: synopsis;
1660  
1661 $validate->issue(['max_length', ['255']]);
1662  
1663 my $issue_args = $validate->issue_args;
1664  
1665 # ['255']
1666  
1667 =back
1668  
1669 =cut
1670  
1671 =head2 issue_info
1672  
1673 issue_info(string $type, any @args) (tuple[string, arrayref])
1674  
1675 The issue_info method gets or sets the issue context and returns the
1676 L</issue_type> and L</issue_args>. Returns a list in list context.
1677  
1678 I<Since C<4.15>>
1679  
1680 =over 4
1681  
1682 =item issue_info example 1
1683  
1684 # given: synopsis;
1685  
1686 my $issue_info = $validate->issue_info;
1687  
1688 # undef
1689  
1690 =back
1691  
1692 =over 4
1693  
1694 =item issue_info example 2
1695  
1696 # given: synopsis;
1697  
1698 my $issue_info = $validate->issue_info('max_length', '255');
1699  
1700 # ['max_length', ['255']]
1701  
1702 =back
1703  
1704 =over 4
1705  
1706 =item issue_info example 3
1707  
1708 # given: synopsis;
1709  
1710 # given: example-2 issue_info
1711  
1712 $issue_info = $validate->issue_info;
1713  
1714 # ['max_length', ['255']]
1715  
1716 =back
1717  
1718 =over 4
1719  
1720 =item issue_info example 4
1721  
1722 # given: synopsis;
1723  
1724 # given: example-2 issue_info
1725  
1726 my ($type, @args) = $validate->issue_info;
1727  
1728 # ('max_length', '255')
1729  
1730 =back
1731  
1732 =cut
1733  
1734 =head2 issue_type
1735  
1736 issue_type() (string)
1737  
1738 The issue_type method returns the issue type (i.e. type of issue) provided as
1739 part of the issue context.
1740  
1741 I<Since C<4.15>>
1742  
1743 =over 4
1744  
1745 =item issue_type example 1
1746  
1747 # given: synopsis;
1748  
1749 $validate->issue(['max_length', ['255']]);
1750  
1751 my $issue_type = $validate->issue_type;
1752  
1753 # 'max_length'
1754  
1755 =back
1756  
1757 =cut
1758  
1759 =head2 length
1760  
1761 length(number $min, number $max) (Venus::Validate)
1762  
1763 The length method accepts a minimum and maximum and validates that the length
1764 of the data meets the criteria and returns the invocant. This method is a proxy
1765 for the L</min_length> and L</max_length> methods and the errors/issues
1766 encountered will be specific to those operations.
1767  
1768 I<Since C<4.15>>
1769  
1770 =over 4
1771  
1772 =item length example 1
1773  
1774 package main;
1775  
1776 use Venus::Validate;
1777  
1778 my $validate = Venus::Validate->new('');
1779  
1780 my $length = $validate->length(1, 3);
1781  
1782 # bless(..., "Venus::Validate")
1783  
1784 # $length->issue;
1785  
1786 # ['min_length', [1]]
1787  
1788 =back
1789  
1790 =over 4
1791  
1792 =item length example 2
1793  
1794 package main;
1795  
1796 use Venus::Validate;
1797  
1798 my $validate = Venus::Validate->new('hello');
1799  
1800 my $length = $validate->length(1, 3);
1801  
1802 # bless(..., "Venus::Validate")
1803  
1804 # $length->issue;
1805  
1806 # ['max_length', [3]]
1807  
1808 =back
1809  
1810 =cut
1811  
1812 =head2 lowercase
1813  
1814 lowercase() (Venus::Validate)
1815  
1816 The lowercase method lowercases the value and returns the invocant.
1817  
1818 I<Since C<4.15>>
1819  
1820 =over 4
1821  
1822 =item lowercase example 1
1823  
1824 package main;
1825  
1826 use Venus::Validate;
1827  
1828 my $validate = Venus::Validate->new('Hello world');
1829  
1830 my $lowercase = $validate->lowercase;
1831  
1832 # bless(..., "Venus::Validate")
1833  
1834 # $lowercase->value;
1835  
1836 # "hello world"
1837  
1838 =back
1839  
1840 =cut
1841  
1842 =head2 max_length
1843  
1844 max_length(number $max) (Venus::Validate)
1845  
1846 The max_length method accepts a maximum and validates that the length of the
1847 data meets the criteria and returns the invocant. This method is a validator
1848 and uses L</issue_info> to capture validation errors.
1849  
1850 I<Since C<4.15>>
1851  
1852 =over 4
1853  
1854 =item max_length example 1
1855  
1856 package main;
1857  
1858 use Venus::Validate;
1859  
1860 my $validate = Venus::Validate->new('hello');
1861  
1862 my $max_length = $validate->max_length(5);
1863  
1864 # bless(..., "Venus::Validate")
1865  
1866 # $max_length->issue;
1867  
1868 # undef
1869  
1870 =back
1871  
1872 =over 4
1873  
1874 =item max_length example 2
1875  
1876 package main;
1877  
1878 use Venus::Validate;
1879  
1880 my $validate = Venus::Validate->new('hello');
1881  
1882 my $max_length = $validate->max_length(3);
1883  
1884 # bless(..., "Venus::Validate")
1885  
1886 # $max_length->issue;
1887  
1888 # ['max_length', [3]]
1889  
1890 =back
1891  
1892 =cut
1893  
1894 =head2 max_number
1895  
1896 max_number(number $max) (Venus::Validate)
1897  
1898 The max_number accepts a maximum and validates that the data is exactly the
1899 number provided or less, and returns the invocant. This method is a validator
1900 and uses L</issue_info> to capture validation errors.
1901  
1902 I<Since C<4.15>>
1903  
1904 =over 4
1905  
1906 =item max_number example 1
1907  
1908 package main;
1909  
1910 use Venus::Validate;
1911  
1912 my $validate = Venus::Validate->new(1);
1913  
1914 my $max_number = $validate->max_number(1);
1915  
1916 # bless(..., "Venus::Validate")
1917  
1918 # $max_number->issue;
1919  
1920 # undef
1921  
1922 =back
1923  
1924 =over 4
1925  
1926 =item max_number example 2
1927  
1928 package main;
1929  
1930 use Venus::Validate;
1931  
1932 my $validate = Venus::Validate->new(1);
1933  
1934 my $max_number = $validate->max_number(0);
1935  
1936 # bless(..., "Venus::Validate")
1937  
1938 # $max_number->issue;
1939  
1940 # ['max_number', [0]]
1941  
1942 =back
1943  
1944 =cut
1945  
1946 =head2 min_length
1947  
1948 min_length(number $min) (Venus::Validate)
1949  
1950 The min_length accepts a minimum and validates that the length of the data
1951 meets the criteria and returns the invocant. This method is a validator and
1952 uses L</issue_info> to capture validation errors.
1953  
1954 I<Since C<4.15>>
1955  
1956 =over 4
1957  
1958 =item min_length example 1
1959  
1960 package main;
1961  
1962 use Venus::Validate;
1963  
1964 my $validate = Venus::Validate->new('hello');
1965  
1966 my $min_length = $validate->min_length(1);
1967  
1968 # bless(..., "Venus::Validate")
1969  
1970 # $min_length->issue;
1971  
1972 # undef
1973  
1974 =back
1975  
1976 =over 4
1977  
1978 =item min_length example 2
1979  
1980 package main;
1981  
1982 use Venus::Validate;
1983  
1984 my $validate = Venus::Validate->new('');
1985  
1986 my $min_length = $validate->min_length(1);
1987  
1988 # bless(..., "Venus::Validate")
1989  
1990 # $min_length->issue;
1991  
1992 # ['min_length', [1]]
1993  
1994 =back
1995  
1996 =cut
1997  
1998 =head2 min_number
1999  
2000 min_number(number $min) (Venus::Validate)
2001  
2002 The min_number accepts a minimum and validates that the data is exactly the
2003 number provided or greater, and returns the invocant. This method is a
2004 validator and uses L</issue_info> to capture validation errors.
2005  
2006 I<Since C<4.15>>
2007  
2008 =over 4
2009  
2010 =item min_number example 1
2011  
2012 package main;
2013  
2014 use Venus::Validate;
2015  
2016 my $validate = Venus::Validate->new(1);
2017  
2018 my $min_number = $validate->min_number(1);
2019  
2020 # bless(..., "Venus::Validate")
2021  
2022 # $min_number->issue;
2023  
2024 # undef
2025  
2026 =back
2027  
2028 =over 4
2029  
2030 =item min_number example 2
2031  
2032 package main;
2033  
2034 use Venus::Validate;
2035  
2036 my $validate = Venus::Validate->new(1);
2037  
2038 my $min_number = $validate->min_number(2);
2039  
2040 # bless(..., "Venus::Validate")
2041  
2042 # $min_number->issue;
2043  
2044 # ['min_number', [2]]
2045  
2046 =back
2047  
2048 =cut
2049  
2050 =head2 new
2051  
2052 new(hashref $data) (Venus::Validate)
2053  
2054 The new method returns a L<Venus::Validate> object.
2055  
2056 I<Since C<4.15>>
2057  
2058 =over 4
2059  
2060 =item new example 1
2061  
2062 package main;
2063  
2064 use Venus::Validate;
2065  
2066 my $validate = Venus::Validate->new;
2067  
2068 # bless(..., "Venus::Validate")
2069  
2070 =back
2071  
2072 =over 4
2073  
2074 =item new example 2
2075  
2076 package main;
2077  
2078 use Venus::Validate;
2079  
2080 my $validate = Venus::Validate->new(input => 'hello');
2081  
2082 # bless(..., "Venus::Validate")
2083  
2084 =back
2085  
2086 =over 4
2087  
2088 =item new example 3
2089  
2090 package main;
2091  
2092 use Venus::Validate;
2093  
2094 my $validate = Venus::Validate->new({input => 'hello'});
2095  
2096 # bless(..., "Venus::Validate")
2097  
2098 =back
2099  
2100 =over 4
2101  
2102 =item new example 4
2103  
2104 package main;
2105  
2106 use Venus::Validate;
2107  
2108 my $validate = Venus::Validate->new('hello');
2109  
2110 # bless(..., "Venus::Validate")
2111  
2112 =back
2113  
2114 =cut
2115  
2116 =head2 number
2117  
2118 number() (Venus::Validate)
2119  
2120 The number method is shorthand for calling L</type> with C<"number">. This
2121 method is a validator and uses L</issue_info> to capture validation errors.
2122  
2123 I<Since C<4.15>>
2124  
2125 =over 4
2126  
2127 =item number example 1
2128  
2129 package main;
2130  
2131 use Venus::Validate;
2132  
2133 my $validate = Venus::Validate->new(123);
2134  
2135 my $number = $validate->number;
2136  
2137 # bless(..., "Venus::Validate")
2138  
2139 # $validate->is_valid;
2140  
2141 # true
2142  
2143 =back
2144  
2145 =over 4
2146  
2147 =item number example 2
2148  
2149 package main;
2150  
2151 use Venus::Validate;
2152  
2153 my $validate = Venus::Validate->new(1.23);
2154  
2155 my $number = $validate->number;
2156  
2157 # bless(..., "Venus::Validate")
2158  
2159 # $validate->is_valid;
2160  
2161 # false
2162  
2163 =back
2164  
2165 =over 4
2166  
2167 =item number example 3
2168  
2169 package main;
2170  
2171 use Venus::Validate;
2172  
2173 my $validate = Venus::Validate->new(1_23);
2174  
2175 my $number = $validate->number;
2176  
2177 # bless(..., "Venus::Validate")
2178  
2179 # $validate->is_valid;
2180  
2181 # true
2182  
2183 =back
2184  
2185 =cut
2186  
2187 =head2 on_invalid
2188  
2189 on_invalid(coderef $callback, any @args) (Venus::Validate)
2190  
2191 The on_invalid method chains an operations by passing the issue value of the
2192 object to the callback provided and returns a L<Venus::Validate> object.
2193  
2194 I<Since C<4.15>>
2195  
2196 =over 4
2197  
2198 =item on_invalid example 1
2199  
2200 package main;
2201  
2202 use Venus::Validate;
2203  
2204 my $validate = Venus::Validate->new('hello')->number;
2205  
2206 my $on_invalid = $validate->on_invalid;
2207  
2208 # bless(..., "Venus::Validate")
2209  
2210 =back
2211  
2212 =over 4
2213  
2214 =item on_invalid example 2
2215  
2216 package main;
2217  
2218 use Venus::Validate;
2219  
2220 my $validate = Venus::Validate->new('hello')->number;
2221  
2222 my $on_invalid = $validate->on_invalid(sub{
2223 $validate->{called} = time;
2224 return $validate;
2225 });
2226  
2227 # bless(..., "Venus::Validate")
2228  
2229 =back
2230  
2231 =cut
2232  
2233 =head2 on_valid
2234  
2235 on_valid(coderef $callback, any @args) (Venus::Validate)
2236  
2237 The on_valid method chains an operations by passing the value of the object to
2238 the callback provided and returns a L<Venus::Validate> object.
2239  
2240 I<Since C<4.15>>
2241  
2242 =over 4
2243  
2244 =item on_valid example 1
2245  
2246 package main;
2247  
2248 use Venus::Validate;
2249  
2250 my $validate = Venus::Validate->new('hello')->string;
2251  
2252 my $on_valid = $validate->on_valid;
2253  
2254 # bless(..., "Venus::Validate")
2255  
2256 =back
2257  
2258 =over 4
2259  
2260 =item on_valid example 2
2261  
2262 package main;
2263  
2264 use Venus::Validate;
2265  
2266 my $validate = Venus::Validate->new('hello')->string;
2267  
2268 my $on_valid = $validate->on_valid(sub{
2269 $validate->{called} = time;
2270 return $validate;
2271 });
2272  
2273 # bless(..., "Venus::Validate")
2274  
2275 =back
2276  
2277 =cut
2278  
2279 =head2 optional
2280  
2281 optional(string $path) (Venus::Validate)
2282  
2283 The optional method uses L</select> to retrieve data and returns a
2284 L<Venus::Validate> object with the selected data marked as optional.
2285  
2286 I<Since C<4.15>>
2287  
2288 =over 4
2289  
2290 =item optional example 1
2291  
2292 package main;
2293  
2294 use Venus::Validate;
2295  
2296 my $validate = Venus::Validate->new({
2297 fname => 'John',
2298 lname => 'Doe',
2299 });
2300  
2301 my $optional = $validate->optional('email');
2302  
2303 # bless(..., "Venus::Validate")
2304  
2305 # $optional->is_valid;
2306  
2307 # true
2308  
2309 =back
2310  
2311 =over 4
2312  
2313 =item optional example 2
2314  
2315 package main;
2316  
2317 use Venus::Validate;
2318  
2319 my $validate = Venus::Validate->new({
2320 fname => 'John',
2321 lname => 'Doe',
2322 email => 'johndoe@example.com',
2323 });
2324  
2325 my $optional = $validate->optional('email');
2326  
2327 # bless(..., "Venus::Validate")
2328  
2329 # $optional->is_valid;
2330  
2331 # true
2332  
2333 =back
2334  
2335 =cut
2336  
2337 =head2 present
2338  
2339 present(string $path) (Venus::Validate)
2340  
2341 The present method uses L</select> to retrieve data and returns a
2342 L<Venus::Validate> object with the selected data marked as needing to be
2343 present. This method is a validator and uses L</issue_info> to capture
2344 validation errors.
2345  
2346 I<Since C<4.15>>
2347  
2348 =over 4
2349  
2350 =item present example 1
2351  
2352 package main;
2353  
2354 use Venus::Validate;
2355  
2356 my $validate = Venus::Validate->new({
2357 fname => 'John',
2358 lname => 'Doe',
2359 });
2360  
2361 my $present = $validate->present('email');
2362  
2363 # bless(..., "Venus::Validate")
2364  
2365 # $present->is_valid;
2366  
2367 # false
2368  
2369 =back
2370  
2371 =over 4
2372  
2373 =item present example 2
2374  
2375 package main;
2376  
2377 use Venus::Validate;
2378  
2379 my $validate = Venus::Validate->new({
2380 fname => 'John',
2381 lname => 'Doe',
2382 email => 'johndoe@example.com',
2383 });
2384  
2385 my $present = $validate->present('email');
2386  
2387 # bless(..., "Venus::Validate")
2388  
2389 # $present->is_valid;
2390  
2391 # true
2392  
2393 =back
2394  
2395 =cut
2396  
2397 =head2 required
2398  
2399 required(string $path) (Venus::Validate)
2400  
2401 The required method uses L</select> to retrieve data and returns a
2402 L<Venus::Validate> object with the selected data marked as required. This
2403 method is a validator and uses L</issue_info> to capture validation errors.
2404  
2405 I<Since C<4.15>>
2406  
2407 =over 4
2408  
2409 =item required example 1
2410  
2411 package main;
2412  
2413 use Venus::Validate;
2414  
2415 my $validate = Venus::Validate->new({
2416 fname => 'John',
2417 lname => 'Doe',
2418 });
2419  
2420 my $required = $validate->required('email');
2421  
2422 # bless(..., "Venus::Validate")
2423  
2424 # $present->is_valid;
2425  
2426 # false
2427  
2428 =back
2429  
2430 =over 4
2431  
2432 =item required example 2
2433  
2434 package main;
2435  
2436 use Venus::Validate;
2437  
2438 my $validate = Venus::Validate->new({
2439 fname => 'John',
2440 lname => 'Doe',
2441 email => 'johndoe@example.com',
2442 });
2443  
2444 my $required = $validate->required('email');
2445  
2446 # bless(..., "Venus::Validate")
2447  
2448 # $present->is_valid;
2449  
2450 # true
2451  
2452 =back
2453  
2454 =cut
2455  
2456 =head2 select
2457  
2458 select(string $path) (Venus::Validate)
2459  
2460 The select method uses L<Venus::Hash/path> to retrieve data and returns a
2461 L<Venus::Validate> object with the selected data. Returns C<undef> if the data
2462 can't be selected.
2463  
2464 I<Since C<4.15>>
2465  
2466 =over 4
2467  
2468 =item select example 1
2469  
2470 # given: synopsis;
2471  
2472 my $select = $validate->select;
2473  
2474 # bless(..., "Venus::Validate")
2475  
2476 =back
2477  
2478 =over 4
2479  
2480 =item select example 2
2481  
2482 # given: synopsis;
2483  
2484 my $select = $validate->select('ello');
2485  
2486 # bless(..., "Venus::Validate")
2487  
2488 =back
2489  
2490 =over 4
2491  
2492 =item select example 3
2493  
2494 package main;
2495  
2496 use Venus::Validate;
2497  
2498 my $validate = Venus::Validate->new([
2499 {
2500 name => 'john',
2501 },
2502 {
2503 name => 'jane',
2504 },
2505 ]);
2506  
2507 my $select = $validate->select('0.name');
2508  
2509 # bless(..., "Venus::Validate")
2510  
2511 # $select->value;
2512  
2513 # "john"
2514  
2515 =back
2516  
2517 =over 4
2518  
2519 =item select example 4
2520  
2521 package main;
2522  
2523 use Venus::Validate;
2524  
2525 my $validate = Venus::Validate->new([
2526 {
2527 name => 'john',
2528 },
2529 {
2530 name => 'jane',
2531 },
2532 ]);
2533  
2534 my $select = $validate->select('1.name');
2535  
2536 # bless(..., "Venus::Validate")
2537  
2538 # $select->value;
2539  
2540 # "jane"
2541  
2542 =back
2543  
2544 =over 4
2545  
2546 =item select example 5
2547  
2548 package main;
2549  
2550 use Venus::Validate;
2551  
2552 my $validate = Venus::Validate->new({
2553 persons => [
2554 {
2555 name => 'john',
2556 },
2557 {
2558 name => 'jane',
2559 },
2560 ]
2561 });
2562  
2563 my $select = $validate->select('persons.0.name');
2564  
2565 # bless(..., "Venus::Validate")
2566  
2567 # $select->value;
2568  
2569 # "john"
2570  
2571 =back
2572  
2573 =over 4
2574  
2575 =item select example 6
2576  
2577 package main;
2578  
2579 use Venus::Validate;
2580  
2581 my $validate = Venus::Validate->new({
2582 persons => [
2583 {
2584 name => 'john',
2585 },
2586 {
2587 name => 'jane',
2588 },
2589 ]
2590 });
2591  
2592 my $select = $validate->select('persons.1.name');
2593  
2594 # bless(..., "Venus::Validate")
2595  
2596 # $select->value;
2597  
2598 # "jane"
2599  
2600 =back
2601  
2602 =cut
2603  
2604 =head2 string
2605  
2606 string() (Venus::Validate)
2607  
2608 The string method is shorthand for calling L</type> with C<"string">. This
2609 method is a validator and uses L</issue_info> to capture validation errors.
2610  
2611 I<Since C<4.15>>
2612  
2613 =over 4
2614  
2615 =item string example 1
2616  
2617 package main;
2618  
2619 use Venus::Validate;
2620  
2621 my $validate = Venus::Validate->new('hello');
2622  
2623 my $string = $validate->string;
2624  
2625 # bless(..., "Venus::Validate")
2626  
2627 # $validate->is_valid;
2628  
2629 # true
2630  
2631 =back
2632  
2633 =over 4
2634  
2635 =item string example 2
2636  
2637 package main;
2638  
2639 use Venus::Validate;
2640  
2641 my $validate = Venus::Validate->new('');
2642  
2643 my $string = $validate->string;
2644  
2645 # bless(..., "Venus::Validate")
2646  
2647 # $validate->is_valid;
2648  
2649 # true
2650  
2651 =back
2652  
2653 =over 4
2654  
2655 =item string example 3
2656  
2657 package main;
2658  
2659 use Venus::Validate;
2660  
2661 my $validate = Venus::Validate->new('1.23');
2662  
2663 my $string = $validate->string;
2664  
2665 # bless(..., "Venus::Validate")
2666  
2667 # $validate->is_valid;
2668  
2669 # false
2670  
2671 =back
2672  
2673 =cut
2674  
2675 =head2 strip
2676  
2677 strip() (Venus::Validate)
2678  
2679 The strip method removes multiple consecutive whitespace characters from the
2680 value and returns the invocant.
2681  
2682 I<Since C<4.15>>
2683  
2684 =over 4
2685  
2686 =item strip example 1
2687  
2688 package main;
2689  
2690 use Venus::Validate;
2691  
2692 my $validate = Venus::Validate->new('hello world');
2693  
2694 my $strip = $validate->strip;
2695  
2696 # bless(..., "Venus::Validate")
2697  
2698 # $strip->value;
2699  
2700 # "hello world"
2701  
2702 =back
2703  
2704 =over 4
2705  
2706 =item strip example 2
2707  
2708 package main;
2709  
2710 use Venus::Validate;
2711  
2712 my $validate = Venus::Validate->new({text => 'hello world'});
2713  
2714 my $strip = $validate->strip;
2715  
2716 # bless(..., "Venus::Validate")
2717  
2718 # $strip->value;
2719  
2720 # {text => 'hello world'}
2721  
2722 =back
2723  
2724 =cut
2725  
2726 =head2 sync
2727  
2728 sync(Venus::Validate $node) (Venus::Validate)
2729  
2730 The sync method merges the L<Venus::Validate> node provided with the current
2731 object.
2732  
2733 I<Since C<4.15>>
2734  
2735 =over 4
2736  
2737 =item sync example 1
2738  
2739 # given: synopsis;
2740  
2741 my $sync = $validate->sync;
2742  
2743 # bless(..., "Venus::Validate")
2744  
2745 =back
2746  
2747 =over 4
2748  
2749 =item sync example 2
2750  
2751 package main;
2752  
2753 use Venus::Validate;
2754  
2755 my $root = Venus::Validate->new({
2756 persons => [
2757 {
2758 name => 'john',
2759 },
2760 {
2761 name => 'jane',
2762 },
2763 ]
2764 });
2765  
2766 my $node = $root->select('persons.1.name');
2767  
2768 # bless(..., "Venus::Validate")
2769  
2770 $node->value('jack');
2771  
2772 # "jack"
2773  
2774 # $root->select('persons.1.name')->value;
2775  
2776 # "john"
2777  
2778 $root->sync($node);
2779  
2780 # bless(..., "Venus::Validate")
2781  
2782 # $root->select('persons.1.name')->value;
2783  
2784 # "jack"
2785  
2786 =back
2787  
2788 =over 4
2789  
2790 =item sync example 3
2791  
2792 package main;
2793  
2794 use Venus::Validate;
2795  
2796 my $root = Venus::Validate->new(['john', 'jane']);
2797  
2798 my $node = $root->select(1);
2799  
2800 # bless(..., "Venus::Validate")
2801  
2802 $node->value('jill');
2803  
2804 # "jill"
2805  
2806 # $root->select(1)->value;
2807  
2808 # "jane"
2809  
2810 $root->sync($node);
2811  
2812 # bless(..., "Venus::Validate")
2813  
2814 # $root->select(1)->value;
2815  
2816 # "jill"
2817  
2818 =back
2819  
2820 =cut
2821  
2822 =head2 titlecase
2823  
2824 titlecase() (Venus::Validate)
2825  
2826 The titlecase method titlecases the value and returns the invocant.
2827  
2828 I<Since C<4.15>>
2829  
2830 =over 4
2831  
2832 =item titlecase example 1
2833  
2834 package main;
2835  
2836 use Venus::Validate;
2837  
2838 my $validate = Venus::Validate->new('hello world');
2839  
2840 my $titlecase = $validate->titlecase;
2841  
2842 # bless(..., "Venus::Validate")
2843  
2844 # $titlecase->value;
2845  
2846 # "Hello World"
2847  
2848 =back
2849  
2850 =cut
2851  
2852 =head2 trim
2853  
2854 trim() (Venus::Validate)
2855  
2856 The trim method removes whitespace characters from both ends of the value and
2857 returns the invocant.
2858  
2859 I<Since C<4.15>>
2860  
2861 =over 4
2862  
2863 =item trim example 1
2864  
2865 package main;
2866  
2867 use Venus::Validate;
2868  
2869 my $validate = Venus::Validate->new(' hello world ');
2870  
2871 my $trim = $validate->trim;
2872  
2873 # bless(..., "Venus::Validate")
2874  
2875 # $trim->value;
2876  
2877 # "hello world"
2878  
2879 =back
2880  
2881 =over 4
2882  
2883 =item trim example 2
2884  
2885 package main;
2886  
2887 use Venus::Validate;
2888  
2889 my $validate = Venus::Validate->new({text => ' hello world '});
2890  
2891 my $trim = $validate->trim;
2892  
2893 # bless(..., "Venus::Validate")
2894  
2895 # $trim->value;
2896  
2897 # {text => ' hello world '}
2898  
2899 =back
2900  
2901 =cut
2902  
2903 =head2 type
2904  
2905 type(string $type) (Venus::Validate)
2906  
2907 The type method validates that the value conforms with the L<Venus::Type> type
2908 expression provided, and returns the invocant. This method is a validator and
2909 uses L</issue_info> to capture validation errors.
2910  
2911 I<Since C<4.15>>
2912  
2913 =over 4
2914  
2915 =item type example 1
2916  
2917 package main;
2918  
2919 use Venus::Validate;
2920  
2921 my $validate = Venus::Validate->new('hello');
2922  
2923 my $type = $validate->type;
2924  
2925 # bless(..., "Venus::Validate")
2926  
2927 # $type->is_valid;
2928  
2929 # true
2930  
2931 =back
2932  
2933 =over 4
2934  
2935 =item type example 2
2936  
2937 package main;
2938  
2939 use Venus::Validate;
2940  
2941 my $validate = Venus::Validate->new('hello');
2942  
2943 my $type = $validate->type('string');
2944  
2945 # bless(..., "Venus::Validate")
2946  
2947 # $type->is_valid;
2948  
2949 # true
2950  
2951 =back
2952  
2953 =over 4
2954  
2955 =item type example 3
2956  
2957 package main;
2958  
2959 use Venus::Validate;
2960  
2961 my $validate = Venus::Validate->new('hello');
2962  
2963 my $type = $validate->type('number');
2964  
2965 # bless(..., "Venus::Validate")
2966  
2967 # $type->is_valid;
2968  
2969 # false
2970  
2971 =back
2972  
2973 =over 4
2974  
2975 =item type example 4
2976  
2977 package main;
2978  
2979 use Venus::Validate;
2980  
2981 my $validate = Venus::Validate->new('Y');
2982  
2983 my $type = $validate->type('yesno');
2984  
2985 # bless(..., "Venus::Validate")
2986  
2987 # $type->is_valid;
2988  
2989 # true
2990  
2991 =back
2992  
2993 =over 4
2994  
2995 =item type example 5
2996  
2997 package main;
2998  
2999 use Venus::Validate;
3000  
3001 my $validate = Venus::Validate->new('A');
3002  
3003 my $type = $validate->type('enum[A, B]');
3004  
3005 # bless(..., "Venus::Validate")
3006  
3007 # $type->is_valid;
3008  
3009 # true
3010  
3011 =back
3012  
3013 =over 4
3014  
3015 =item type example 6
3016  
3017 package main;
3018  
3019 use Venus::Validate;
3020  
3021 my $validate = Venus::Validate->new([200, [], '']);
3022  
3023 my $type = $validate->type('tuple[number, arrayref, string]');
3024  
3025 # bless(..., "Venus::Validate")
3026  
3027 # $type->is_valid;
3028  
3029 # true
3030  
3031 =back
3032  
3033 =over 4
3034  
3035 =item type example 7
3036  
3037 package main;
3038  
3039 use Venus::Validate;
3040  
3041 my $validate = Venus::Validate->new(undef);
3042  
3043 my $type = $validate->type('string');
3044  
3045 # bless(..., "Venus::Validate")
3046  
3047 # $type->is_valid;
3048  
3049 # true
3050  
3051 =back
3052  
3053 =over 4
3054  
3055 =item type example 8
3056  
3057 package main;
3058  
3059 use Venus::Validate;
3060  
3061 my $validate = Venus::Validate->new(undef);
3062  
3063 my $type = $validate->optional->type('string');
3064  
3065 # bless(..., "Venus::Validate")
3066  
3067 # $type->is_valid;
3068  
3069 # true
3070  
3071 =back
3072  
3073 =cut
3074  
3075 =head2 uppercase
3076  
3077 uppercase() (Venus::Validate)
3078  
3079 The uppercase method uppercases the value and returns the invocant.
3080  
3081 I<Since C<4.15>>
3082  
3083 =over 4
3084  
3085 =item uppercase example 1
3086  
3087 package main;
3088  
3089 use Venus::Validate;
3090  
3091 my $validate = Venus::Validate->new('hello world');
3092  
3093 my $uppercase = $validate->uppercase;
3094  
3095 # bless(..., "Venus::Validate")
3096  
3097 # $uppercase->value;
3098  
3099 # "HELLO WORLD"
3100  
3101 =back
3102  
3103 =cut
3104  
3105 =head2 value
3106  
3107 value() (any)
3108  
3109 The value method returns the value being validated.
3110  
3111 I<Since C<4.15>>
3112  
3113 =over 4
3114  
3115 =item value example 1
3116  
3117 # given: synopsis;
3118  
3119 my $value = $validate->value;
3120  
3121 # "hello"
3122  
3123 =back
3124  
3125 =cut
3126  
3127 =head2 yesno
3128  
3129 yesno() (Venus::Validate)
3130  
3131 The yesno method is shorthand for calling L</type> with C<"yesno">. This method
3132 is a validator and uses L</issue_info> to capture validation errors.
3133  
3134 I<Since C<4.15>>
3135  
3136 =over 4
3137  
3138 =item yesno example 1
3139  
3140 package main;
3141  
3142 use Venus::Validate;
3143  
3144 my $validate = Venus::Validate->new('Yes');
3145  
3146 my $yesno = $validate->yesno;
3147  
3148 # bless(..., "Venus::Validate")
3149  
3150 # $validate->is_valid;
3151  
3152 # true
3153  
3154 =back
3155  
3156 =over 4
3157  
3158 =item yesno example 2
3159  
3160 package main;
3161  
3162 use Venus::Validate;
3163  
3164 my $validate = Venus::Validate->new('No');
3165  
3166 my $yesno = $validate->yesno;
3167  
3168 # bless(..., "Venus::Validate")
3169  
3170 # $validate->is_valid;
3171  
3172 # true
3173  
3174 =back
3175  
3176 =over 4
3177  
3178 =item yesno example 3
3179  
3180 package main;
3181  
3182 use Venus::Validate;
3183  
3184 my $validate = Venus::Validate->new(1);
3185  
3186 my $yesno = $validate->yesno;
3187  
3188 # bless(..., "Venus::Validate")
3189  
3190 # $validate->is_valid;
3191  
3192 # true
3193  
3194 =back
3195  
3196 =over 4
3197  
3198 =item yesno example 4
3199  
3200 package main;
3201  
3202 use Venus::Validate;
3203  
3204 my $validate = Venus::Validate->new(0);
3205  
3206 my $yesno = $validate->yesno;
3207  
3208 # bless(..., "Venus::Validate")
3209  
3210 # $validate->is_valid;
3211  
3212 # true
3213  
3214 =back
3215  
3216 =over 4
3217  
3218 =item yesno example 5
3219  
3220 package main;
3221  
3222 use Venus::Validate;
3223  
3224 my $validate = Venus::Validate->new('hello');
3225  
3226 my $yesno = $validate->yesno;
3227  
3228 # bless(..., "Venus::Validate")
3229  
3230 # $validate->is_valid;
3231  
3232 # false
3233  
3234 =back
3235  
3236 =cut
3237  
3238 =head1 AUTHORS
3239  
3240 Awncorp, C<awncorp@cpan.org>
3241  
3242 =cut
3243  
3244 =head1 LICENSE
3245  
3246 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
3247  
3248 This program is free software, you can redistribute it and/or modify it under
3249 the terms of the Apache license version 2.0.
3250  
3251 =cut
« blib/lib/Venus/Run.pm ↑ Summary blib/lib/Venus/Error.pm »