| 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.2'; | |||||||||||||||||||||||||||||||||||||||||
| 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 | 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
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 48 | ▶ | return 'FORMAT_ERROR' if ref($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 44 | ▶ | return 'WRONG_URL' unless $value =~ $URL_RE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 43 | ▶ | if ( $value =~ $ISO_DATE_RE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 29 | my ( $year, $month, $day ) = @+{qw/year month day/}; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||