Validator/Formats.pm

filter: Statementstmt 98.7% 157 / 159 Branchbran 89.7% 70 / 78 Conditioncond 77.7% 35 / 45 MC/DCmcdc 59.2% 16 / 27 Subroutinesub 96.9% 32 / 33 Podpod 100% 24 / 24 totaltotal 91.2% 334 / 366 CC 57 SCAR 17.1
CC
57
Cov
93%
check_iri_reference
15.3
check_date_time
7.0
_match_number
6.2
CRAP 5.5
« Validator/Error.pm ↑ Summary Validator/Joi.pm »
1 package JSON::Validator::Formats;
2 82 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 82 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 82 require Time::Local;
7  
8 82 ▶use constant DATA_VALIDATE_DOMAIN => eval 'require Data::Validate::Domain;1';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 82 ▶use constant DATA_VALIDATE_IP => eval 'require Data::Validate::IP;1';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 82 ▶use constant IV_SIZE => eval 'require Config;$Config::Config{ivsize}';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 82 ▶use constant NET_IDN_ENCODE => eval 'require Net::IDN::Encode;1';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 82 ▶use constant WARN_MISSING_MODULE => $ENV{JSON_VALIDATOR_WARN} // 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"JSON_VALIDATOR_WARN"} // 1
l!l
082
MC/DC0%0/1
$ENV{"JSON_VALIDATOR_WARN"} // 1
$ENV{"JSON_VALIDATOR_WARN"}
Truth table50%1/2
A
A= $ENV{"JSON_VALIDATOR_WARN"}
Aresult
11
00
13  
14 82 our $IRI_TEST_NAME = 'iri-reference';
15  
16 2 ▶sub check_byte {
Subroutine check_byte: called CC=2 SCAR=6.9
Pod documented
17 2 ▶ $_[0] =~ /^[A-Za-z0-9\+\/\=]+$/ ? undef : 'Does not match byte format.';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ m[^[A-Za-z0-9\+/\=]+$]u ? : 1 1 2
18 }
19  
20 8 ▶sub check_date {
Subroutine check_date: called CC=4 SCAR=13.9
Pod documented
21 8 my @date = $_[0] =~ m!^(\d{4})-(\d\d)-(\d\d)$!io;
22 8 ▶ return 'Does not match date format.' unless @date;
Branch: 1 branch
BranchTrueFalseTotal
unless @date 5 3 8
23 3 ▶ @date = map { s/^0+//; $_ || 0 } reverse @date;
Condition100%2/2
$_ || 0
l!l
45
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
24 3 $date[1] -= 1; # month are zero based
25 3 local $@;
26 3 ▶ return undef if eval { Time::Local::timegm(0, 0, 0, @date); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { Time::Local::timegm(0, 0, 0, @date); 1 } } 1 2 3
27 2 my $err = (split / at /, $@)[0];
28 2 $err =~ s!('-?\d+'\s|\s[\d\.]+)!!g;
29 2 $err .= '.';
30 2 return $err;
31 }
32  
33 28 ▶sub check_date_time {
Subroutine check_date_time: called CC=7 SCAR=19.5
Pod documented
34 28 my @dt = $_[0] =~ m!^(\d{4})-(\d\d)-(\d\d)[T ](\d\d):(\d\d):(\d\d(?:\.\d+)?)(?:Z|([+-])(\d\d):(\d\d))?$!io;
35 28 ▶ return 'Does not match date-time format.' unless @dt;
Branch: 1 branch
BranchTrueFalseTotal
unless @dt 10 18 28
36 18 ▶ return 'Time offset hour out of range.' if defined $dt[7] and $dt[7] > 23;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dt[7] and $dt[7] > 23 1 17 18
Condition100%3/3
defined $dt[7] and $dt[7] > 23
!ll&&!rl&&r
1251
MC/DC100%2/2
defined $dt[7] and $dt[7] > 23
defined $dt[7] $dt[7] > 23
Truth table100%3/3
A and B
A= defined $dt[7]B= $dt[7] > 23
ABresult
0X0
100
111
37 17 ▶ return 'Time offset minute out of range.' if defined $dt[8] and $dt[8] > 59;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dt[8] and $dt[8] > 59 1 16 17
Condition100%3/3
defined $dt[8] and $dt[8] > 59
!ll&&!rl&&r
1241
MC/DC100%2/2
defined $dt[8] and $dt[8] > 59
defined $dt[8] $dt[8] > 59
Truth table100%3/3
A and B
A= defined $dt[8]B= $dt[8] > 59
ABresult
0X0
100
111
38 16 @dt = map { s/^0//; $_ } reverse @dt[0 .. 5];
39 16 $dt[4] -= 1; # month are zero based
40 16 local $@;
41 16 ▶ return undef if eval { Time::Local::timegm(@dt); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { Time::Local::timegm(@dt); 1 } } 7 9 16
42 9 my $err = (split / at /, $@)[0];
43 9 $err =~ s!('-?\d+'\s|\s[\d\.]+)!!g;
44 9 $err .= '.';
45 9 return $err;
46 }
47  
48 1 ▶sub check_double { _match_number(double => $_[0], '') }
Subroutine check_double: called CC=1 SCAR=0.0
Pod documented
49  
50 9 ▶sub check_duration {
Subroutine check_duration: called CC=2 SCAR=6.9
Pod documented
51 9 state $rfc3339_duration_re = do {
52 1 my $num = qr{\d+(?:[,.]\d+)?};
53 1 my $sec = qr/${num}S/;
54 1 my $min = qr/${num}M(?:$sec)?/;
55 1 my $hour = qr/${num}H(?:$min)?/;
56 1 my $day = qr/${num}D(?:$hour)?/;
57 1 my $mon = qr/${num}M(?:$day)?/;
58 1 my $year = qr/${num}Y(?:$mon)?/;
59 1 my $week = qr/${num}W/;
60 1 my $time = qr/T($hour|$min|$sec)/;
61 1 my $date = qr/(?:$day|$mon|$year)(?:$time)?/;
62 1 qr{^P(?:$date|$time|$week)$};
63 };
64  
65 9 ▶ return $_[0] =~ $rfc3339_duration_re ? undef : 'Does not match duration format.';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$rfc3339_duration_re/u ? : 8 1 9
66 }
67  
68 29 ▶sub check_email {
Subroutine check_email: called CC=2 SCAR=6.9
Pod documented
69 29 state $email_rfc5322_re = do {
70 2 my $atom = qr;[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+;o;
71 2 my $quoted_string = qr/"(?:\\[^\r\n]|[^\\"])*"/o;
72 2 my $domain_literal = qr/\[(?:\\[\x01-\x09\x0B-\x0c\x0e-\x7f]|[\x21-\x5a\x5e-\x7e])*\]/o;
73 2 my $dot_atom = qr/$atom(?:[.]$atom)*/o;
74 2 my $local_part = qr/(?:$dot_atom|$quoted_string)/o;
75 2 my $domain = qr/(?:$dot_atom|$domain_literal)/o;
76  
77 2 qr/^$local_part\@$domain$/o;
78 };
79  
80 29 ▶ return $_[0] =~ $email_rfc5322_re ? undef : 'Does not match email format.';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$email_rfc5322_re/u ? : 19 10 29
81 }
82  
83 2 ▶sub check_float { _match_number(float => $_[0], '') }
Subroutine check_float: called CC=1 SCAR=0.0
Pod documented
84  
85 3 ▶sub check_hostname {
Subroutine check_hostname: called CC=2 SCAR=6.9
Pod documented
86 3 return _module_missing(hostname => 'Data::Validate::Domain') unless DATA_VALIDATE_DOMAIN;
87 3 ▶ return undef if Data::Validate::Domain::is_hostname($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if Data::Validate::Domain::is_hostname($_[0]) 2 1 3
88 1 return 'Does not match hostname format.';
89 }
90  
91 29 ▶sub check_idn_email {
Subroutine check_idn_email: called CC=5 SCAR=16.2
Pod documented
92 29 return _module_missing('idn-email' => 'Net::IDN::Encode') unless NET_IDN_ENCODE;
93  
94 29 local $@;
95 29 my $err = eval {
96 29 my ($local_part, $domain) = split /@/, $_[0], 2;
97 $local_part = join '.', map {
98 29 ▶ Net::IDN::Encode::to_ascii($_)
Condition50%1/2
$local_part // ""
l!l
290
MC/DC0%0/1
$local_part // ""
$local_part
Truth table50%1/2
A
A= $local_part
Aresult
11
00
99 } split( /\./, $local_part // '');
100  
101 27 ▶ check_email(
Condition50%1/2
$domain // ""
l!l
270
MC/DC0%0/1
$domain // ""
$domain
Truth table50%1/2
A
A= $domain
Aresult
11
00
102 join '@',
103 $local_part,
104 Net::IDN::Encode::domain_to_ascii($domain // ''),
105 );
106 };
107  
108 29 ▶ return $err || $@ ? 'Does not match idn-email format.' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$err || $@ ? : 12 17 29
Condition100%3/3
$err or $@
l!l&&r!l&&!r
6617
MC/DC100%2/2
$err or $@
$err $@
Truth table100%3/3
A or B
A= $errB= $@
ABresult
1X1
011
000
109 }
110  
111 1 ▶sub check_idn_hostname {
Subroutine check_idn_hostname: called CC=3 SCAR=11.3
Pod documented
112 1 return _module_missing('idn-hostname' => 'Net::IDN::Encode') unless NET_IDN_ENCODE;
113  
114 1 local $@;
115 1 my $err = eval { check_hostname(Net::IDN::Encode::domain_to_ascii($_[0])) };
116 1 ▶ return $err ? 'Does not match idn-hostname format.' : $@ || undef;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 1 1
Condition50%1/2
$@ or undef
l!l
01
MC/DC0%0/1
$@ or undef
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
117 }
118  
119 9 ▶sub check_int32 { _match_number(int32 => $_[0], 'l') }
Subroutine check_int32: called CC=1 SCAR=0.0
Pod documented
120 13 ▶sub check_int64 { _match_number(int64 => $_[0], IV_SIZE >= 8 ? 'q' : '') }
Subroutine check_int64: called CC=1 SCAR=0.0
Pod documented
121  
122 4 ▶sub check_iri {
Subroutine check_iri: called CC=2 SCAR=6.9
Pod documented
123 4 local $IRI_TEST_NAME = 'iri';
124 4 ▶ return 'Scheme missing.' unless $_[0] =~ m!^\w+:!;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\w+:/u 1 3 4
125 3 return check_iri_reference($_[0]);
126 }
127  
128 166 ▶sub check_iri_reference {
Subroutine check_iri_reference: called CC=15 SCAR=27.3
Pod documented
129 166 ▶ return "Does not match $IRI_TEST_NAME format."
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ m[^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?]u 0 166 166
130 unless $_[0] =~ m!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!;
131  
132 166 ▶ my ($scheme, $auth_host, $path, $query, $has_fragment, $fragment) = map { $_ // '' } ($2, $4, $5, $7, $8, $9);
Condition100%2/2
$_ // ""
l!l
494502
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
133  
134 166 ▶ return 'Scheme missing.' if length $auth_host and !length $scheme;
Branch: 1 branch
BranchTrueFalseTotal
if length $auth_host and not length $scheme 1 165 166
Condition100%3/3
length $auth_host and not length $scheme
!ll&&!rl&&r
145201
MC/DC100%2/2
length $auth_host and not length $scheme
length $auth_host not length $scheme
Truth table100%3/3
A and not(B)
A= length $auth_hostB= not length $scheme
ABresult
0X0
100
111
135 165 ▶ return 'Scheme, path or fragment are required.' unless length($scheme) + length($path) + length($has_fragment);
Branch: 1 branch
BranchTrueFalseTotal
unless length($scheme) + length($path) + length($has_fragment) 1 164 165
136 164 ▶ return 'Scheme must begin with a letter.' if length $scheme and lc($scheme) !~ m!^[a-z][a-z0-9\+\-\.]*$!;
Branch: 1 branch
BranchTrueFalseTotal
if length $scheme and not lc($scheme) =~ /^[a-z][a-z0-9\+\-\.]*$/u 1 163 164
Condition100%3/3
length $scheme and not lc($scheme) =~ /^[a-z][a-z0-9\+\-\.]*$/u
!ll&&!rl&&r
140231
MC/DC100%2/2
length $scheme and not lc($scheme) =~ /^[a-z][a-z0-9\+\-\.]*$/u
length $scheme not lc($scheme) =~ /^[a-z][a-z0-9\+\-\.]*$/u
Truth table100%3/3
A and not(B)
A= length $schemeB= not lc($scheme) =~ /^[a-z][a-z0-9\+\-\.]*$/u
ABresult
0X0
100
111
137 163 ▶ return 'Invalid hex escape.' if $_[0] =~ /%[^0-9a-f]/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /%[^0-9a-f]/iu 1 162 163
138 162 ▶ return 'Hex escapes are not complete.' if $_[0] =~ /%[0-9a-f](:?[^0-9a-f]|$)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /%[0-9a-f](:?[^0-9a-f]|$)/iu 1 161 162
139  
140 161 ▶ if (defined $auth_host and length $auth_host) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $auth_host and length $auth_host) { } 17 144 161
elsif ($path =~ m[^//]u) { } 1 143 144
Condition66%2/3
defined $auth_host and length $auth_host
!ll&&!rl&&r
014417
MC/DC50%1/2
defined $auth_host and length $auth_host
defined $auth_host length $auth_host
Truth table66%2/3
A and B
A= defined $auth_hostB= length $auth_host
ABresult
0X0
100
111
141 17 ▶ return 'Path cannot be empty and must begin with a /' unless !length $path or $path =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
unless not length $path or $path =~ m[^/]u 0 17 17
Condition33%1/3
not length $path or $path =~ m[^/]u
l!l&&r!l&&!r
0170
MC/DC0%0/2
not length $path or $path =~ m[^/]u
not length $path $path =~ m[^/]u
Truth table33%1/3
not(A) or B
A= not length $pathB= $path =~ m[^/]u
ABresult
1X1
011
000
142 }
143 elsif ($path =~ m!^//!) {
144 1 return 'Path cannot not start with //.';
145 }
146  
147 160 return undef;
148 }
149  
150 5 ▶sub check_json_pointer {
Subroutine check_json_pointer: called CC=3 SCAR=11.0
Pod documented
151 5 ▶ return !length $_[0] || $_[0] =~ m!^/! ? undef : 'Does not match json-pointer format.';
Branch: 1 branch
BranchTrueFalseTotal
!length($_[0]) || $_[0] =~ m[^/]u ? : 3 2 5
Condition100%3/3
not length $_[0] or $_[0] =~ m[^/]u
l!l&&r!l&&!r
122
MC/DC100%2/2
not length $_[0] or $_[0] =~ m[^/]u
not length $_[0] $_[0] =~ m[^/]u
Truth table100%3/3
not(A) or B
A= not length $_[0]B= $_[0] =~ m[^/]u
ABresult
1X1
011
000
152 }
153  
154 2 ▶sub check_ipv4 {
Subroutine check_ipv4: called CC=5 SCAR=16.5
Pod documented
155 2 ▶ return undef if DATA_VALIDATE_IP and Data::Validate::IP::is_ipv4($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if Data::Validate::IP::is_ipv4($_[0]) 1 1 2
156 1 my (@octets) = $_[0] =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
157 1 ▶ return undef if 4 == grep { $_ >= 0 && $_ <= 255 && $_ !~ /^0\d{1,2}$/ } @octets;
Branch: 1 branch
BranchTrueFalseTotal
if 4 == grep({$_ >= 0 and $_ <= 255 and not $_ =~ /^0\d{1,2}$/u;} @octets) 0 1 1
Condition66%4/6
$_ >= 0 && $_ <= 255 && !($_ =~ /^0\d{1,2}$/u)
!ll&&!rl&&r
103
$_ >= 0 and $_ <= 255
!ll&&!rl&&r
013
MC/DC33%1/3
$_ >= 0 && $_ <= 255 && !($_ =~ /^0\d{1,2}$/u)
$_ >= 0 $_ <= 255 !($_ =~ /^0\d{1,2}$/u)
Truth table50%2/4
A and B && not(C)
A= $_ >= 0B= $_ <= 255C= !($_ =~ /^0\d{1,2}$/u)
ABCresult
0XX0
10X0
1100
1111
158 1 return 'Does not match ipv4 format.';
159 }
160  
161 2 ▶sub check_ipv6 {
Subroutine check_ipv6: called CC=2 SCAR=6.9
Pod documented
162 2 return _module_missing(ipv6 => 'Data::Validate::IP') unless DATA_VALIDATE_IP;
163 2 ▶ return undef if Data::Validate::IP::is_ipv6($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if Data::Validate::IP::is_ipv6($_[0]) 1 1 2
164 1 return 'Does not match ipv6 format.';
165 }
166  
167 5 ▶sub check_relative_json_pointer {
Subroutine check_relative_json_pointer: called CC=5 SCAR=16.1
Pod documented
168 5 ▶ return 'Relative JSON Pointer must start with a non-negative-integer.' unless $_[0] =~ m!^\d+!;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\d+/u 1 4 5
169 4 ▶ return undef if $_[0] =~ m!^(\d+)#?$!;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^(\d+)#?$/u 2 2 4
170 2 ▶ return 'Relative JSON Pointer must have "#" or a JSON Pointer.' unless $_[0] =~ m!^\d+(.+)!;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\d+(.+)/u 0 2 2
171 2 ▶ return 'Does not match relative-json-pointer format.' if check_json_pointer($1);
Branch: 1 branch
BranchTrueFalseTotal
if check_json_pointer($1) 1 1 2
172 1 return undef;
173 }
174  
175 2 ▶sub check_regex {
Subroutine check_regex: called CC=2 SCAR=6.9
Pod documented
176 2 ▶ eval {qr{$_[0]}} ? undef : 'Does not match regex format.';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { qr/$_[0]/u } } ? : 1 1 2
177 }
178  
179 8 ▶sub check_time {
Subroutine check_time: called CC=3 SCAR=11.0
Pod documented
180 8 my @time = $_[0] =~ m!^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(?:Z|([+-])(\d+):(\d+))?$!io;
181 8 ▶ return 'Does not match time format.' unless @time;
Branch: 1 branch
BranchTrueFalseTotal
unless @time 1 7 8
182 7 @time = map { s/^0//; $_ } reverse @time[0 .. 2];
183 7 local $@;
184 7 ▶ return undef if eval { Time::Local::timegm(@time, 31, 11, 1947); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { Time::Local::timegm(@time, 31, 11, 1947); 1 } } 4 3 7
185 3 my $err = (split / at /, $@)[0];
186 3 $err =~ s!('-?\d+'\s|\s[\d\.]+)!!g;
187 3 $err .= '.';
188 3 return $err;
189 }
190  
191 28 ▶sub check_uri {
Subroutine check_uri: called CC=2 SCAR=7.1
Pod documented
192 28 ▶ return 'An URI can only only contain ASCII characters.' if $_[0] =~ m!\P{ASCII}!;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\P{ASCII}/u 0 28 28
193 28 local $IRI_TEST_NAME = 'uri';
194 28 return check_iri_reference($_[0]);
195 }
196  
197 132 ▶sub check_uri_reference {
Subroutine check_uri_reference: called CC=1 SCAR=0.0
Pod documented
198 132 local $IRI_TEST_NAME = 'uri-reference';
199 132 return check_iri_reference($_[0]);
200 }
201  
202 1 ▶sub check_uri_template {
Subroutine check_uri_template: called CC=1 SCAR=0.0
Pod documented
203 1 return check_iri($_[0]);
204 }
205  
206 2 ▶sub check_uuid {
Subroutine check_uuid: called CC=2 SCAR=6.9
Pod documented
207 2 state $uuid_re = do {
208 1 my $x = qr{[0-9A-Fa-f]};
209 1 qr{^$x$x$x$x$x$x$x$x-$x$x$x$x-[0-9]$x$x$x-$x$x$x$x-$x$x$x$x$x$x$x$x$x$x$x$x$};
210 };
211  
212 2 ▶ return $_[0] =~ $uuid_re ? undef : 'Does not match uuid format.';
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /$uuid_re/u ? : 1 1 2
213 }
214  
215 25 ▶sub _match_number {
Subroutine _match_number: called CC=6 SCAR=18.2
216 25 my ($name, $val, $format) = @_;
217 25 ▶ return 'Does not look like an integer' if $name =~ m!^int! and $val !~ /^-?\d+(\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^int/u and not $val =~ /^-?\d+(\.\d+)?$/u 0 25 25
Condition66%2/3
$name =~ /^int/u and not $val =~ /^-?\d+(\.\d+)?$/u
!ll&&!rl&&r
3220
MC/DC0%0/2
$name =~ /^int/u and not $val =~ /^-?\d+(\.\d+)?$/u
$name =~ /^int/u not $val =~ /^-?\d+(\.\d+)?$/u
Truth table66%2/3
A and not(B)
A= $name =~ /^int/uB= not $val =~ /^-?\d+(\.\d+)?$/u
ABresult
0X0
100
111
218 25 ▶ return 'Does not look like a number.' unless looks_like_number $val;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($val) 0 25 25
219 25 ▶ return undef unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 3 22 25
220 22 ▶ return undef if $val eq unpack $format, pack $format, $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq unpack($format, pack($format, $val)) 20 2 22
221 2 return "Does not match $name format.";
222 }
223  
224 0 ▶sub _module_missing {
Subroutine _module_missing: not called CC=1 SCAR=6.9
225 0 warn "[JSON::Validator] Cannot validate $_[0] format: $_[1] is missing" if WARN_MISSING_MODULE;
226 0 return undef;
227 }
228  
229 82 1;
230  
231 =encoding utf8
232  
233 =head1 NAME
234  
235 JSON::Validator::Formats - Functions for validating JSON schema formats
236  
237 =head1 SYNOPSIS
238  
239 use JSON::Validator::Formats;
240 my $error = JSON::Validator::Formats::check_uri($str);
241 die $error if $error;
242  
243 my $jv = JSON::Validator->new;
244 $jv->formats({
245 "date-time" => JSON::Validator::Formats->can("check_date_time"),
246 "email" => JSON::Validator::Formats->can("check_email"),
247 "hostname" => JSON::Validator::Formats->can("check_hostname"),
248 "ipv4" => JSON::Validator::Formats->can("check_ipv4"),
249 "ipv6" => JSON::Validator::Formats->can("check_ipv6"),
250 "regex" => JSON::Validator::Formats->can("check_regex"),
251 "uri" => JSON::Validator::Formats->can("check_uri"),
252 "uri-reference" => JSON::Validator::Formats->can("check_uri_reference"),
253 });
254  
255 =head1 DESCRIPTION
256  
257 L<JSON::Validator::Formats> is a module with utility functions used by
258 L<JSON::Validator/formats> to match JSON Schema formats.
259 All functions return C<undef> for success or an error message for failure.
260  
261 =head1 FUNCTIONS
262  
263 =head2 check_byte
264  
265 my $str_or_undef = check_byte $str;
266  
267 Checks that the string matches byte format.
268  
269 =head2 check_date
270  
271 my $str_or_undef = check_date $str;
272  
273 Validates the date part of a RFC3339 string.
274  
275 =head2 check_date_time
276  
277 my $str_or_undef = check_date_time $str;
278  
279 Validated against RFC3339 timestamp in UTC time. This is formatted as
280 "YYYY-MM-DDThh:mm:ss.fffZ". The milliseconds portion (".fff") is optional
281  
282 =head2 check_duration
283  
284 my $str_or_undef = check_duration $str;
285  
286 Validate a RFC3339 duration string, such as "P3Y6M4DT12H30M5S".
287  
288 =head2 check_double
289  
290 my $str_or_undef = check_double $number;
291  
292 Tries to check if the number is a double. Note that this check is not very
293 accurate.
294  
295 =head2 check_email
296  
297 my $str_or_undef = check_email $str;
298  
299 Validated against the RFC5322 spec.
300  
301 =head2 check_float
302  
303 my $str_or_undef = check_float $number;
304  
305 Tries to check if the number is a float. Note that this check is not very
306 accurate.
307  
308 =head2 check_hostname
309  
310 my $str_or_undef = check_hostname $str;
311  
312 Will be validated using L<Data::Validate::Domain/is_hostname>, if installed.
313  
314 =head2 check_idn_email
315  
316 my $str_or_undef = check_idn_email $str;
317  
318 Will validate an email with non-ASCII characters using L<Net::IDN::Encode> if
319 installed.
320  
321 =head2 check_idn_hostname
322  
323 my $str_or_undef = check_idn_hostname $str;
324  
325 Will validate a hostname with non-ASCII characters using L<Net::IDN::Encode> if
326 installed.
327  
328 =head2 check_int32
329  
330 my $str_or_undef = check_int32 $number;
331  
332 Tries to check if the number is a int32. Note that this check is not very
333 accurate.
334  
335 =head2 check_int64
336  
337 my $str_or_undef = check_int64 $number;
338  
339 Tries to check if the number is a int64. Note that this check is not very
340 accurate.
341  
342 =head2 check_ipv4
343  
344 my $str_or_undef = check_ipv4 $str;
345  
346 Will be validated using L<Data::Validate::IP/is_ipv4>, if installed or fall
347 back to a plain IPv4 IP regex.
348  
349 =head2 check_ipv6
350  
351 my $str_or_undef = check_ipv6 $str;
352  
353 Will be validated using L<Data::Validate::IP/is_ipv6>, if installed.
354  
355 =head2 check_iri
356  
357 my $str_or_undef = check_iri $str;
358  
359 Validate either an absolute IRI containing ASCII or non-ASCII characters,
360 against the RFC3986 spec.
361  
362 =head2 check_iri_reference
363  
364 my $str_or_undef = check_iri_reference $str;
365  
366 Validate either a relative or absolute IRI containing ASCII or non-ASCII
367 characters, against the RFC3986 spec.
368  
369 =head2 check_json_pointer
370  
371 my $str_or_undef = check_json_pointer $str;
372  
373 Validates a JSON pointer, such as "/foo/bar/42".
374  
375 =head2 check_regex
376  
377 my $str_or_undef = check_regex $str;
378  
379 Will check if the string is a regex, using C<qr{...}>.
380  
381 =head2 check_relative_json_pointer
382  
383 my $str_or_undef = check_relative_json_pointer $str;
384  
385 Validates a relative JSON pointer, such as "0/foo" or "3#".
386  
387 =head2 check_time
388  
389 my $str_or_undef = check_time $str;
390  
391 Validates the time and optionally the offset part of a RFC3339 string.
392  
393 =head2 check_uri
394  
395 my $str_or_undef = check_uri $str;
396  
397 Validate either a relative or absolute URI containing just ASCII characters,
398 against the RFC3986 spec.
399  
400 Note that this might change in the future to only check absolute URI.
401  
402 =head2 check_uri_reference
403  
404 my $str_or_undef = check_uri_reference $str;
405  
406 Validate either a relative or absolute URI containing just ASCII characters,
407 against the RFC3986 spec.
408  
409 =head2 check_uri_template
410  
411 my $str_or_undef = check_uri_reference $str;
412  
413 Validate an absolute URI with template characters.
414  
415 =head2 check_uuid
416  
417 my $str_or_undef = check_uuid $str;
418  
419 Will check if C<$str> looks like an UUID. Example UUID:
420 "5782165B-6BB6-472F-B3DD-369D707D6C72".
421  
422 =head1 SEE ALSO
423  
424 L<JSON::Validator>.
425  
426 =cut
« Validator/Error.pm ↑ Summary Validator/Joi.pm »