lib/Data/Validation.pm

filter: Statementstmt 95.0% 116 / 122 Branchbran 92.8% 26 / 28 Conditioncond 63.2% 31 / 49 MC/DCmcdc 34.6% 9 / 26 Subroutinesub 82.8% 29 / 35 Podpod 100% 2 / 2 totaltotal 81.3% 213 / 262 CC 38 SCAR 17.7
CC
38
Cov
87%
__ANON__
11.6
__ANON__
11.1
check_form
9.4
CRAP 5.9
↑ Summary lib/Data/Validation/Constraints.pm »
1 package Data::Validation;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use version; our $VERSION = qv( sprintf '0.28.%d', q$Rev: 1 $ =~ /\d+/gmx );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Validation::Constants qw( EXCEPTION_CLASS FALSE HASH NUL SPC );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Validation::Constraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Validation::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Validation::Utils qw( throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Unexpected::Functions qw( FieldComparison ValidationErrors );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Unexpected::Types qw( HashRef NonZeroPositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 31 ▶has 'constraints' => is => 'ro', isa => HashRef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 2 ▶has 'fields' => is => 'ro', isa => HashRef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 77 ▶has 'filters' => is => 'ro', isa => HashRef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 1 has 'level' => is => 'ro', isa => NonZeroPositiveInt, default => 1;
24  
25 # Private functions
26 4 ▶my $_comparisons = sub {
Subroutine __ANON__: called CC=1 SCAR=2.6
27 3 ▶ return { 'eq' => sub { $_[ 0 ] eq $_[ 1 ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 0 ▶ '==' => sub { $_[ 0 ] == $_[ 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 1 ▶ 'ne' => sub { $_[ 0 ] ne $_[ 1 ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 0 ▶ '!=' => sub { $_[ 0 ] != $_[ 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 ▶ '>' => sub { $_[ 0 ] > $_[ 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 ▶ '>=' => sub { $_[ 0 ] >= $_[ 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ '<' => sub { $_[ 0 ] < $_[ 1 ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 ▶ '<=' => sub { $_[ 0 ] <= $_[ 1 ] }, };
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 1 };
36  
37 152 ▶my $_get_methods = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
38 152 ▶ return split SPC, $_[ 0 ] // NUL;
Condition50%1/2
$_[0] // ""
l!l
1520
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
39 1 };
40  
41 29 ▶my $_should_compare = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 29 ▶ return first { $_ eq 'compare' } $_get_methods->( $_[ 0 ]->{validate} );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 1 };
44  
45 # Private methods
46 13 ▶my $_filter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
47 13 my ($self, $filters, $id, $v) = @_;
48  
49 13 for my $method ($_get_methods->( $filters )) {
50 15 ▶ my $attr = { %{ $self->filters->{ $id } // {} }, method => $method, };
Condition100%2/2
$self->filters->{$id} // {}
l!l
123
MC/DC100%1/1
$self->filters->{$id} // {}
$self->filters->{$id}
Truth table100%2/2
A
A= $self->filters->{$id}
Aresult
11
00
51 15 my $dvf_obj = Data::Validation::Filters->new_from_method( $attr );
52  
53 14 $v = $dvf_obj->filter( $v );
54 }
55  
56 12 return $v;
57 1 };
58  
59 5 ▶my $_compare_fields = sub {
Subroutine __ANON__: called CC=10 SCAR=24.5
60 5 my ($self, $prefix, $form, $lhs_name) = @_;
61  
62 5 my $id = $prefix.$lhs_name;
63 5 ▶ my $constraint = $self->constraints->{ $id } // {};
Condition50%1/2
$self->constraints->{$id} // {}
l!l
50
MC/DC0%0/1
$self->constraints->{$id} // {}
$self->constraints->{$id}
Truth table50%1/2
A
A= $self->constraints->{$id}
Aresult
11
00
64 my $rhs_name = $constraint->{other_field}
65 5 ▶ or throw 'Constraint [_1] has no comparison field', [ $id ];
Branch: 1 branch
BranchTrueFalseTotal
my $rhs_name = $constraint->{"other_field"} or &throw("Constraint [_1] has no comparison field", [$id]) 4 1 5
66 4 ▶ my $op = $constraint->{operator} // 'eq';
Condition100%2/2
$constraint->{"operator"} // "eq"
l!l
22
MC/DC100%1/1
$constraint->{"operator"} // "eq"
$constraint->{"operator"}
Truth table100%2/2
A
A= $constraint->{"operator"}
Aresult
11
00
67 4 ▶ my $compare = $_comparisons->()->{ $op }
Branch: 1 branch
BranchTrueFalseTotal
my $compare = &$_comparisons()->{$op} or &throw("Constraint [_1] unknown comparison operator [_2]", [$id, $op]) 4 0 4
68 or throw 'Constraint [_1] unknown comparison operator [_2]', [ $id, $op ];
69 4 ▶ my $lhs = $form->{ $lhs_name } // NUL;
Condition50%1/2
$form->{$lhs_name} // ""
l!l
40
MC/DC0%0/1
$form->{$lhs_name} // ""
$form->{$lhs_name}
Truth table50%1/2
A
A= $form->{$lhs_name}
Aresult
11
00
70 4 ▶ my $rhs = $form->{ $rhs_name } // NUL;
Condition50%1/2
$form->{$rhs_name} // ""
l!l
40
MC/DC0%0/1
$form->{$rhs_name} // ""
$form->{$rhs_name}
Truth table50%1/2
A
A= $form->{$rhs_name}
Aresult
11
00
71  
72 4 ▶ $compare->( $lhs, $rhs ) and return;
Branch: 1 branch
BranchTrueFalseTotal
&$compare($lhs, $rhs) and return 3 1 4
73  
74 1 ▶ $lhs_name = $self->fields->{ $prefix.$lhs_name }->{label} // $lhs_name;
Condition33%1/3
$self->fields->{$prefix . $lhs_name}{"label"} // $lhs_name
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->fields->{$prefix . $lhs_name}{"label"} // $lhs_name
$self->fields->{$prefix . $lhs_name}{"label"} $lhs_name
Truth table33%1/3
A // B
A= $self->fields->{$prefix . $lhs_name}{"label"}B= $lhs_name
ABresult
1X1
011
000
75 1 ▶ $rhs_name = $self->fields->{ $prefix.$rhs_name }->{label} // $rhs_name;
Condition33%1/3
$self->fields->{$prefix . $rhs_name}{"label"} // $rhs_name
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->fields->{$prefix . $rhs_name}{"label"} // $rhs_name
$self->fields->{$prefix . $rhs_name}{"label"} $rhs_name
Truth table33%1/3
A // B
A= $self->fields->{$prefix . $rhs_name}{"label"}B= $rhs_name
ABresult
1X1
011
000
76 1 throw FieldComparison, [ $lhs_name, $op, $rhs_name ], level => $self->level;
77 1 };
78  
79 111 ▶my $_validate = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
80 111 my ($self, $valids, $id, $v) = @_;
81  
82 111 ▶ $valids !~ m{ isMandatory }mx and (not defined $v or not length $v)
Branch: 1 branch
BranchTrueFalseTotal
$valids =~ / isMandatory /mx or defined $v and length $v or return 110 1 111
Condition83%5/6
$valids =~ / isMandatory /mx or defined $v and length $v
l!l&&r!l&&!r
31071
defined $v and length $v
!ll&&!rl&&r
01107
MC/DC66%2/3
$valids =~ / isMandatory /mx or defined $v and length $v
$valids =~ / isMandatory /mx defined $v length $v
Truth table75%3/4
A or B and C
A= $valids =~ / isMandatory /mxB= defined $vC= length $v
ABCresult
1XX1
0111
00X0
0100
83 and return;
84  
85 110 ▶ my $params = $self->constraints->{ $id } // {};
Condition100%2/2
$self->constraints->{$id} // {}
l!l
5654
MC/DC100%1/1
$self->constraints->{$id} // {}
$self->constraints->{$id}
Truth table100%2/2
A
A= $self->constraints->{$id}
Aresult
11
00
86 110 ▶ my $label = $self->fields->{ $id }->{label} // $id;
Condition33%1/3
$self->fields->{$id}{"label"} // $id
l!l&&r!l&&!r
01100
MC/DC0%0/2
$self->fields->{$id}{"label"} // $id
$self->fields->{$id}{"label"} $id
Truth table33%1/3
A // B
A= $self->fields->{$id}{"label"}B= $id
ABresult
1X1
011
000
87  
88 110 for my $methods (grep { $_ ne 'compare' } $_get_methods->( $valids )) {
89 111 my @fails;
90  
91 111 for my $method (split m{ [|] }mx, $methods) {
92 my $constraint = Data::Validation::Constraints->new_from_method
93 112 ( { %{ $params }, method => $method, } );
94 110 (my $class = $method) =~ s{ \A is }{}mx;
95  
96 110 ▶ if ($constraint->validate( $v )) { @fails = (); last }
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint->validate($v)) 68 41 109
97  
98 41 push @fails, $class;
99 }
100  
101 108 ▶ @fails == 1 and throw sub { $fails[ 0 ] }, [ $label ],
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
@fails == 1 and &throw(sub { $fails[0]; } , [$label], "constraints", $params, "level", $self->level) 39 69 108
102 constraints => $params, level => $self->level;
103 69 ▶ @fails > 1 and throw 'Field [_1] is none of [_2]',
Branch: 1 branch
BranchTrueFalseTotal
@fails > 1 and &throw("Field [_1] is none of [_2]", [$label, join(" | ", @fails)], "level", $self->level) 1 68 69
104 [ $label, join ' | ', @fails ],
105 level => $self->level;
106 }
107  
108 67 return;
109 1 };
110  
111 # Public methods
112 6 ▶sub check_form { # Validate all fields on a form by repeated calling check_field
Subroutine check_form: called CC=9 SCAR=22.5
Pod documented
113 6 ▶ my ($self, $prefix, $form) = @_; my @errors = (); $prefix ||= NUL;
Condition100%2/2
$prefix ||= ""
l!l
51
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
114  
115 6 ▶ ($form and ref $form eq HASH) or throw 'Form parameter not a hash ref';
Branch: 1 branch
BranchTrueFalseTotal
$form and ref $form eq "HASH" or &throw("Form parameter not a hash ref") 5 1 6
Condition66%2/3
$form and ref $form eq "HASH"
!ll&&!rl&&r
015
MC/DC50%1/2
$form and ref $form eq "HASH"
$form ref $form eq "HASH"
Truth table66%2/3
A and B
A= $formB= ref $form eq "HASH"
ABresult
0X0
100
111
116  
117 5 for my $name (sort keys %{ $form }) {
118 30 my $id = $prefix.$name; my $conf = $self->fields->{ $id };
119  
120 30 ▶ ($conf and ($conf->{filters} or $conf->{validate})) or next;
Branch: 1 branch
BranchTrueFalseTotal
$conf and $conf->{"filters"} || $conf->{"validate"} or next 30 0 30
Condition33%2/6
$conf and $conf->{"filters"} || $conf->{"validate"}
!ll&&!rl&&r
0030
$conf->{"filters"} or $conf->{"validate"}
l!l&&r!l&&!r
0300
MC/DC0%0/3
$conf and $conf->{"filters"} || $conf->{"validate"}
$conf $conf->{"filters"} $conf->{"validate"}
Truth table25%1/4
A and B or C
A= $confB= $conf->{"filters"}C= $conf->{"validate"}
ABCresult
0XX0
1000
11X1
1011
121  
122 30 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
123 30 $form->{ $name } = $self->check_field( $id, $form->{ $name } );
124 29 ▶ $_should_compare->( $conf )
Branch: 1 branch
BranchTrueFalseTotal
&$_should_compare($conf) and $self->$_compare_fields($prefix, $form, $name) 5 24 29
125 and $self->$_compare_fields( $prefix, $form, $name );
126 }
127 30 ▶ catch { push @errors, $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
128 }
129  
130 5 ▶ @errors and throw ValidationErrors, \@errors, level => $self->level;
Branch: 1 branch
BranchTrueFalseTotal
@errors and &throw(ValidationErrors(), \@errors, "level", $self->level) 3 2 5
131  
132 2 return $form;
133 }
134  
135 117 ▶sub check_field { # Validate a single form field value
Subroutine check_field: called CC=7 SCAR=19.5
Pod documented
136 117 my ($self, $id, $v) = @_; my $conf;
137  
138 117 ▶ unless ($id and $conf = $self->fields->{ $id }
Branch: 1 branch
BranchTrueFalseTotal
unless ($id and $conf = $self->fields->{$id} and $conf->{"filters"} || $conf->{"validate"}) 2 115 117
Condition77%7/9
$id and $conf = $self->fields->{$id} and $conf->{"filters"} || $conf->{"validate"}
!ll&&!rl&&r
20115
$id and $conf = $self->fields->{$id}
!ll&&!rl&&r
11115
$conf->{"filters"} or $conf->{"validate"}
l!l&&r!l&&!r
131020
MC/DC50%2/4
$id and $conf = $self->fields->{$id} and $conf->{"filters"} || $conf->{"validate"}
$id $conf = $self->fields->{$id} $conf->{"filters"} $conf->{"validate"}
Truth table80%4/5
A and B and C or D
A= $idB= $conf = $self->fields->{$id}C= $conf->{"filters"}D= $conf->{"validate"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
139 and ($conf->{filters} or $conf->{validate})) {
140 2 throw 'Field [_1] validation configuration not found', [ $id, $v ];
141 }
142  
143 115 ▶ $conf->{filters } and $v = $self->$_filter( $conf->{filters }, $id, $v );
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"filters"} and $v = $self->$_filter($conf->{"filters"}, $id, $v) 13 102 115
144 114 ▶ $conf->{validate} and $self->$_validate( $conf->{validate}, $id, $v );
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"validate"} and $self->$_validate($conf->{"validate"}, $id, $v) 111 3 114
145  
146 71 return $v;
147 }
148  
149 1 1;
150  
151 __END__
↑ Summary lib/Data/Validation/Constraints.pm »