LIVR/Rules/Special.pm

filter: Statementstmt 100% 50 / 50 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.6% 148 / 158 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.21';
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 122 ▶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 122 };
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 10 my $URL_LABEL = qr/[a-z0-9\x{a1}-\x{10ffff}]+ (?: -+[a-z0-9\x{a1}-\x{10ffff}]+ )*/xi;
62  
63 # The last label starts with a letter, so a host name is never an IP address
64 10 my $URL_LAST_LABEL = qr/[a-z\x{a1}-\x{10ffff}] [a-z0-9\x{a1}-\x{10ffff}]* (?: -+[a-z0-9\x{a1}-\x{10ffff}]+ )*/xi;
65  
66 # An http or https URL with a host name or an IPv4 address, and an optional user, port,
67 # path, query and fragment. As in Validator::LIVR 2.0, and unlike the other LIVR
68 # implementations, a host name can be without a TLD, such as "intranet", any IPv4
69 # address and a port of up to 5 digits are allowed, and the length is not limited
70 10 my $URL_RE = qr/\A
71 https?:\/\/
72 (?: \S+\@ )?
73 (?:
74 $IPV4_OCTET (?: \.$IPV4_OCTET ){3}
75 | (?: $URL_LABEL \. )* $URL_LAST_LABEL
76 ) \.?
77 (?: :[0-9]{0,5} )?
78 (?: [\/?\#] \S* )?
79 \z/xi;
80  
81 15 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod undocumented
82 50 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
83 50 my $value = shift;
84 50 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 2 48 50
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
0248
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
85 48 ▶ return 'FORMAT_ERROR' if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 44 48
86  
87 44 ▶ return 'WRONG_URL' unless $value =~ $URL_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$URL_RE/xi 17 27 44
88 27 return;
89 15 };
90 }
91  
92  
93 # \z, not $: "$" also matches before a trailing newline
94 10 my $ISO_DATE_RE = qr#^
95 (?<year>[0-9]{4})-
96 (?<month>[0-1][0-9])-
97 (?<day>[0-3][0-9])
98 \z#x;
99  
100 157 ▶sub iso_date {
Subroutine iso_date: called CC=4 SCAR=13.9
Pod undocumented
101 # LIVR 2.5: the first argument, if any, is an object with the bounds "min" and "max".
102 # LIVR 2.0 ignored any argument. { iso_date => undef } has no arguments
103 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
104 104 require Validator::LIVR::Rules::DateTime;
105 104 goto &Validator::LIVR::Rules::DateTime::iso_date;
106 }
107  
108 54 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
109 54 my $value = shift;
110 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
111  
112 # LIVR 2.5: a boolean is a primitive value, not a date
113 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
114  
115 43 ▶ if ( $value =~ $ISO_DATE_RE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$ISO_DATE_RE/x) 29 14 43
116 29 my ( $year, $month, $day ) = @+{qw/year month day/};
117 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
118  
119 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
120 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
121  
122 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
123 }
124  
125 23 return "WRONG_DATE";
126 53 };
127 }
128  
129 10 1;
130  
131 =head1 NAME
132  
133 Validator::LIVR::Rules::Special - Special rules of Validator::LIVR: email, url, iso_date and equal_to_field
134  
135 =head1 DESCRIPTION
136  
137 This module has the code of the special rules, which are documented in
138 L<Validator::LIVR/Special rules>.
139  
140 Each sub is the builder of the rule with the same name. The code is a good
141 example for L<writing your own rules|Validator::LIVR/WRITING RULES IN PERL>.
142  
143 =cut
« LIVR/Rules/Collection.pm ↑ Summary LIVR/Rules/Types.pm »