File Coverage

blib/lib/MooX/Value/ValidationUtils.pm
Criterion Covered Total %
statement 43 43 100.0
branch 28 28 100.0
condition 15 15 100.0
subroutine 10 10 100.0
pod 8 8 100.0
total 104 104 100.0


line stmt bran cond sub pod time code
1             package MooX::Value::ValidationUtils;
2              
3 9     9   64654 use warnings;
  9         16  
  9         259  
4 9     9   32 use strict;
  9         12  
  9         5992  
5              
6             our $VERSION = '0.03';
7              
8             # RFC 1123 and 2181
9             sub why_invalid_domain_name
10             {
11 93     93 1 122 my ($poss_domain) = @_;
12              
13 93 100       198 return ( 'No domain supplied', '', undef ) unless defined $poss_domain;
14 90 100 100     419 return ( 'Domain must be between 1 and 255 octets in length.', '', undef )
15             if !length $poss_domain or length $poss_domain > 255;
16 85         346 my @labels = split( /\./, $poss_domain );
17 85 100       213 return ( __PACKAGE__ . ': At least one label is required', '', undef ) unless @labels;
18              
19             # Final label can be empty
20 83 100       179 my $last = length $labels[0] ? $#labels : $#labels-1;
21 83         205 foreach my $label ( @labels[0 .. $last] )
22             {
23 394         510 my ($why, $long, $data) = why_invalid_domain_label( $label );
24 394 100       744 return ($why, $long, $label) if defined $why;
25             }
26 69         174 return;
27             }
28              
29             sub is_valid_domain_name
30             {
31 34     34 1 11203 my ($poss_domain) = @_;
32 34         64 my ($why) = why_invalid_domain_name( $poss_domain );
33 34         156 return !defined $why;
34             }
35              
36             # RFC 1123 and 2181
37             sub why_invalid_domain_label
38             {
39 435     435 1 1724 my ($poss_label) = @_;
40 435 100       636 return ( 'No domain label supplied', '', undef ) unless defined $poss_label;
41 432 100 100     1367 return ( 'Label is not in the length range 1 to 63', '', undef )
42             if !length $poss_label or length $poss_label > 63;
43 419 100       1204 return ( 'Label is not the correct form.', '', undef )
44             unless $poss_label =~ m{\A[a-zA-Z0-9] # No hyphens at front
45             (?:[-a-zA-Z0-9]* # hyphens allowed in the middle
46             [a-zA-Z0-9])? # No hyphens at the end
47             \z}x;
48 406         465 return;
49             }
50              
51             sub is_valid_domain_label
52             {
53 16     16 1 3872 my ($poss_label) = @_;
54 16         30 my ($why) = why_invalid_domain_label( $poss_label );
55 16         61 return !defined $why;
56             }
57              
58              
59             # RFC 5322
60             sub why_invalid_email_local_part
61             {
62 220     220 1 269 my ($poss_part) = @_;
63 220 100       436 return ( 'No email local part supplied', '', undef ) unless defined $poss_part;
64 218 100 100     886 return ( 'Local part is not in the length range 1 to 64', '', undef )
65             if !length $poss_part or length $poss_part > 64;
66 214 100 100     1293 return ( 'Local part is not correct form.', '', undef )
67             unless $poss_part =~ m/\A"(?:\\.|[!#-[\]-~])+"\z/ # quoted string (all 7-bit ASCII except \ and " unless quoted)
68             || $poss_part =~ m{\A[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+ # any 'atext' characters
69             (?:\. # separated by dots
70             [a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+ # any 'atext' characters
71             )*
72             \z}x;
73 182         292 return;
74             }
75              
76             sub is_valid_email_local_part
77             {
78 107     107 1 31708 my ($poss_part) = @_;
79 107         160 my ($why) = why_invalid_email_local_part( $poss_part );
80 107         431 return !defined $why;
81             }
82              
83              
84              
85             # RFC 5322
86             sub why_invalid_common_email_local_part
87             {
88 198     198 1 265 my ($poss_part) = @_;
89 198 100       399 return ( 'No email local part supplied', '', undef ) unless defined $poss_part;
90 196 100 100     820 return ( 'Local part is not in the length range 1 to 64', '', undef )
91             if !length $poss_part or length $poss_part > 64;
92 192 100       884 return ( 'Local part is not correct form.', '', undef )
93             unless $poss_part =~ m{\A[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+ # any 'atext' characters
94             (?:\. # separated by dots
95             [a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+ # any 'atext' characters
96             )*
97             \z}x;
98 156         231 return;
99             }
100              
101             sub is_valid_common_email_local_part
102             {
103 96     96 1 34371 my ($poss_part) = @_;
104 96         156 my ($why) = why_invalid_common_email_local_part( $poss_part );
105 96         455 return !defined $why;
106             }
107              
108             1;
109             __END__