| 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
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 117 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
length $value > 254 or index($value, "\@") > 64
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
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 111 | ▶ | return 'WRONG_EMAIL' unless $value =~ $EMAIL_RE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 35 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 31 | ▶ | return 'FIELDS_NOT_EQUAL' unless defined( $params->{$field} ) && $value eq $params->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $params->{$field} and $value eq $params->{$field}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 112 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 108 | ▶ | return 'WRONG_URL' unless $value =~ $URL_RE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
@_ > 2 or @_ == 2 and defined $_[0]
@_ == 2 and defined $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 43 | ▶ | if ( $value =~ $ISO_DATE_RE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 29 | my ( $year, $month, $day ) = @+{qw/year month day/}; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 29 | ▶ | return "WRONG_DATE" if $month < 1 || $month > 12; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$month < 1 or $month > 12
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$year % 4 == 0 and $year % 100 != 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 26 | ▶ | return if $day >= 1 && $day <= $max_day; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$day >= 1 and $day <= $max_day
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||