Catmandu/Validator.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 95.4% 21 / 22 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 98.2% 113 / 115 CC 16 SCAR 18.5
CC
16
Cov
98%
_process_record
11.0
validate
4.0
is_valid
3.0
CRAP 6.3
« Catmandu/CQLSearchable.pm ↑ Summary Catmandu/Fix/Bind/timeout.pm »
1 package Catmandu::Validator;
2  
3 7 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = '1.2025';
6  
7 7 ▶use Catmandu::Util qw(:is);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 requires 'validate_data';
12  
13 7 has 'last_errors' =>
14 (is => 'rwp', clearer => '_clear_last_errors', init_arg => undef,);
15  
16 7 has 'after_callback' => (is => 'ro', clearer => 1,);
17  
18 7 has 'error_callback' => (is => 'ro', clearer => 1,);
19  
20 7 has 'error_field' => (is => 'ro', clearer => 1,);
21  
22 has ['valid_count', 'invalid_count'] =>
23 32 ▶ (is => 'rwp', init_arg => undef, default => sub {0},);
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 10 ▶sub is_valid {
Subroutine is_valid: called CC=3 SCAR=11.0
Pod documented
26 10 my ($self, $data) = @_;
27  
28 10 ▶ if (!is_hash_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_hash_ref($data)) 0 10 10
29 0 Catmandu::BadArg->throw('Cannot validate data of this type');
30 }
31  
32 10 $self->_clear_last_errors;
33 10 $self->_set_valid_count(0);
34 10 $self->_set_invalid_count(0);
35  
36 10 my $errors = $self->validate_data($data);
37  
38 10 ▶ if ($errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) { } 6 4 10
39 6 $self->_set_last_errors($errors);
40 6 $self->_set_invalid_count(1);
41 6 return 0;
42 }
43 else {
44 4 $self->_set_valid_count(1);
45 }
46  
47 4 1;
48 }
49  
50 12 ▶sub validate {
Subroutine validate: called CC=4 SCAR=13.9
Pod documented
51 12 my ($self, $data) = @_;
52  
53 12 $self->_set_valid_count(0);
54 12 $self->_set_invalid_count(0);
55  
56 # handle a single record
57 12 ▶ if (is_hash_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($data)) 5 7 12
58 5 return $self->_process_record($data);
59 }
60  
61 # handle arrayref, returns a new arrayref
62 7 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($data)) 5 2 7
63 5 return [grep {defined} map {$self->_process_record($_)} @$data];
64 }
65  
66 # handle iterators, returns a new iterator
67 2 ▶ if (is_invocant($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_invocant($data)) 1 1 2
68 1 ▶ return $data->select(sub {$self->_process_record($_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
69 }
70  
71 1 Catmandu::BadArg->throw('Cannot validate data of this type');
72 }
73  
74 24 ▶sub _process_record {
Subroutine _process_record: called CC=11 SCAR=24.0
75 24 my ($self, $data) = @_;
76  
77 24 ▶ my $error_field
Branch: 1 branch
BranchTrueFalseTotal
($self->error_field || 0) eq '1' ? : 3 21 24
Condition100%2/2
$self->error_field || 0
l!l
618
MC/DC100%1/1
$self->error_field || 0
$self->error_field
Truth table100%2/2
A
A= $self->error_field
Aresult
11
00
78 = ($self->error_field || 0) eq '1'
79 ? '_validation_errors'
80 : $self->error_field;
81  
82 24 $self->_clear_last_errors;
83 24 my $errors = $self->validate_data($data);
84 24 $self->_set_last_errors($errors);
85  
86 24 ▶ if ($errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) { } 14 10 24
87 14 $self->_set_invalid_count(1 + $self->invalid_count);
88 }
89 else {
90 10 $self->_set_valid_count(1 + $self->valid_count);
91 }
92  
93 24 ▶ if ($errors && $error_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors and $error_field) 4 20 24
Condition100%3/3
$errors and $error_field
!ll&&!rl&&r
10104
MC/DC100%2/2
$errors and $error_field
$errors $error_field
Truth table100%3/3
A and B
A= $errorsB= $error_field
ABresult
0X0
100
111
94 4 $data->{$error_field} = $errors;
95 }
96  
97 24 ▶ if ($self->after_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->after_callback) 8 16 24
98 8 return $self->after_callback->($data, $errors);
99 }
100  
101 16 ▶ if ($errors && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors and $self->error_callback) 2 14 16
Condition100%3/3
$errors and $self->error_callback
!ll&&!rl&&r
772
MC/DC100%2/2
$errors and $self->error_callback
$errors $self->error_callback
Truth table100%3/3
A and B
A= $errorsB= $self->error_callback
ABresult
0X0
100
111
102 2 $self->error_callback->($data, $errors);
103 2 return;
104 }
105  
106 14 ▶ return if $errors && !$error_field;
Branch: 1 branch
BranchTrueFalseTotal
if $errors and not $error_field 3 11 14
Condition100%3/3
$errors and not $error_field
!ll&&!rl&&r
743
MC/DC100%2/2
$errors and not $error_field
$errors not $error_field
Truth table100%3/3
A and not(B)
A= $errorsB= not $error_field
ABresult
0X0
100
111
107  
108 11 $data;
109 }
110  
111 7 1;
112  
113 __END__
« Catmandu/CQLSearchable.pm ↑ Summary Catmandu/Fix/Bind/timeout.pm »