LIVR/Rules/String.pm

filter: Statementstmt 100% 101 / 101 Branchbran 96.0% 73 / 76 Conditioncond 95.2% 40 / 42 MC/DCmcdc 92.3% 24 / 26 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 8 totaltotal 94.5% 259 / 274 CC 55 SCAR 18.5
CC
55
Cov
98%
__ANON__
13.0
__ANON__
10.0
__ANON__
6.0
CRAP 6.3
« LIVR/Rules/Conditional.pm ↑ Summary LIVR/Rules/Numeric.pm »
1 package Validator::LIVR::Rules::String;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '2.22';
7  
8 65 ▶sub one_of {
Subroutine one_of: called CC=3 SCAR=11.0
Pod undocumented
9 65 my $allowed_values;
10 65 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 14 51 65
11 14 $allowed_values = $_[0];
12 } else {
13 51 $allowed_values = [@_];
14 51 pop @$allowed_values; # pop rule_builders
15 }
16  
17 # LIVR 2.5: booleans are compared as strings, true equals "true"
18 65 my @allowed_strings = map { _boolean_string($_) } @$allowed_values;
19 65 my $has_boolean = grep { _is_boolean($_) } @$allowed_values;
20  
21 # The 2.0 form: Perl strings, where a boolean is "1" or "0". Of copies: before Perl 5.36,
22 # a number used as a string is a string for JSON encoders, and the output is the number
23 65 ▶ my @perl_strings = map { my $copy = $_; defined($copy) ? "$copy" : '' } @$allowed_values;
Branch: 1 branch
BranchTrueFalseTotal
defined $copy ? : 221 0 221
24  
25 59 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
26 59 my ($value, undef, $output_ref) = @_;
27 59 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 6 53 59
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1553
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
28 53 ▶ return 'FORMAT_ERROR' if ref($value) && ref($value) !~ 'Boolean';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and not ref($value) =~ /Boolean/ 4 49 53
Condition100%3/3
ref $value and not ref($value) =~ /Boolean/
!ll&&!rl&&r
4454
MC/DC100%2/2
ref $value and not ref($value) =~ /Boolean/
ref $value not ref($value) =~ /Boolean/
Truth table100%3/3
A and not(B)
A= ref $valueB= not ref($value) =~ /Boolean/
ABresult
0X0
100
111
29  
30 49 ▶ my $string = ref($value) ? ( $value ? 'true' : 'false' ) : $value;
Branch: 2 branches
BranchTrueFalseTotal
ref $value ? : 5 44 49
$value ? : 5 0 5
31 49 for my $i ( 0 .. $#allowed_strings ) {
32 162 ▶ if ( $string eq $allowed_strings[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq $allowed_strings[$i]) 30 132 162
33 30 $$output_ref = $allowed_values->[$i];
34 30 return;
35 }
36 }
37  
38 # As in 2.0, where a boolean is "1" or "0" as a Perl string: "1" passes [true],
39 # and true passes ["1"] and [1]
40 19 ▶ if ( $has_boolean || ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_boolean or ref $value) 8 11 19
Condition100%3/3
$has_boolean or ref $value
l!l&&r!l&&!r
5311
MC/DC100%2/2
$has_boolean or ref $value
$has_boolean ref $value
Truth table100%3/3
A or B
A= $has_booleanB= ref $value
ABresult
1X1
011
000
41 8 for my $i ( 0 .. $#perl_strings ) {
42 11 ▶ if ( $value eq $perl_strings[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $perl_strings[$i]) 4 7 11
43 4 $$output_ref = $allowed_values->[$i];
44 4 return;
45 }
46 }
47 }
48  
49 15 return 'NOT_ALLOWED_VALUE';
50 }
51 65 }
52  
53  
54 43 ▶sub max_length {
Subroutine max_length: called CC=1 SCAR=0.0
Pod undocumented
55 43 my $max_length = shift;
56  
57 28 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
58 28 my $value = shift;
59 28 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 4 24 28
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1324
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
60 24 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 20 24
61  
62 20 ▶ return 'TOO_LONG' if length($value) > $max_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max_length 6 14 20
63 14 return;
64 43 };
65 }
66  
67  
68 23 ▶sub min_length {
Subroutine min_length: called CC=1 SCAR=0.0
Pod undocumented
69 23 my $min_length = shift;
70  
71 23 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
72 23 my $value = shift;
73 23 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 3 20 23
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1220
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
74 20 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 16 20
75  
76 16 ▶ return 'TOO_SHORT' if length($value) < $min_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $min_length 9 7 16
77 7 return;
78 23 };
79 }
80  
81  
82 18 ▶sub length_equal {
Subroutine length_equal: called CC=1 SCAR=0.0
Pod undocumented
83 18 my $length = shift;
84  
85 17 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
86 17 my $value = shift;
87 17 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 3 14 17
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1214
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
88 14 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 10 14
89  
90 10 ▶ return 'TOO_SHORT' if length($value) < $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $length 2 8 10
91 8 ▶ return 'TOO_LONG' if length($value) > $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $length 2 6 8
92 6 return;
93 18 };
94 }
95  
96  
97 15 ▶sub length_between {
Subroutine length_between: called CC=1 SCAR=0.0
Pod undocumented
98 15 my ($min_length, $max_length) = @_;
99  
100 15 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
101 15 my $value = shift;
102 15 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 3 12 15
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1212
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
103 12 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 8 12
104  
105 8 ▶ return 'TOO_SHORT' if length($value) < $min_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $min_length 1 7 8
106 7 ▶ return 'TOO_LONG' if length($value) > $max_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max_length 2 5 7
107 5 return;
108 15 };
109 }
110  
111  
112 55 ▶sub like {
Subroutine like: called CC=3 SCAR=11.0
Pod undocumented
113 55 my ($re, $flags) = @_;
114  
115 55 ▶ my $is_ignore_case = @_ >= 3 && index( $flags, 'i') >= 0;
Condition66%2/3
@_ >= 3 && index($flags, 'i') != -1
!ll&&!rl&&r
5005
MC/DC50%1/2
@_ >= 3 && index($flags, 'i') != -1
@_ >= 3 index($flags, 'i') != -1
Truth table66%2/3
A && B
A= @_ >= 3B= index($flags, 'i') != -1
ABresult
0X0
100
111
116 55 $re = _dollar_end_only($re);
117 55 ▶ $re = $is_ignore_case ? qr/$re/i : qr/$re/;
Branch: 1 branch
BranchTrueFalseTotal
$is_ignore_case ? : 5 50 55
118  
119 51 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
120 51 my $value = shift;
121 51 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 5 46 51
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1446
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
122 46 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 42 46
123  
124 42 ▶ return 'WRONG_FORMAT' unless $value =~ m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$re/ 20 22 42
125 22 return;
126 55 };
127 }
128  
129  
130 # In LIVR "$" at the end of a pattern matches only at the end of the value, the
131 # way RFC 9485 (I-Regexp) maps a pattern to PCRE ("\z"). In Perl it also matches
132 # before a trailing newline, so "12345\n" would match "^[0-9]{5}$". Replaces "$"
133 # outside character classes with "\z".
134 55 ▶sub _dollar_end_only {
Subroutine _dollar_end_only: called CC=2 SCAR=6.9
135 55 my $re = shift;
136  
137 55 $re =~ s{
138 ( \\. # an escaped character
139 | \[ \^? \]? (?: \[:[^\]]*:\] | \\. | [^\]] )* \] # a character class
140 )
141 | \$
142 127 ▶ }{ defined $1 ? $1 : '\z' }gesx;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 82 45 127
143  
144 55 return $re;
145 }
146  
147  
148 95 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod undocumented
149 68 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
150 68 my $value = shift;
151  
152 68 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 6 62 68
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
4262
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
153 62 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 5 57 62
154 57 return;
155 95 };
156 }
157  
158  
159 135 ▶sub equal {
Subroutine equal: called CC=1 SCAR=0.0
Pod undocumented
160 135 my $allowed_value = shift;
161 135 my $allowed_string = _boolean_string($allowed_value);
162 135 my $is_boolean = _is_boolean($allowed_value);
163  
164 111 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
165 111 my ($value, undef, $output_ref) = @_;
166 111 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 13 98 111
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
31098
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
167  
168 # LIVR 2.5: a boolean is compared as a string, so ["boolean", { eq => JSON::PP::true }]
169 # accepts true and "true"
170 98 my $string = $value;
171 98 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 13 85 98
172 13 ▶ return 'FORMAT_ERROR' if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 4 9 13
173 9 ▶ $string = $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 9 0 9
174 }
175  
176 # The output is the value of the rule, as in one_of: { eq => 2 } outputs 2 for "2".
177 # As in 2.0, where true is "1" as a Perl string, "1" passes { eq => JSON::PP::true }
178 94 ▶ if ( $string eq $allowed_string || $is_boolean && !ref($value) && $value eq $allowed_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq $allowed_string or $is_boolean and not ref $value and $value eq $allowed_value) 48 46 94
Condition88%8/9
$string eq $allowed_string or $is_boolean and not ref $value and $value eq $allowed_value
l!l&&r!l&&!r
42646
$is_boolean and not ref $value and $value eq $allowed_value
!ll&&!rl&&r
4336
$is_boolean and not ref $value
!ll&&!rl&&r
4309
MC/DC75%3/4
$string eq $allowed_string or $is_boolean and not ref $value and $value eq $allowed_value
$string eq $allowed_string $is_boolean not ref $value $value eq $allowed_value
Truth table80%4/5
A or B and not(C) and D
A= $string eq $allowed_stringB= $is_booleanC= not ref $valueD= $value eq $allowed_value
ABCDresult
1XXX1
01111
00XX0
010X0
01100
179 48 $$output_ref = $allowed_value;
180 48 return;
181 }
182  
183 46 return 'NOT_ALLOWED_VALUE';
184 135 };
185 }
186  
187 # A boolean of a JSON decoder as "true" or "false", any other value as it is
188 356 ▶sub _boolean_string {
Subroutine _boolean_string: called CC=3 SCAR=11.0
189 356 my $value = shift;
190 356 ▶ return _is_boolean($value) ? ( $value ? 'true' : 'false' ) : $value;
Branch: 2 branches
BranchTrueFalseTotal
_is_boolean($value) ? : 38 318 356
$value ? : 25 13 38
191 }
192  
193 712 ▶sub _is_boolean { ref( $_[0] ) && ref( $_[0] ) =~ /Boolean/ }
Subroutine _is_boolean: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and ref($_[0]) =~ /Boolean/ 76 636 712
194  
195 10 1;
196  
197 =head1 NAME
198  
199 Validator::LIVR::Rules::String - String rules of Validator::LIVR: string, eq, one_of, min_length, max_length, length_equal, length_between and like
200  
201 =head1 DESCRIPTION
202  
203 This module has the code of the string rules, which are documented in
204 L<Validator::LIVR/String rules>.
205  
206 Each sub is the builder of the rule with the same name. The builder of C<eq>
207 is C<equal>. The code is a good example for L<writing your own
208 rules|Validator::LIVR/WRITING RULES IN PERL>.
209  
210 =cut
« LIVR/Rules/Conditional.pm ↑ Summary LIVR/Rules/Numeric.pm »