LIVR/Rules/Special.pm

filter: Statementstmt 100% 52 / 52 Branchbran 100% 36 / 36 Conditioncond 91.6% 33 / 36 MC/DCmcdc 86.3% 19 / 22 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 93.7% 150 / 160 CC 31 SCAR 20.2
CC
31
Cov
98%
__ANON__
13.0
__ANON__
7.0
__ANON__
6.0
CRAP 7.5
« LIVR/Rules/Collection.pm ↑ Summary LIVR/Rules/Types.pm »
1 package Validator::LIVR::Rules::Special;
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 # From 0 to 255. Leading zeros are allowed, as Validator::LIVR 2.0 allowed them in URLs
9 10 my $IPV4_OCTET = qr/25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9]/;
10  
11 # Letters, digits and hyphens, but not a hyphen at the start or the end. Up to 63 characters
12 10 my $EMAIL_LABEL = qr/[a-zA-Z0-9] (?: [a-zA-Z0-9-]{0,61} [a-zA-Z0-9] )?/x;
13  
14 # The TLD starts with a letter, as the last label of a host name in "url"
15 10 my $EMAIL_TLD = qr/[a-zA-Z] (?: [a-zA-Z0-9-]{0,61} [a-zA-Z0-9] )?/x;
16  
17 # As in the other LIVR implementations, the local part is dot-separated atoms or a quoted
18 # string, and the domain is a host name or an IP address in brackets.
19 # \z, not $: "$" also matches before a trailing newline
20 10 my $EMAIL_RE = qr/\A
21 (?:
22 [^<>()\[\]\\.,;:\s\@"\x00-\x1f\x7f]+ (?: \.[^<>()\[\]\\.,;:\s\@"\x00-\x1f\x7f]+ )*
23 | " (?: [^"\\\@\x00-\x1f\x7f\x{2028}\x{2029}] | \\[^\x00-\x1f\x7f\x{2028}\x{2029}] )+ "
24 )
25 \@
26 (?:
27 \[ $IPV4_OCTET (?: \.$IPV4_OCTET ){3} \]
28 | (?: $EMAIL_LABEL \. )+ $EMAIL_TLD
29 )
30 \z/x;
31  
32 119 ▶sub email {
Subroutine email: called CC=1 SCAR=0.0
Pod undocumented
33 130 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
34 130 my $value = shift;
35 130 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 13 117 130
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
94117
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
36 117 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 113 117
37  
38 # The local part is up to 64 characters, the address up to 254. "@" is not in the local part
39 113 ▶ return 'WRONG_EMAIL' if length($value) > 254 || index($value, '@') > 64;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > 254 or index($value, "\@") > 64 2 111 113
Condition100%3/3
length $value > 254 or index($value, "\@") > 64
l!l&&r!l&&!r
11111
MC/DC100%2/2
length $value > 254 or index($value, "\@") > 64
length $value > 254 index($value, "\@") > 64
Truth table100%3/3
A or B
A= length $value > 254B= index($value, "\@") > 64
ABresult
1X1
011
000
40 111 ▶ return 'WRONG_EMAIL' unless $value =~ $EMAIL_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$EMAIL_RE/x 55 56 111
41 56 return;
42 119 };
43 }
44  
45  
46 36 ▶sub equal_to_field {
Subroutine equal_to_field: called CC=1 SCAR=0.0
Pod undocumented
47 36 my $field = shift;
48  
49 39 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
50 39 my ( $value, $params ) = @_;
51 39 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 4 35 39
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
0435
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
52 35 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 31 35
53  
54 31 ▶ return 'FIELDS_NOT_EQUAL' unless defined( $params->{$field} ) && $value eq $params->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$field} and $value eq $params->{$field} 17 14 31
Condition100%3/3
defined $params->{$field} and $value eq $params->{$field}
!ll&&!rl&&r
51214
MC/DC100%2/2
defined $params->{$field} and $value eq $params->{$field}
defined $params->{$field} $value eq $params->{$field}
Truth table100%3/3
A and B
A= defined $params->{$field}B= $value eq $params->{$field}
ABresult
0X0
100
111
55 14 return;
56 36 };
57 }
58  
59  
60 # Letters of any language, digits and hyphens, but not a hyphen at the start or the end.
61 # Up to 63 characters. "." is not in a label, so labels do not overlap
62 10 my $URL_LABEL = qr/[a-z0-9\x{a1}-\x{10ffff}] (?: [a-z0-9\x{a1}-\x{10ffff}-]{0,61} [a-z0-9\x{a1}-\x{10ffff}] )?/xi;
63  
64 # The last label starts with a letter, so a host name is never an IP address
65 10 my $URL_LAST_LABEL = qr/[a-z\x{a1}-\x{10ffff}] (?: [a-z0-9\x{a1}-\x{10ffff}-]{0,61} [a-z0-9\x{a1}-\x{10ffff}] )?/xi;
66  
67 10 my $URL_HOST = qr/ $IPV4_OCTET (?: \.$IPV4_OCTET ){3} | (?: $URL_LABEL \. )* $URL_LAST_LABEL /x;
68  
69 # From 0 to 65535, with 1-5 digits
70 10 my $URL_PORT = qr/ [0-9]{1,4} | [0-5][0-9]{4} | 6[0-4][0-9]{3} | 65[0-4][0-9]{2} | 655[0-2][0-9] | 6553[0-5] /x;
71  
72 # An http or https URL with a host name or an IPv4 address, and an optional user, port,
73 # path, query and fragment. As in Validator::LIVR 2.0, and unlike the other LIVR
74 # implementations, a host name can be without a TLD, such as "intranet", any IPv4
75 # address is allowed, and the length is not limited. As in LIVR 2.5, there is no dot
76 # after the host, a label has up to 63 characters and a port is at most 65535.
77 #
78 # It is "https?:// (?: \S+\@ )? $URL_HOST (?: :$URL_PORT )? (?: [\/?\#] \S* )? \z" in two
79 # cases. The user can end at any "@" of the value, and in each case the path is matched
80 # once, not again for every "@":
81 # - Without spaces: the path, the query and the fragment are anything after a "/", "?"
82 # or "#" that follows the host
83 # - With spaces, which can only be in the host (labels have the characters from U+00A1
84 # on, and some of them are spaces, such as U+3000): the user ends at the last "@"
85 # before the first space
86 10 my $URL_RE = qr/\A
87 https?:\/\/
88 (?:
89 (?= \S* \z )
90 (?: \S+\@ )? $URL_HOST (?: :$URL_PORT )? (?: [\/?\#] | \z )
91 |
92 (?: \S+\@ (?= [^\s\@\/?\#]* \s ) )? $URL_HOST (?: :$URL_PORT )? (?: [\/?\#] \S* )? \z
93 )
94 /xi;
95  
96 40 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod undocumented
97 116 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
98 116 my $value = shift;
99 116 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 4 112 116
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
04112
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
100 112 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 108 112
101  
102 108 ▶ return 'WRONG_URL' unless $value =~ $URL_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$URL_RE/xi 59 49 108
103 49 return;
104 40 };
105 }
106  
107  
108 # \z, not $: "$" also matches before a trailing newline
109 10 my $ISO_DATE_RE = qr#^
110 (?<year>[0-9]{4})-
111 (?<month>[0-1][0-9])-
112 (?<day>[0-3][0-9])
113 \z#x;
114  
115 157 ▶sub iso_date {
Subroutine iso_date: called CC=4 SCAR=13.9
Pod undocumented
116 # LIVR 2.5: the first argument, if any, is an object with the bounds "min" and "max".
117 # LIVR 2.0 ignored any argument. { iso_date => undef } has no arguments
118 157 ▶ if ( @_ > 2 || @_ == 2 && defined( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2 or @_ == 2 and defined $_[0]) 104 53 157
Condition100%6/6
@_ > 2 or @_ == 2 and defined $_[0]
l!l&&r!l&&!r
210253
@_ == 2 and defined $_[0]
!ll&&!rl&&r
512102
MC/DC100%3/3
@_ > 2 or @_ == 2 and defined $_[0]
@_ > 2 @_ == 2 defined $_[0]
Truth table100%4/4
A or B and C
A= @_ > 2B= @_ == 2C= defined $_[0]
ABCresult
1XX1
0111
00X0
0100
119 104 require Validator::LIVR::Rules::DateTime;
120 104 goto &Validator::LIVR::Rules::DateTime::iso_date;
121 }
122  
123 54 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
124 54 my $value = shift;
125 54 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 4 50 54
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
0450
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
126  
127 # LIVR 2.5: a boolean is a primitive value, not a date
128 50 ▶ return ref($value) =~ /Boolean/ ? 'WRONG_DATE' : 'FORMAT_ERROR' if ref($value);
Branch: 2 branches
BranchTrueFalseTotal
if ref $value 7 43 50
ref($value) =~ /Boolean/ ? : 2 5 7
129  
130 43 ▶ if ( $value =~ $ISO_DATE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$ISO_DATE_RE/x) 29 14 43
131 29 my ( $year, $month, $day ) = @+{qw/year month day/};
132 29 ▶ return "WRONG_DATE" if $month < 1 || $month > 12;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 3 26 29
Condition100%3/3
$month < 1 or $month > 12
l!l&&r!l&&!r
1226
MC/DC100%2/2
$month < 1 or $month > 12
$month < 1 $month > 12
Truth table100%3/3
A or B
A= $month < 1B= $month > 12
ABresult
1X1
011
000
133  
134 26 ▶ my $is_leap_year = ( $year % 4 == 0 && $year % 100 != 0 ) || $year % 400 == 0;
Condition100%6/6
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
l!l&&r!l&&!r
5318
$year % 4 == 0 and $year % 100 != 0
!ll&&!rl&&r
1565
MC/DC100%3/3
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
$year % 4 == 0 $year % 100 != 0 $year % 400 == 0
Truth table80%4/5
A and B || C
A= $year % 4 == 0B= $year % 100 != 0C= $year % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
135 26 ▶ my $max_day = ( 31, $is_leap_year ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 )[ $month - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
$is_leap_year ? : 8 18 26
136  
137 26 ▶ return if $day >= 1 && $day <= $max_day;
Branch: 1 branch
BranchTrueFalseTotal
if $day >= 1 and $day <= $max_day 17 9 26
Condition100%3/3
$day >= 1 and $day <= $max_day
!ll&&!rl&&r
2717
MC/DC100%2/2
$day >= 1 and $day <= $max_day
$day >= 1 $day <= $max_day
Truth table100%3/3
A and B
A= $day >= 1B= $day <= $max_day
ABresult
0X0
100
111
138 }
139  
140 23 return "WRONG_DATE";
141 53 };
142 }
143  
144 10 1;
145  
146 =head1 NAME
147  
148 Validator::LIVR::Rules::Special - Special rules of Validator::LIVR: email, url, iso_date and equal_to_field
149  
150 =head1 DESCRIPTION
151  
152 This module has the code of the special rules, which are documented in
153 L<Validator::LIVR/Special rules>.
154  
155 Each sub is the builder of the rule with the same name. The code is a good
156 example for L<writing your own rules|Validator::LIVR/WRITING RULES IN PERL>.
157  
158 =cut
« LIVR/Rules/Collection.pm ↑ Summary LIVR/Rules/Types.pm »