lib/Form/Sensible/Validator.pm

filter: Statementstmt 89.5% 60 / 67 Branchbran 86.3% 19 / 22 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 80.8% 97 / 120 CC 19 SCAR 20.0
CC
19
Cov
85%
validate
11.2
validate_field
9.6
__ANON__
2.0
CRAP 7.4
« lib/Form/Sensible/Field/FileSelector.pm ↑ Summary lib/Form/Sensible/Field/DateTime.pm »
1 package Form::Sensible::Validator;
2  
3 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Form::Sensible::Validator::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Form::Sensible::DelegateConnection;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ## this module provides the basics for validation of a Form.
10  
11 has 'config' => (
12 is => 'rw',
13 isa => 'HashRef',
14 required => 1,
15 0 ▶ default => sub { return { }; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 16 lazy => 1,
17 );
18  
19 has 'message_delegate' => (
20 is => 'rw',
21 isa => 'Form::Sensible::DelegateConnection',
22 required => 1,
23 11 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 83 ▶ return FSConnector( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 83 my $caller = shift;
26 83 my $field = shift;
27 83 my $message = shift;
28
29 83 my $fieldname = $field->display_name;
30 83 $message =~ s/_FIELDNAME_/$fieldname/g;
31
32 83 return $message;
33 11 });
34 },
35 16 lazy => 1,
36 coerce => 1,
37 # additional options
38 );
39  
40 # returns a Form::Sensible::Validator::Result
41 57 ▶sub validate {
Subroutine validate: called CC=11 SCAR=24.1
Pod documented
42 57 my ($self, $form) = @_;
43
44 ## validation follows this process: Validate each field in order, using
45 ## the order provided by the form. If all of those succeed, then proceed to
46 ## complete form validation if provided.
47
48 ## Prepare our validation result - it will be 'valid' unless we fail something.
49 57 my $validation_result = Form::Sensible::Validator::Result->new();
50
51 57 foreach my $field ($form->get_fields()) {
52 276 ▶ if(ref $field eq 'Form::Sensible::Field::SubForm') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field eq "Form::Sensible::Field::SubForm") 1 275 276
53 1 $validation_result->merge_from_result($field->form->validate);
54 1 next;
55 }
56  
57 275 my $fieldname = $field->name;
58 275 my $results = $self->validate_field($field);
59
60 275 ▶ if (scalar keys %{$results}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$results;}) 76 199 275
61 76 ▶ if (exists($results->{'errors'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $results->{"errors"}) 75 1 76
62 75 foreach my $error (@{$results->{'errors'}}) {
63 82 $validation_result->add_error($fieldname, $self->message_delegate->($self, $field, $error));
64 }
65 }
66
67 76 ▶ if (exists($results->{'missing'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $results->{"missing"}) 1 75 76
68 1 foreach my $missing (@{$results->{'missing'}}) {
69 1 $validation_result->add_error($fieldname, $self->message_delegate->($self, $field, $missing));
70 }
71 }
72 }
73 }
74
75 57 ▶ if ($validation_result->is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validation_result->is_valid) 7 50 57
76 7 ▶ if (defined($form->validation->{'code'}) && ref($form->validation->{'code'}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form->validation->{"code"} and ref $form->validation->{"code"} eq "CODE") 0 7 7
Condition33%1/3
defined $form->validation->{"code"} and ref $form->validation->{"code"} eq "CODE"
!ll&&!rl&&r
700
MC/DC0%0/2
defined $form->validation->{"code"} and ref $form->validation->{"code"} eq "CODE"
defined $form->validation->{"code"} ref $form->validation->{"code"} eq "CODE"
Truth table33%1/3
A and B
A= defined $form->validation->{"code"}B= ref $form->validation->{"code"} eq "CODE"
ABresult
0X0
100
111
77 0 my $results = $form->validation->{'code'}->($form, $validation_result);
78 }
79 }
80 57 return $validation_result;
81 }
82  
83 275 ▶sub validate_field {
Subroutine validate_field: called CC=9 SCAR=22.7
Pod documented
84 275 my ($self, $field) = @_;
85
86 275 my $results = {};
87 275 my @errors;
88 275 ▶ if (defined($field->value) && $field->value ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $field->value and $field->value ne '') { } 265 10 275
elsif ($field->validation->{'required'}) { } 1 9 10
Condition66%2/3
defined $field->value and $field->value ne ""
!ll&&!rl&&r
100265
MC/DC50%1/2
defined $field->value and $field->value ne ""
defined $field->value $field->value ne ""
Truth table66%2/3
A and B
A= defined $field->valueB= $field->value ne ""
ABresult
0X0
100
111
89
90 ## field has value, so we run the field validators
91 ## first regex.
92 # if (defined($field->validation->{'regex'})) {
93 # my $invalid = $self->validate_field_with_regex($field, $field->validation->{'regex'});
94 # if ($invalid) {
95 # push @errors, $invalid;
96 # }
97 # }
98 ## if we have a coderef, and we passed regex, run the coderef. Otherwise we
99 ## don't bother.
100 # if (defined($field->validation->{'code'}) && $#errors == -1) {
101 # my $invalid = $self->validate_field_with_coderef($field, $field->validation->{'code'});
102 # if ($invalid) {
103 # push @errors, $invalid;
104 # }
105 # }
106 ## finally, we run the fields internal validate routine
107 265 my @results = $field->validate($self);
108 265 foreach my $error (@results) {
109 82 ▶ if (defined($error) && $error ne '0') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error and $error ne 0) 82 0 82
Condition33%1/3
defined $error and $error ne 0
!ll&&!rl&&r
0082
MC/DC0%0/2
defined $error and $error ne 0
defined $error $error ne 0
Truth table33%1/3
A and B
A= defined $errorB= $error ne 0
ABresult
0X0
100
111
110 82 push @errors, $error;
111 }
112 }
113 } elsif ($field->validation->{'required'}) {
114 ## field was required but was empty.
115 1 ▶ if (exists($field->validation->{'missing_message'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->validation->{'missing_message'}) { } 0 1 1
116 0 $results->{'missing'} = [ $field->validation->{'missing_message'} ];
117 } else {
118 1 $results->{'missing'} = [ '_FIELDNAME_ is a required field but was not provided.' ];
119 }
120 }
121 275 ▶ if ($#errors > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#errors > -1) 75 200 275
122 75 $results->{'errors'} = [ @errors ];
123 }
124 275 return $results;
125 }
126  
127 0 ▶sub validate_field_with_regex {
Subroutine validate_field_with_regex: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($self, $field, $regex) = @_;
129
130 0 return $field->validate_with_regex($regex);
131 # if (ref($regex) ne 'Regexp') {
132 # $regex = qr/$regex/;
133 # }
134
135 # if ($field->value !~ $regex) {
136 # if (exists($field->validation->{'invalid_message'})) {
137 # return $field->validation->{'invalid_message'};
138 # } else {
139 # return "_FIELDNAME_ is invalid.";
140 # }
141 # } else {
142 # return 0;
143 # }
144 }
145  
146 0 ▶sub validate_field_with_coderef {
Subroutine validate_field_with_coderef: not called CC=1 SCAR=6.9
Pod documented
147 0 my ($self, $field, $code) = @_;
148  
149 0 return $field->validate_with_coderef($code);
150  
151 # if (ref($code) ne 'CODE') {
152 # croak('Bad coderef provided to validate_field_with_coderef');
153 # }
154
155 # my $results = $code->($field->value, $field);
156 #
157 ## if we get $results of 0 or a message, we return it.
158 ## if we get $results of simply one, we generate the invalid message
159 # if ($results && $results eq "1") {
160 # if (exists($field->validation->{invalid_message})) {
161 # return $field->validation->{invalid_message};
162 # } else {
163 # return "_FIELDNAME_ is invalid.";
164 # }
165 # } else {
166 # return $results;
167 # }
168 }
169  
170  
171 16 __PACKAGE__->meta->make_immutable;
172 16 1;
173  
174 __END__
« lib/Form/Sensible/Field/FileSelector.pm ↑ Summary lib/Form/Sensible/Field/DateTime.pm »