blib/lib/DBIx/Class/Validation/Structure.pm

filter: Statementstmt 83.5% 137 / 164 Branchbran 71.3% 77 / 108 Conditioncond 71.9% 77 / 107 MC/DCmcdc 54.2% 32 / 59 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 73.9% 340 / 460 CC 96 SCAR 43.0
CC
96
Cov
77%
validate
209.6
check_uniques
15.1
_val_password
10.2
CRAP 73.3
↑ Summary Build »
1 package DBIx::Class::Validation::Structure;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 our $VERSION = '0.11';
7  
8 3 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use HTML::TagFilter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub validate {
14 6 ▶ my $self = shift;
Subroutine validate: called CC=36 SCAR=53.5
Pod undocumented
15 6 my @check_columns = @_;
16  
17 6 ▶ my $check_columns = { map{ $_ => 1 } @check_columns } || {};
Condition50%1/2
{map({$_, 1;} @check_columns)} || {}
l!l
60
MC/DC0%0/1
{map({$_, 1;} @check_columns)} || {}
{map({$_, 1;} @check_columns)}
Truth table50%1/2
A
A= {map({$_, 1;} @check_columns)}
Aresult
11
00
18  
19 6 my $source = $self->result_source;
20 6 my %data = $self->get_columns;
21 6 my $columns = $source->columns_info;
22  
23 6 my ($error, @error_list, $stmt);
24  
25 6 for my $column ( keys %$columns ) {
26  
27 36 ▶ if ( ( not keys %$check_columns ) or $check_columns->{$column} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %$check_columns or $check_columns->{$column}) 36 0 36
Condition33%1/3
not keys %$check_columns or $check_columns->{$column}
l!l&&r!l&&!r
3600
MC/DC0%0/2
not keys %$check_columns or $check_columns->{$column}
not keys %$check_columns $check_columns->{$column}
Truth table33%1/3
not(A) or B
A= not keys %$check_columnsB= $check_columns->{$column}
ABresult
1X1
011
000
28  
29 36 ▶ if ($columns->{$column}{validation_function} and ref $columns->{$column}{validation_function} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns->{$column}{'validation_function'} and ref $columns->{$column}{'validation_function'} eq 'CODE') { } 0 36 36
Condition33%1/3
$columns->{$column}{"validation_function"} and ref $columns->{$column}{"validation_function"} eq "CODE"
!ll&&!rl&&r
3600
MC/DC0%0/2
$columns->{$column}{"validation_function"} and ref $columns->{$column}{"validation_function"} eq "CODE"
$columns->{$column}{"validation_function"} ref $columns->{$column}{"validation_function"} eq "CODE"
Truth table33%1/3
A and B
A= $columns->{$column}{"validation_function"}B= ref $columns->{$column}{"validation_function"} eq "CODE"
ABresult
0X0
100
111
30 ($data{$column}, $error) = $columns->{$column}{validation_function}(
31 info => $columns->{$column},
32 0 value => $data{$column},
33 data => \%data,
34 self => $self,
35 );
36 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
37 } else {
38 36 ▶ my $mand = (defined $columns->{$column}{is_nullable} and $columns->{$column}{is_nullable} == 1 or ( defined $columns->{$column}{is_auto_increment} and $columns->{$column}{is_auto_increment} == 1 ) ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $columns->{$column}{'is_nullable'} && $columns->{$column}{'is_nullable'} == 1 || defined $columns->{$column}{'is_auto_increment'} && $columns->{$column}{'is_auto_increment'} == 1 ? : 18 18 36
Condition77%7/9
defined $columns->{$column}{"is_nullable"} and $columns->{$column}{"is_nullable"} == 1 or defined $columns->{$column}{"is_auto_increment"} and $columns->{$column}{"is_auto_increment"} == 1
l!l&&r!l&&!r
12618
defined $columns->{$column}{"is_nullable"} and $columns->{$column}{"is_nullable"} == 1
!ll&&!rl&&r
24012
defined $columns->{$column}{"is_auto_increment"} and $columns->{$column}{"is_auto_increment"} == 1
!ll&&!rl&&r
1806
MC/DC50%2/4
defined $columns->{$column}{"is_nullable"} and $columns->{$column}{"is_nullable"} == 1 or defined $columns->{$column}{"is_auto_increment"} and $columns->{$column}{"is_auto_increment"} == 1
defined $columns->{$column}{"is_nullable"} $columns->{$column}{"is_nullable"} == 1 defined $columns->{$column}{"is_auto_increment"} $columns->{$column}{"is_auto_increment"} == 1
Truth table42%3/7
A and B or C and D
A= defined $columns->{$column}{"is_nullable"}B= $columns->{$column}{"is_nullable"} == 1C= defined $columns->{$column}{"is_auto_increment"}D= $columns->{$column}{"is_auto_increment"} == 1
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
39 36 ▶ my $val_type = (defined $columns->{$column}{val_override}) ? $columns->{$column}{val_override} : $columns->{$column}{data_type};
Branch: 1 branch
BranchTrueFalseTotal
defined $columns->{$column}{'val_override'} ? : 6 30 36
40  
41 36 ▶ if ($val_type eq 'email') {
Branch: 6 branches
BranchTrueFalseTotal
if ($val_type eq 'email') { } 6 30 36
elsif ($val_type eq 'varchar' or $val_type eq 'text') { } 24 6 30
elsif ($val_type eq 'password') { } 0 6 6
elsif ($val_type eq 'selected') { } 0 6 6
elsif ($val_type eq 'integer' or $val_type =~ /int/g) { } 6 0 6
elsif ($val_type eq 'number') { } 0 0 0
42 6 ($data{$column}, $error) = _val_email( $mand, $data{$column} );
43 6 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 6 6
44 ▶ } elsif ($val_type eq 'varchar' or $val_type eq 'text') {
Condition66%2/3
$val_type eq "varchar" or $val_type eq "text"
l!l&&r!l&&!r
2406
MC/DC50%1/2
$val_type eq "varchar" or $val_type eq "text"
$val_type eq "varchar" $val_type eq "text"
Truth table66%2/3
A or B
A= $val_type eq "varchar"B= $val_type eq "text"
ABresult
1X1
011
000
45 24 ($data{$column}, $error) = _val_text( $mand, $columns->{$column}{size}, $data{$column} );
46 24 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 24 24
47 } elsif ($val_type eq 'password') {
48 0 ($data{$column}, $error) = _val_password( $mand, $columns->{$column}{size}, $data{$column} );
49 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
50 } elsif ($val_type eq 'selected') {
51 0 ▶ if ($columns->{$column}{data_type} eq 'varchar' or $columns->{$column}{data_type} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns->{$column}{'data_type'} eq 'varchar' or $columns->{$column}{'data_type'} eq 'text') { } 0 0 0
Condition0%0/3
$columns->{$column}{"data_type"} eq "varchar" or $columns->{$column}{"data_type"} eq "text"
l!l&&r!l&&!r
000
MC/DC0%0/2
$columns->{$column}{"data_type"} eq "varchar" or $columns->{$column}{"data_type"} eq "text"
$columns->{$column}{"data_type"} eq "varchar" $columns->{$column}{"data_type"} eq "text"
Truth table0%0/3
A or B
A= $columns->{$column}{"data_type"} eq "varchar"B= $columns->{$column}{"data_type"} eq "text"
ABresult
1X1
011
000
52 0 ($data{$column}, $error) = _val_text( 0, $columns->{$column}{size}, $data{$column} );
53 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
54 } else {
55 0 ($data{$column}, $error) = _val_int( 0, $data{$column} );
56 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
57 }
58 0 ($data{$column}, $error) = _val_selected( $data{$column} );
59 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
60 ▶ } elsif ($val_type eq 'integer' or $val_type =~ /int/g) {
Condition33%1/3
$val_type eq "integer" or $val_type =~ /int/g
l!l&&r!l&&!r
600
MC/DC0%0/2
$val_type eq "integer" or $val_type =~ /int/g
$val_type eq "integer" $val_type =~ /int/g
Truth table33%1/3
A or B
A= $val_type eq "integer"B= $val_type =~ /int/g
ABresult
1X1
011
000
61 6 ($data{$column}, $error) = _val_int( $mand, $data{$column} );
62 6 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 6 6
63 } elsif ($val_type eq 'number') {
64 0 ($data{$column}, $error) = _val_number( $mand, $columns->{$column}{size}, $data{$column} );
65 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
66 } else {
67 0 ($data{$column}, $error) = _val_text( $mand, $columns->{$column}{size}, $data{$column} );
68 0 ▶ if ( $error-> { msg } ) { push @error_list, { $column => $error->{ msg } }; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"msg"}) 0 0 0
69 }
70  
71 # If the column is auto_increment and there is no value set, set it to undef
72 # @TODO decide if this should be deleted all together
73 36 ▶ $data{$column} = undef if $columns->{$column}{is_auto_increment} and not $data{$column};
Branch: 1 branch
BranchTrueFalseTotal
if $columns->{$column}{"is_auto_increment"} and not $data{$column} 4 32 36
Condition100%3/3
$columns->{$column}{"is_auto_increment"} and not $data{$column}
!ll&&!rl&&r
3024
MC/DC100%2/2
$columns->{$column}{"is_auto_increment"} and not $data{$column}
$columns->{$column}{"is_auto_increment"} not $data{$column}
Truth table100%3/3
A and not(B)
A= $columns->{$column}{"is_auto_increment"}B= not $data{$column}
ABresult
0X0
100
111
74 }
75 }
76 }
77  
78 6 ▶ unless (@error_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@error_list) 6 0 6
79 # Check the unique constraints
80 6 @error_list = check_uniques($self, \%data, $check_columns);
81 }
82  
83 6 $self->set_columns(\%data);
84  
85 6 ▶ if (@error_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@error_list) 4 2 6
86 4 return { 'errors' => \@error_list };
87 }
88 2 return {};
89 }
90  
91 # Returns whether any of the primary columns have changed
92 # @TODO Write tests for this function
93 sub primary_cols_have_changed {
94 1 ▶ my $self = shift;
Subroutine primary_cols_have_changed: called CC=3 SCAR=11.1
Pod undocumented
95 1 foreach ( $self->result_source->primary_columns ) {
96 1 ▶ return 1 if $self->is_column_changed($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_column_changed($_) 0 1 1
97 }
98  
99 1 return 0;
100 }
101  
102 sub check_uniques {
103 6 ▶ my $self = shift;
Subroutine check_uniques: called CC=15 SCAR=27.1
Pod undocumented
104 6 my $data = shift;
105 6 my $check_columns = shift;
106 6 my $source = $self->result_source;
107 6 my %unique_constraints = $source->unique_constraints();
108  
109 6 my %errors;
110  
111 6 foreach my $constraint ( keys %unique_constraints ) {
112  
113 # Skip the primary constraint uniqueness test if self is in_storage
114 # and the primary columns haven't changed
115 24 ▶ next if $constraint eq 'primary' and $self->in_storage and not primary_cols_have_changed($self);
Branch: 1 branch
BranchTrueFalseTotal
if $constraint eq "primary" and $self->in_storage and not primary_cols_have_changed($self) 1 23 24
Condition83%5/6
$constraint eq "primary" and $self->in_storage and not primary_cols_have_changed($self)
!ll&&!rl&&r
2301
$constraint eq "primary" and $self->in_storage
!ll&&!rl&&r
1851
MC/DC66%2/3
$constraint eq "primary" and $self->in_storage and not primary_cols_have_changed($self)
$constraint eq "primary" $self->in_storage not primary_cols_have_changed($self)
Truth table75%3/4
A and B and not(C)
A= $constraint eq "primary"B= $self->in_storageC= not primary_cols_have_changed($self)
ABCresult
0XX0
10X0
1100
1111
116  
117 my $search = {
118 map {
119 41 ▶ next unless ( not keys %$check_columns ) or $check_columns->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless not keys %$check_columns or $check_columns->{$_} 0 41 41
Condition33%1/3
not keys %$check_columns or $check_columns->{$_}
l!l&&r!l&&!r
4100
MC/DC0%0/2
not keys %$check_columns or $check_columns->{$_}
not keys %$check_columns $check_columns->{$_}
Truth table33%1/3
not(A) or B
A= not keys %$check_columnsB= $check_columns->{$_}
ABresult
1X1
011
000
120 23 ($_ => $data->{$_}) } @{ $unique_constraints{$constraint} }
121 };
122  
123 # Exclude this entries primary keys to the search for dupes
124 # to not detect itself when updating.
125 23 ▶ unless ( $constraint eq 'primary' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($constraint eq "primary") 18 5 23
126 18 for my $column ( $source->primary_columns ) {
127 $search->{$column} = {
128 18 '!=' => $data->{$column},
129 };
130 }
131 }
132  
133 # If there is an entry with the combined value defined above...
134 23 ▶ if ( $source->resultset->count($search) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->resultset->count($search)) 5 18 23
135 5 foreach my $key ( @{ $unique_constraints{$constraint} } ) {
136 10 ▶ $errors{$key} = [] unless defined $errors{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errors{$key} 9 1 10
137 10 my @other_fields = @{ $unique_constraints{$constraint} };
138 # Remove the field so we get a list of other fields in the
139 # combination
140 10 my $index = 0;
141 10 ▶ $index++ until $other_fields[$index] eq $key;
Branch: 1 branch
BranchTrueFalseTotal
$other_fields[$index] eq $key or do { ++$index } 7 10 17
142 10 splice(@other_fields, $index, 1);
143 # If there are no keys other than the key that isnt unique,
144 # then write the error as singular else explain the combination.
145 10 ▶ if ( $#other_fields >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#other_fields >= 0) { } 8 2 10
146 8 push @{$errors{$key}}, { $key => 'must be unique when combined with '.join(', ',@other_fields) };
147 } else {
148 2 push @{$errors{$key}}, { $key => 'must be unique' };
149 }
150 }
151 }
152 }
153  
154 6 ▶ if ( %errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%errors) { } 4 2 6
155 # Convert hash into the array of hashrefs like the validate returns
156 4 return map { { $_ => join( ' and ', map { values %$_ } @{ $errors{$_} } ) } } keys %errors;
157 } else {
158 2 return ();
159 }
160 }
161  
162 sub insert {
163 5 ▶ my $self = shift;
Subroutine insert: called CC=2 SCAR=6.9
Pod undocumented
164 5 my $result = $self->validate;
165 # If errors return the result
166 5 ▶ if ($result->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'errors'}) { } 4 1 5
167 4 return $result;
168 } else {
169 # Else do the normal insert
170 1 $self->next::method(@_);
171 }
172 }
173  
174 sub update {
175 1 ▶ my $self = shift;
Subroutine update: called CC=3 SCAR=11.6
Pod undocumented
176 1 my $columns = shift;
177  
178 1 ▶ $self->set_inflated_columns($columns) if $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $columns 1 0 1
179  
180 1 my $result = $self->validate;
181 # If errors return the result
182 1 ▶ if ($result->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'errors'}) { } 0 1 1
183 0 return $result;
184 } else {
185 # Else do the normal update
186 1 $self->next::method(@_);
187 }
188 }
189  
190 # =============== Validatators ===============
191  
192 sub _val_email {
193 11 ▶ my ($mand, $value) = @_;
Subroutine _val_email: called CC=6 SCAR=17.9
194 11 ▶ if (not defined $value) { $value = ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 10 11
195 11 ▶ if ( !Email::Valid->address($value) && $mand ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not 'Email::Valid'->address($value) and $mand) { } 1 10 11
elsif (not 'Email::Valid'->address($value) and $value) { } 1 9 10
Condition100%3/3
not "Email::Valid"->address($value) and $mand
!ll&&!rl&&r
731
MC/DC100%2/2
not "Email::Valid"->address($value) and $mand
not "Email::Valid"->address($value) $mand
Truth table100%3/3
not(A) and B
A= not "Email::Valid"->address($value)B= $mand
ABresult
0X0
100
111
196 1 return ( undef, { msg => 'address is blank or not valid' } );
197 ▶ } elsif ( !Email::Valid->address($value) && $value ) {
Condition100%3/3
not "Email::Valid"->address($value) and $value
!ll&&!rl&&r
721
MC/DC100%2/2
not "Email::Valid"->address($value) and $value
not "Email::Valid"->address($value) $value
Truth table100%3/3
not(A) and B
A= not "Email::Valid"->address($value)B= $value
ABresult
0X0
100
111
198 1 return ( undef, { msg => 'address is blank or not valid' } );
199 } else {
200 9 return $value;
201 }
202 }
203  
204 sub _val_text {
205 30 ▶ my ($mand, $len, $value) = @_;
Subroutine _val_text: called CC=10 SCAR=23.1
206  
207 # To ensure the text is correctly encoded etc. SZ 7/12/12
208 #my $decoder = Encode::Guess->guess($value); # First guess the decoder
209 #if (ref($decoder)){
210 # $value = $decoder->decode($value); # If a decoder is found, then decode.
211 #}
212 #$value = Encode::encode_utf8($value); # If there is no decoder, assume its UTF8
213  
214 30 ▶ if ($mand && (!$value || $value =~ /bogus="1"/)) { #tiny mce
Branch: 3 branches
BranchTrueFalseTotal
if ($mand and !$value || $value =~ /bogus="1"/) { } 1 29 30
elsif ($len and length $value and length $value > $len) { } 1 28 29
elsif ($value and not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]) { } 1 27 28
Condition83%5/6
$mand and !$value || $value =~ /bogus="1"/
!ll&&!rl&&r
17121
not $value or $value =~ /bogus="1"/
l!l&&r!l&&!r
1012
MC/DC66%2/3
$mand and !$value || $value =~ /bogus="1"/
$mand not $value $value =~ /bogus="1"/
Truth table75%3/4
A and not(B) or C
A= $mandB= not $valueC= $value =~ /bogus="1"/
ABCresult
0XX0
1000
11X1
1011
215 1 return (undef, { msg => 'cannot be blank' });
216 ▶ } elsif ($len && length($value) && (length($value) > $len) ) {
Condition83%5/6
$len and length $value and length $value > $len
!ll&&!rl&&r
10181
$len and length $value
!ll&&!rl&&r
01019
MC/DC66%2/3
$len and length $value and length $value > $len
$len length $value length $value > $len
Truth table75%3/4
A and B and C
A= $lenB= length $valueC= length $value > $len
ABCresult
0XX0
10X0
1100
1111
217 1 return (undef, { msg => 'is limited to '.$len.' characters' });
218 ▶ } elsif ($value && $value !~ /^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\?\;\n\r\<\>\/\@äÄöÖüÜßéÉáÁíÍ]*)$/) {
Condition100%3/3
$value and not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
!ll&&!rl&&r
10171
MC/DC100%2/2
$value and not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
$value not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
Truth table100%3/3
A and not(B)
A= $valueB= not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
ABresult
0X0
100
111
219 1 return (undef, { msg => 'can only use letters, 0-9 and -.,\'\"!&#$?:()=%<>;/@ (do not cut and paste from a Word document, you must Save As text only)' });
220 } else {
221 27 my $tf = new HTML::TagFilter;
222 27 ▶ if ($value) { # This is to prevent empty strings from returning as the last regex match.
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 17 10 27
223 17 return ($tf->filter($1)); # $1 is a tricky value. If value is blank $1 will be the last regex match.
224 } else {
225 10 return ''; # Take that $1. Conditional statement to the face.
226 }
227 }
228 }
229  
230 # _val_password is the same as _val_text but it also allows {}s
231 sub _val_password {
232 7 ▶ my ($mand, $len, $value) = @_;
Subroutine _val_password: called CC=10 SCAR=23.2
233  
234 # To ensure the text is correctly encoded etc. SZ 7/12/12
235 #my $decoder = Encode::Guess->guess($value); # First guess the decoder
236 #if (ref($decoder)){
237 # $value = $decoder->decode($value); # If a decoder is found, then decode.
238 #}
239 #$value = Encode::encode_utf8($value); # If there is no decoder, assume its UTF8
240  
241 7 ▶ if ($mand && (!$value || $value =~ /bogus="1"/)) { #tiny mce
Branch: 3 branches
BranchTrueFalseTotal
if ($mand and !$value || $value =~ /bogus="1"/) { } 1 6 7
elsif ($len and length $value and length $value > $len) { } 1 5 6
elsif ($value and not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\{\}\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]) { } 1 4 5
Condition50%3/6
$mand and !$value || $value =~ /bogus="1"/
!ll&&!rl&&r
601
not $value or $value =~ /bogus="1"/
l!l&&r!l&&!r
100
MC/DC33%1/3
$mand and !$value || $value =~ /bogus="1"/
$mand not $value $value =~ /bogus="1"/
Truth table50%2/4
A and not(B) or C
A= $mandB= not $valueC= $value =~ /bogus="1"/
ABCresult
0XX0
1000
11X1
1011
242 1 return (undef, { msg => 'cannot be blank' });
243 ▶ } elsif ($len && length($value) && (length($value) > $len) ) {
Condition83%5/6
$len and length $value and length $value > $len
!ll&&!rl&&r
231
$len and length $value
!ll&&!rl&&r
024
MC/DC66%2/3
$len and length $value and length $value > $len
$len length $value length $value > $len
Truth table75%3/4
A and B and C
A= $lenB= length $valueC= length $value > $len
ABCresult
0XX0
10X0
1100
1111
244 1 return (undef, { msg => 'is limited to '.$len.' characters' });
245 ▶ } elsif ($value && $value !~ /^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\{\}\?\;\n\r\<\>\/\@äÄöÖüÜßéÉáÁíÍ]*)$/) {
Condition100%3/3
$value and not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\{\}\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
!ll&&!rl&&r
221
MC/DC100%2/2
$value and not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\{\}\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
$value not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\{\}\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
Truth table100%3/3
A and not(B)
A= $valueB= not $value =~ m[^([\w \.\,\-\'\"\!\$\#\%\=\&\:\+\(\)\{\}\?\;\n\r\<\>/\@\303\244\303\204\303\266\303\226\303\274\303\234\303\237\303\251\303\211\303\241\303\201\303\255\303\215]*)$]
ABresult
0X0
100
111
246 1 return (undef, { msg => 'can only use letters, 0-9 and -.,\'\"!&#$?:()=%<>;/@ (do not cut and paste from a Word document, you must Save As text only)' });
247 } else {
248 4 my $tf = new HTML::TagFilter;
249 4 ▶ if ($value) { # This is to prevent empty strings from returning as the folder name.
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 2 2 4
250 2 return ($tf->filter($1)); # $1 is a tricky value. If value is blank $1 will be the name of the folder from the instance script.
251 } else {
252 2 return ''; # Take that $1. Conditional statement to the face.
253 }
254 }
255 }
256  
257 sub _val_int {
258 10 ▶ my ($mand, $value) = @_;
Subroutine _val_int: called CC=9 SCAR=22.5
259 10 ▶ if ( (not( defined $value ) or $value ne '0') && !$value && $mand ) {
Branch: 2 branches
BranchTrueFalseTotal
if (!defined($value) || $value ne '0' and not $value and $mand) { } 1 9 10
elsif (defined $value and $value || $value eq '0' and not $value =~ /^[-]?\d+$/) { } 1 8 9
Condition66%6/9
!defined($value) || $value ne 0 and not $value and $mand
!ll&&!rl&&r
451
!defined($value) || $value ne 0 and not $value
!ll&&!rl&&r
046
not defined $value or $value ne 0
l!l&&r!l&&!r
0100
MC/DC50%2/4
!defined($value) || $value ne 0 and not $value and $mand
not defined $value $value ne 0 not $value $mand
Truth table42%3/7
not(A) or B and not(C) and D
A= not defined $valueB= $value ne 0C= not $valueD= $mand
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
260 1 return (undef, { msg => 'cannot be blank' });
261 ▶ } elsif ( ( defined $value and ( $value or $value eq '0' ) ) and $value !~ /^[-]?\d+$/) {
Condition77%7/9
defined $value and $value || $value eq 0 and not $value =~ /^[-]?\d+$/
!ll&&!rl&&r
531
defined $value and $value || $value eq 0
!ll&&!rl&&r
054
$value or $value eq 0
l!l&&r!l&&!r
405
MC/DC50%2/4
defined $value and $value || $value eq 0 and not $value =~ /^[-]?\d+$/
defined $value $value $value eq 0 not $value =~ /^[-]?\d+$/
Truth table50%3/6
A and B or C and not(D)
A= defined $valueB= $valueC= $value eq 0D= not $value =~ /^[-]?\d+$/
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
262 1 return (undef, { msg => 'can only use numbers' });
263 } else {
264 8 return ($value);
265 }
266 }
267  
268 sub _val_selected {
269 2 ▶ my ($value) = @_;
Subroutine _val_selected: called CC=3 SCAR=11.0
270 2 ▶ if (not defined $value or $value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or $value eq '') { } 1 1 2
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
011
MC/DC50%1/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
271 1 return (undef, { msg => 'must be selected' });
272 } else {
273 1 return $value;
274 }
275 }
276  
277 sub _val_number {
278 6 ▶ my ($mand, $len, $value) = @_;
Subroutine _val_number: called CC=9 SCAR=22.0
279 6 ▶ if ((!defined $value or $value eq '') && $mand) {
Branch: 3 branches
BranchTrueFalseTotal
if (!defined($value) || $value eq '' and $mand) { } 1 5 6
elsif ($len and length $value > $len) { } 2 3 5
elsif ($value and not $value =~ /^([-\.]*\d[\d\.-]*)$/) { } 1 2 3
Condition83%5/6
!defined($value) || $value eq "" and $mand
!ll&&!rl&&r
411
not defined $value or $value eq ""
l!l&&r!l&&!r
024
MC/DC66%2/3
!defined($value) || $value eq "" and $mand
not defined $value $value eq "" $mand
Truth table60%3/5
not(A) or B and C
A= not defined $valueB= $value eq ""C= $mand
ABCresult
00X0
1X00
0100
1X11
0111
280 1 return (undef, { msg => 'cannot be blank' });
281 ▶ } elsif ($len && (length($value) > $len) ) {
Condition66%2/3
$len and length $value > $len
!ll&&!rl&&r
032
MC/DC50%1/2
$len and length $value > $len
$len length $value > $len
Truth table66%2/3
A and B
A= $lenB= length $value > $len
ABresult
0X0
100
111
282 2 return (undef, { msg => 'is limited to '.$len.' characters' });
283 ▶ } elsif ($value && $value !~ /^([-\.]*\d[\d\.-]*)$/) {
Condition100%3/3
$value and not $value =~ /^([-\.]*\d[\d\.-]*)$/
!ll&&!rl&&r
111
MC/DC100%2/2
$value and not $value =~ /^([-\.]*\d[\d\.-]*)$/
$value not $value =~ /^([-\.]*\d[\d\.-]*)$/
Truth table100%3/3
A and not(B)
A= $valueB= not $value =~ /^([-\.]*\d[\d\.-]*)$/
ABresult
0X0
100
111
284 1 return (undef, { msg => 'can only use numbers and . or -' });
285 } else {
286 2 ▶ if ($value ne '') { # This is to prevent empty strings from returning as the folder name.
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne '') { } 1 1 2
287 1 return ($1); # $1 is a tricky value. If value is blank $1 will be the name of the folder from the instance script.
288 } else {
289 1 return ''; # Take that $1. Conditional statement to the face.
290 }
291 }
292 }
293  
294  
295 3 1;
296 __END__
297  
298 =encoding utf-8
299  
300 =head1 NAME
301  
302 DBIx::Class::Validation::Structure - DBIx::Class Validation based on the column meta data
303  
304 =head1 SYNOPSIS
305  
306 package MyApp::Schema::Result::Artist;
307 use base qw/DBIx::Class::Core/;
308  
309 __PACKAGE__->load_components(qw/Validation::Structure/);
310  
311 __PACKAGE__->table('artist');
312 __PACKAGE__->add_columns(qw/ artistid name /);
313 __PACKAGE__->set_primary_key('artistid');
314 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD');
315  
316  
317 =head1 DESCRIPTION
318  
319 DBIx::Class::Validation::Structure is DBIx::Class Validation based on the column meta data set in add_columns or add_column.
320  
321 =head1 AUTHOR
322  
323 Sean Zellmer E<lt>sean@lejeunerenard.comE<gt>
324  
325 =head1 COPYRIGHT
326  
327 Copyright 2013- Sean Zellmer
328  
329 =head1 LICENSE
330  
331 This library is free software; you can redistribute it and/or modify
332 it under the same terms as Perl itself.
333  
334 =head1 SEE ALSO
335  
336 =over 8
337  
338 =item L<DBIx::Class>
339  
340 =item L<DBIx::Class::Validation>
341  
342 =item L<Email::Valid>
343  
344 =item L<HTML::TagFilter>
345  
346 =back
347  
348 =cut
↑ Summary Build »