| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 53 | ▶ | return 'FORMAT_ERROR' if ref($value) && ref($value) !~ 'Boolean'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value and not ref($value) =~ /Boolean/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 49 | ▶ | my $string = ref($value) ? ( $value ? 'true' : 'false' ) : $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 49 | for my $i ( 0 .. $#allowed_strings ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 162 | ▶ | if ( $string eq $allowed_strings[$i] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$has_boolean or ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 8 | for my $i ( 0 .. $#perl_strings ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 11 | ▶ | if ( $value eq $perl_strings[$i] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 24 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 20 | ▶ | return 'TOO_LONG' if length($value) > $max_length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 20 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 16 | ▶ | return 'TOO_SHORT' if length($value) < $min_length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 14 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | ▶ | return 'TOO_SHORT' if length($value) < $length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 8 | ▶ | return 'TOO_LONG' if length($value) > $length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 12 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | ▶ | return 'TOO_SHORT' if length($value) < $min_length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | return 'TOO_LONG' if length($value) > $max_length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 55 | $re = _dollar_end_only($re); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 55 | ▶ | $re = $is_ignore_case ? qr/$re/i : qr/$re/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 46 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 42 | ▶ | return 'WRONG_FORMAT' unless $value =~ m/$re/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 62 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 13 | ▶ | return 'FORMAT_ERROR' if ref($value) !~ /Boolean/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 9 | ▶ | $string = $value ? 'true' : 'false'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition88%8/9
$string eq $allowed_string or $is_boolean and not ref $value and $value eq $allowed_value
$is_boolean and not ref $value and $value eq $allowed_value
$is_boolean and not ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 712 | ▶ | sub _is_boolean { ref( $_[0] ) && ref( $_[0] ) =~ /Boolean/ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_boolean: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||