Modern/Utilities.pm

filter: Statementstmt 96.4% 404 / 419 Branchbran 84.2% 251 / 298 Conditioncond 83.3% 185 / 222 MC/DCmcdc 61.0% 72 / 118 Subroutinesub 94.5% 87 / 92 Podpod 60.5% 23 / 38 totaltotal 86.1% 1022 / 1187 CC 249 SCAR 25.5
CC
249
Cov
89%
is_equal
29.0
is_type
27.1
jsonp_set
23.2
CRAP 12.8
« Modern/Result.pm ↑ Summary Modern/Vocabulary/Applicator.pm »
1 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package JSON::Schema::Modern::Utilities;
4 # vim: set ts=8 sts=2 sw=2 tw=100 et :
5 # ABSTRACT: Internal utilities for JSON::Schema::Modern
6  
7 46 our $VERSION = '0.654';
8  
9 46 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 46 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 46 ▶use stable 0.031 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 46 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 46 ▶no autovivification warn => qw(fetch store exists delete);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 46 ▶use if "$]" >= 5.022, experimental => 're_strict';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 46 ▶use if "$]" < 5.025002, experimental => 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 46 ▶no if "$]" >= 5.031009, feature => 'indirect';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 46 ▶no if "$]" >= 5.033001, feature => 'multidimensional';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 46 ▶no if "$]" >= 5.033006, feature => 'bareword_filehandles';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 46 ▶no if "$]" >= 5.041009, feature => 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 46 ▶no feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 46 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 46 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 46 ▶use builtin::compat qw(blessed created_as_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 46 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 46 ▶use List::Util 'pairmap';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 46 ▶use if "$]" < 5.041010, 'List::Util' => 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 46 ▶use if "$]" >= 5.041010, experimental => 'keyword_any';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 46 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 46 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 46 ▶use Mojo::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 46 ▶use Mojo::JSON::Pointer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 46 ▶use Mojo::Util 'url_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 46 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 46 ▶use Types::Standard qw(Str InstanceOf Enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 46 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 46 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 46 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 46 our @EXPORT_OK = qw(
41 is_type
42 get_type
43 is_bool
44 is_schema
45 is_bignum
46 is_equal
47 is_elements_unique
48 canonicalize_uri
49 jsonp
50 unjsonp
51 jsonp_get
52 jsonp_elements
53 jsonp_set
54 local_annotations
55 canonical_uri
56 E
57 A
58 abort
59 assert_keyword_exists
60 assert_keyword_type
61 assert_pattern
62 assert_uri_reference
63 assert_uri
64 annotate_self
65 sprintf_num
66 true
67 false
68 json_pointer_type
69 canonical_uri_type
70 core_types_type
71 core_formats_type
72 register_schema
73 load_cached_document
74 parse_media_type
75 add_media_type
76 delete_media_type
77 decode_media_type
78 encode_media_type
79 match_media_type
80 );
81  
82 46 ▶use constant HAVE_BUILTIN => "$]" >= 5.035010;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 46 ▶use if HAVE_BUILTIN, experimental => 'builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 46 ▶use constant _BUILTIN_BOOLS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 46 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
87 _BUILTIN_BOOLS && HAVE_BUILTIN
88 46 && Mojo::JSON::JSON_XS && eval { Cpanel::JSON::XS->VERSION(4.38); 1 }
89 ? (true => builtin::true, false => builtin::false)
90 : (true => JSON::PP::true, false => JSON::PP::false)
91 46 };
92  
93 # Mojo::JSON::JSON_XS is false when the environment variable $MOJO_NO_JSON_XS is set
94 # and also checks if Cpanel::JSON::XS is installed.
95 # Mojo::JSON falls back to its own pure-perl encoder/decoder but does not support all the options
96 # that we require here.
97 46 ▶use constant _JSON_BACKEND =>
Subroutine BEGIN: called CC=3 SCAR=13.5
98 46 ▶ Mojo::JSON::JSON_XS && eval { Cpanel::JSON::XS->VERSION('4.38'); 1 } ? 'Cpanel::JSON::XS'
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'JSON::PP'->VERSION('4.11'); 1 } } ? : 0 0 0
99 0 ▶ : eval { JSON::PP->VERSION('4.11'); 1 } ? 'JSON::PP'
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'Cpanel::JSON::XS'->VERSION('4.38'); 1 } } ? : 46 0 46
100 46 : croak 'Cpanel::JSON::XS 4.38 or JSON::PP 4.11 is required';
101  
102 # supports the six core types, plus integer (which is also a number)
103 # we do NOT check stringy_numbers here -- you must do that in the caller
104 # note that sometimes a value may return true for more than one type, e.g. integer+number,
105 # or number+string, depending on its internal flags; use get_type() to report ambiguous values.
106 # pass { legacy_ints => 1 } in $config to use draft4 integer behaviour
107 # behaviour is consistent with get_type() (where integers are also numbers).
108 88069 ▶sub is_type ($type, $value, $config = {}) {
Subroutine is_type: called CC=27 SCAR=33.0
Pod documented
109 88069 ▶ if ($type eq 'null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "null") 113 87956 88069
110 113 return !(defined $value);
111 }
112 87956 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 11243 76713 87956
113 11243 return is_bool($value);
114 }
115 76713 ▶ if ($type eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "object") 20092 56621 76713
116 20092 return ref $value eq 'HASH';
117 }
118 56621 ▶ if ($type eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array") 14113 42508 56621
119 14113 return ref $value eq 'ARRAY';
120 }
121  
122 42508 ▶ if ($type eq 'string' or $type eq 'number' or $type eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string" or $type eq "number" or $type eq "integer") 42492 16 42508
Condition100%6/6
$type eq "string" or $type eq "number" or $type eq "integer"
l!l&&r!l&&!r
38519397316
$type eq "string" or $type eq "number"
l!l&&r!l&&!r
3036981503989
MC/DC100%3/3
$type eq "string" or $type eq "number" or $type eq "integer"
$type eq "string" $type eq "number" $type eq "integer"
Truth table100%4/4
A or B or C
A= $type eq "string"B= $type eq "number"C= $type eq "integer"
ABCresult
1XX1
01X1
0011
0000
123 42492 ▶ return 0 if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 20 42472 42492
124 42472 my $flags = B::svref_2object(\$value)->FLAGS;
125  
126 # dualvars with the same string and (stringified) numeric value could be either a string or a
127 # number, and before 5.36 we can't tell the difference, so we will answer yes for both.
128 # in 5.36+, stringified numbers still get a PV but don't have POK set, whereas
129 # numified strings do have POK set, so we can tell which one came first.
130  
131 42472 ▶ if ($type eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 30353 12119 42472
132 # like created_as_string, but rejects dualvars with stringwise-unequal string and numeric parts
133 return !length ref($value)
134 && !(HAVE_BUILTIN && builtin::is_bool($value))
135 && $flags & B::SVf_POK
136 && (!($flags & (B::SVf_IOK | B::SVf_NOK))
137 46 ▶ || do { no warnings 'numeric'; 0+$value eq $value });
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition100%12/12
!(length ref $value || builtin::is_bool($value)) && $flags & 1024 && (!($flags & 768) || do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value })
!ll&&!rl&&r
907329443
not length ref $value || builtin::is_bool($value) and $flags & 1024
!ll&&!rl&&r
65125629446
length ref $value || builtin::is_bool($value)
l!l&&r!l&&!r
649229702
not $flags & 768 or do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
l!l&&r!l&&!r
2943943
MC/DC100%5/5
!(length ref $value || builtin::is_bool($value)) && $flags & 1024 && (!($flags & 768) || do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value })
length ref $value builtin::is_bool($value) $flags & 1024 not $flags & 768 do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
Truth table100%6/6
not(A || B) and C && not(D) or E
A= length ref $valueB= builtin::is_bool($value)C= $flags & 1024D= not $flags & 768E= do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
ABCDEresult
1XXXX0
01XXX0
000XX0
001000
0011X1
001011
138 }
139  
140 12119 ▶ if ($type eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 8148 3971 12119
141 # floats in json will always be parsed into Math::BigFloat, when allow_bignum is enabled
142 8148 ▶ return is_bignum($value) || created_as_number($value);
Condition100%3/3
is_bignum($value) || &created_as_number($value)
l!l&&r!l&&!r
76361311254
MC/DC100%2/2
is_bignum($value) || &created_as_number($value)
is_bignum($value) &created_as_number($value)
Truth table100%3/3
A || B
A= is_bignum($value)B= &created_as_number($value)
ABresult
1X1
011
000
143 }
144  
145 3971 ▶ if ($type eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "integer") 3971 0 3971
146 3971 ▶ if ($config->{legacy_ints}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$config{'legacy_ints'}) { } 17 3954 3971
147 # in draft4, an integer is "A JSON number without a fraction or exponent part.",
148 # therefore 2.0 is NOT an integer
149 17 ▶ return ref($value) eq 'Math::BigInt'
Condition77%7/9
ref $value eq 'Math::BigInt' || $flags & 256 && !($flags & 512) && &created_as_number($value)
l!l&&r!l&&!r
269
$flags & 256 and not $flags & 512 and &created_as_number($value)
!ll&&!rl&&r
906
$flags & 256 and not $flags & 512
!ll&&!rl&&r
906
MC/DC50%2/4
ref $value eq 'Math::BigInt' || $flags & 256 && !($flags & 512) && &created_as_number($value)
ref $value eq 'Math::BigInt' $flags & 256 not $flags & 512 &created_as_number($value)
Truth table60%3/5
A || B and not(C) and D
A= ref $value eq 'Math::BigInt'B= $flags & 256C= not $flags & 512D= &created_as_number($value)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
150 || ($flags & B::SVf_IOK) && !($flags & B::SVf_NOK) && created_as_number($value);
151 }
152 else {
153 # note: values that are larger than $Config{ivsize} will be represented as an NV, not IV,
154 # therefore they will fail this check -- which is why use of Math::BigInt is recommended
155 # if the exact type is important, or if loss of any accuracy is unacceptable
156 3954 ▶ return is_bignum($value) && $value->is_int
Condition100%9/9
is_bignum($value) && $value->is_int || &created_as_number($value) && int $value == $value
l!l&&r!l&&!r
130376955
is_bignum($value) and $value->is_int
!ll&&!rl&&r
38186130
&created_as_number($value) and int $value == $value
!ll&&!rl&&r
5233769
MC/DC100%4/4
is_bignum($value) && $value->is_int || &created_as_number($value) && int $value == $value
is_bignum($value) $value->is_int &created_as_number($value) int $value == $value
Truth table71%5/7
A and B || C and D
A= is_bignum($value)B= $value->is_intC= &created_as_number($value)D= int $value == $value
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
157 # if dualvar, PV and stringified NV/IV must be identical
158 || created_as_number($value) && int($value) == $value;
159 }
160 }
161 }
162  
163 16 ▶ if ($type =~ /^reference to (.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^reference to (.+)\z/u) 11 5 16
164 11 ▶ return !blessed($value) && ref($value) eq $1;
Condition33%1/3
!builtin::blessed($value) && ref $value eq $1
!ll&&!rl&&r
0011
MC/DC0%0/2
!builtin::blessed($value) && ref $value eq $1
!builtin::blessed($value) ref $value eq $1
Truth table33%1/3
not(A) && B
A= !builtin::blessed($value)B= ref $value eq $1
ABresult
0X0
100
111
165 }
166  
167 5 return ref($value) eq $type;
168 }
169  
170 # returns one of the six core types, plus integer
171 # we do NOT check stringy_numbers here -- you must do that in the caller
172 # pass { legacy_ints => 1 } in $config to use draft4 integer behaviour
173 # behaviour is consistent with is_type().
174 124359 ▶sub get_type ($value, $config = {}) {
Subroutine get_type: called CC=21 SCAR=30.5
Pod documented
175 124359 ▶ return 'object' if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 75208 49151 124359
176 49151 ▶ return 'boolean' if is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_bool($value) 11418 37733 49151
177 37733 ▶ return 'null' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 687 37046 37733
178 37046 ▶ return 'array' if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 6119 30927 37046
179  
180 # floats in json will always be parsed into Math::BigFloat, when allow_bignum is enabled
181 30927 ▶ if (length(my $ref = ref $value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $ref = ref $value)) 930 29997 30927
182 return $ref eq 'Math::BigInt' ? 'integer'
183 930 ▶ : $ref eq 'Math::BigFloat' ? (!$config->{legacy_ints} && $value->is_int ? 'integer' : 'number')
Branch: 4 branches
BranchTrueFalseTotal
$ref eq 'Math::BigInt' ? : 182 748 930
$ref eq 'Math::BigFloat' ? : 677 71 748
!$$config{'legacy_ints'} && $value->is_int ? : 327 350 677
defined builtin::blessed($value) ? : 7 64 71
Condition100%3/3
not $$config{"legacy_ints"} and $value->is_int
!ll&&!rl&&r
53297327
MC/DC100%2/2
not $$config{"legacy_ints"} and $value->is_int
not $$config{"legacy_ints"} $value->is_int
Truth table100%3/3
not(A) and B
A= not $$config{"legacy_ints"}B= $value->is_int
ABresult
0X0
100
111
184 : (defined blessed($value) ? '' : 'reference to ').$ref;
185 }
186  
187 29997 my $flags = B::svref_2object(\$value)->FLAGS;
188  
189 # dualvars with the same string and (stringified) numeric value could be either a string or a
190 # number, and before 5.36 we can't tell the difference, so we choose number because it has been
191 # evaluated as a number already.
192 # in 5.36+, stringified numbers still get a PV but don't have POK set, whereas
193 # numified strings do have POK set, so we can tell which one came first.
194  
195 # like created_as_string, but rejects dualvars with stringwise-unequal string and numeric parts
196 return 'string'
197 if $flags & B::SVf_POK
198 && (!($flags & (B::SVf_IOK | B::SVf_NOK))
199 46 ▶ || do { no warnings 'numeric'; 0+$value eq $value });
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 1024 and !($flags & 768) || do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value } 23049 6948 29997
Condition100%6/6
$flags & 1024 and !($flags & 768) || do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
!ll&&!rl&&r
69331523049
not $flags & 768 or do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
l!l&&r!l&&!r
23041815
MC/DC100%3/3
$flags & 1024 and !($flags & 768) || do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
$flags & 1024 not $flags & 768 do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
Truth table100%4/4
A and not(B) or C
A= $flags & 1024B= not $flags & 768C= do { BEGIN {${^WARNING_BITS} = "\xdd\x7f\xf7\xc3\xd7\xff\xf5\xff\xff\xff\xff\xff\xfd\x35\xfd\x75\x45\x54\x55\x51\x55\x55\x55"} 0 + $value eq $value }
ABCresult
0XX0
1000
11X1
1011
200  
201 6948 ▶ if ($config->{legacy_ints}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$config{'legacy_ints'}) { } 316 6632 6948
202 # in draft4, an integer is "A JSON number without a fraction or exponent part.",
203 # therefore 2.0 is NOT an integer
204 316 ▶ return ($flags & B::SVf_IOK) && !($flags & B::SVf_NOK) ? 'integer' : 'number'
Branch: 2 branches
BranchTrueFalseTotal
if &created_as_number($value) 316 0 316
$flags & 256 && !($flags & 512) ? : 313 3 316
Condition66%2/3
$flags & 256 and not $flags & 512
!ll&&!rl&&r
30313
MC/DC50%1/2
$flags & 256 and not $flags & 512
$flags & 256 not $flags & 512
Truth table66%2/3
A and not(B)
A= $flags & 256B= not $flags & 512
ABresult
0X0
100
111
205 if created_as_number($value);
206 }
207 else {
208 # note: values that are larger than $Config{ivsize} will be represented as an NV, not IV,
209 # therefore they will fail this check -- which is why use of Math::BigInt is recommended
210 # if the exact type is important, or loss of any accuracy is unacceptable
211 6632 ▶ return int($value) == $value ? 'integer' : 'number' if created_as_number($value);
Branch: 2 branches
BranchTrueFalseTotal
if &created_as_number($value) 6617 15 6632
int $value == $value ? : 6575 42 6617
212 }
213  
214 # this might be a scalar with POK|IOK or POK|NOK set
215 15 return 'ambiguous type';
216 }
217  
218 # lifted from JSON::MaybeXS
219 # note: unlike builtin::compat::is_bool on older perls, we do not accept
220 # dualvar(0,"") or dualvar(1,"1") because JSON::PP and Cpanel::JSON::XS
221 # do not encode these as booleans.
222 60804 ▶sub is_bool ($value) {
Subroutine is_bool: called CC=5 SCAR=16.2
Pod documented
223 60804 ▶ HAVE_BUILTIN and builtin::is_bool($value)
Branch: 1 branch
BranchTrueFalseTotal
builtin::is_bool($value) or not !builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("Cpanel::JSON::XS::Boolean") || $value->isa("JSON::XS::Boolean") 4 60800 60804
Condition83%10/12
builtin::is_bool($value) || !!builtin::blessed($value) && ($value->isa('JSON::PP::Boolean') || $value->isa('Cpanel::JSON::XS::Boolean') || $value->isa('JSON::XS::Boolean'))
l!l&&r!l&&!r
41591244888
not !builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("Cpanel::JSON::XS::Boolean") || $value->isa("JSON::XS::Boolean")
!ll&&!rl&&r
4398190715912
$value->isa("JSON::PP::Boolean") or $value->isa("Cpanel::JSON::XS::Boolean") or $value->isa("JSON::XS::Boolean")
l!l&&r!l&&!r
159120907
$value->isa("JSON::PP::Boolean") or $value->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
159120907
MC/DC60%3/5
builtin::is_bool($value) || !!builtin::blessed($value) && ($value->isa('JSON::PP::Boolean') || $value->isa('Cpanel::JSON::XS::Boolean') || $value->isa('JSON::XS::Boolean'))
builtin::is_bool($value) not !builtin::blessed($value) $value->isa("JSON::PP::Boolean") $value->isa("Cpanel::JSON::XS::Boolean") $value->isa("JSON::XS::Boolean")
Truth table66%4/6
A || B and C or D or E
A= builtin::is_bool($value)B= not !builtin::blessed($value)C= $value->isa("JSON::PP::Boolean")D= $value->isa("Cpanel::JSON::XS::Boolean")E= $value->isa("JSON::XS::Boolean")
ABCDEresult
1XXXX1
011XX1
0101X1
010011
00XXX0
010000
224 or
225 !!blessed($value)
226 and ($value->isa('JSON::PP::Boolean')
227 or $value->isa('Cpanel::JSON::XS::Boolean')
228 or $value->isa('JSON::XS::Boolean'));
229 }
230  
231 34762 ▶sub is_schema ($value) {
Subroutine is_schema: called CC=2 SCAR=6.9
Pod documented
232 34762 ▶ ref $value eq 'HASH' || is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "HASH" or is_bool($value) 34352 410 34762
233 }
234  
235 13741 ▶sub is_bignum ($value) {
Subroutine is_bignum: called CC=1 SCAR=0.0
Pod undocumented
236 13741 ref($value) =~ /^Math::Big(?:Int|Float)\z/;
237 }
238  
239 # compares two arbitrary data payloads for equality, as per
240 # https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.4.2.2
241 # $state hashref supports the following fields:
242 # - scalarref_booleans (input): treats \0 and \1 as boolean values
243 # - stringy_numbers (input): strings will also be compared numerically
244 # - path (output): location of the first difference
245 # - error (output): description of the first difference
246 17606 ▶sub is_equal ($x, $y, $state = {}) {
Subroutine is_equal: called CC=29 SCAR=33.7
Pod documented
247 17606 ▶ $state->{path} //= '';
Condition100%2/2
$$state{"path"} //= ""
l!l
108966710
MC/DC100%1/1
$$state{"path"} //= ""
$$state{"path"}
Truth table100%2/2
A
A= $$state{"path"}
Aresult
11
00
248  
249 17606 my @types = map get_type($_), $x, $y;
250  
251 17606 ▶ $state->{error} = 'ambiguous type encountered', return 0
Branch: 1 branch
BranchTrueFalseTotal
if grep(($types[$_] eq "ambiguous type"), (0, 1)) 3 17603 17606
252 if grep $types[$_] eq 'ambiguous type', 0..1;
253  
254 17603 ▶ if ($state->{scalarref_booleans}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$state{"scalarref_booleans"}) 88 17515 17603
255 88 ▶ ($x, $types[0]) = (0+!!$$x, 'boolean') if $types[0] eq 'reference to SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $types[0] eq "reference to SCALAR" 14 74 88
256 88 ▶ ($y, $types[1]) = (0+!!$$y, 'boolean') if $types[1] eq 'reference to SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $types[1] eq "reference to SCALAR" 18 70 88
257 }
258  
259 17603 ▶ if ($state->{stringy_numbers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$state{"stringy_numbers"}) 16 17587 17603
260 16 ▶ ($x, $types[0]) = (0+$x, int(0+$x) == $x ? 'integer' : 'number')
Branch: 2 branches
BranchTrueFalseTotal
if $types[0] eq "string" and &looks_like_number($x) 8 8 16
int 0 + $x == $x ? : 5 3 8
Condition100%3/3
$types[0] eq "string" and &looks_like_number($x)
!ll&&!rl&&r
538
MC/DC100%2/2
$types[0] eq "string" and &looks_like_number($x)
$types[0] eq "string" &looks_like_number($x)
Truth table100%3/3
A and B
A= $types[0] eq "string"B= &looks_like_number($x)
ABresult
0X0
100
111
261 if $types[0] eq 'string' and looks_like_number($x);
262  
263 16 ▶ ($y, $types[1]) = (0+$y, int(0+$y) == $y ? 'integer' : 'number')
Branch: 2 branches
BranchTrueFalseTotal
if $types[1] eq "string" and &looks_like_number($y) 5 11 16
int 0 + $y == $y ? : 4 1 5
Condition100%3/3
$types[1] eq "string" and &looks_like_number($y)
!ll&&!rl&&r
835
MC/DC100%2/2
$types[1] eq "string" and &looks_like_number($y)
$types[1] eq "string" &looks_like_number($y)
Truth table100%3/3
A and B
A= $types[1] eq "string"B= &looks_like_number($y)
ABresult
0X0
100
111
264 if $types[1] eq 'string' and looks_like_number($y);
265 }
266  
267 17603 ▶ $state->{error} = "wrong type: $types[0] vs $types[1]", return 0 if $types[0] ne $types[1];
Branch: 1 branch
BranchTrueFalseTotal
if $types[0] ne $types[1] 1376 16227 17603
268 16227 ▶ return 1 if $types[0] eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $types[0] eq "null" 62 16165 16227
269 16165 ▶ ($x eq $y and return 1), $state->{error} = 'strings not equal', return 0
Branch: 1 branch
BranchTrueFalseTotal
if $types[0] eq "string" 9698 6467 16165
Condition100%2/2
$x eq $y and return 1
!ll
16448054
MC/DC100%1/1
$x eq $y and return 1
$x eq $y
Truth table100%2/2
A
A= $x eq $y
Aresult
00
11
270 if $types[0] eq 'string';
271 6467 ▶ ($x == $y and return 1), $state->{error} = "$types[0]s not equal", return 0
Branch: 1 branch
BranchTrueFalseTotal
if grep(($types[0] eq $_), ("boolean", "number", "integer")) 2118 4349 6467
Condition100%2/2
$x == $y and return 1
!ll
6901428
MC/DC100%1/1
$x == $y and return 1
$x == $y
Truth table100%2/2
A
A= $x == $y
Aresult
00
11
272 if grep $types[0] eq $_, qw(boolean number integer);
273  
274 4349 my $path = $state->{path};
275 4349 ▶ if ($types[0] eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($types[0] eq "object") 1864 2485 4349
276 1864 ▶ $state->{error} = 'property count differs: '.keys(%$x).' vs '.keys(%$y), return 0
Branch: 1 branch
BranchTrueFalseTotal
if keys %$x != keys %$y 101 1763 1864
277 if keys %$x != keys %$y;
278  
279 1763 ▶ if (not is_equal(my $arr_x = [ sort keys %$x ], my $arr_y = [ sort keys %$y ], my $s={})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_equal(my $arr_x = [sort(keys %$x)], my $arr_y = [sort(keys %$y)], scalar(my $s = {}))) 11 1752 1763
280 11 my $pos = substr($s->{path}, 1);
281 11 $state->{error} = 'property names differ starting at position '.$pos.' ("'.$arr_x->[$pos].'" vs "'.$arr_y->[$pos].'")';
282 11 return 0;
283 }
284  
285 1752 foreach my $property (sort keys %$x) {
286 4115 $state->{path} = jsonp($path, $property);
287 4115 ▶ return 0 if not is_equal($x->{$property}, $y->{$property}, $state);
Branch: 1 branch
BranchTrueFalseTotal
unless is_equal($$x{$property}, $$y{$property}, $state) 177 3938 4115
288 }
289  
290 1575 return 1;
291 }
292  
293 2485 ▶ if ($types[0] eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($types[0] eq "array") 2485 0 2485
294 2485 ▶ $state->{error} = 'element count differs: '.@$x.' vs '.@$y, return 0 if @$x != @$y;
Branch: 1 branch
BranchTrueFalseTotal
if @$x != @$y 11 2474 2485
295 2474 foreach my $idx (0 .. $x->$#*) {
296 5774 $state->{path} = $path.'/'.$idx;
297 5774 ▶ return 0 if not is_equal($x->[$idx], $y->[$idx], $state);
Branch: 1 branch
BranchTrueFalseTotal
unless is_equal($$x[$idx], $$y[$idx], $state) 198 5576 5774
298 }
299 2276 return 1;
300 }
301  
302 0 $state->{error} = 'got surprising type: '.$types[0], return 0; # should never get here
303 }
304  
305 # checks array elements for uniqueness. short-circuits on first pair of matching elements
306 # $state hashref supports the following fields:
307 # - scalarref_booleans (input): treats \0 and \1 as boolean values
308 # - stringy_numbers (input): strings will also be compared numerically
309 # - path (output): location of the first difference
310 # - error (output): description of the first difference
311 # - equal_indices (output): the indices of identical items
312 3609 ▶sub is_elements_unique ($array, $state = {}) {
Subroutine is_elements_unique: called CC=6 SCAR=18.0
Pod documented
313 3609 foreach my $idx0 (0 .. $array->$#*-1) {
314 1447 foreach my $idx1 ($idx0+1 .. $array->$#*) {
315 2103 ▶ if (is_equal($array->[$idx0], $array->[$idx1], $state)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_equal($$array[$idx0], $$array[$idx1], $state)) 280 1823 2103
316 280 ▶ push $state->{equal_indices}->@*, $idx0, $idx1 if exists $state->{equal_indices};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$state{"equal_indices"} 280 0 280
317 280 return 0;
318 }
319 }
320 }
321 3329 return 1;
322 }
323  
324 # given a URI to a schema resource, return the canonical form of this URI
325 2 ▶sub canonicalize_uri ($evaluator, $uri) {
Subroutine canonicalize_uri: called CC=2 SCAR=7.2
Pod documented
326 2 my $schema_resource = $evaluator->_fetch_from_uri($uri);
327 2 ▶ return $schema_resource ? $schema_resource->{canonical_uri} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$schema_resource ? : 2 0 2
328 }
329  
330 # shorthand for creating and appending json pointers
331 # the first argument is an already-encoded json pointer; remaining arguments are path segments to be
332 # encoded and appended
333 52288 ▶sub jsonp {
Subroutine jsonp: called CC=3 SCAR=11.7
Pod documented
334 52288 ▶ carp q{first argument to jsonp should be '' or start with '/'} if length($_[0]) and substr($_[0],0,1) ne '/';
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] and substr($_[0], 0, 1) ne "/" 0 52288 52288
Condition66%2/3
length $_[0] and substr($_[0], 0, 1) ne "/"
!ll&&!rl&&r
38466138220
MC/DC0%0/2
length $_[0] and substr($_[0], 0, 1) ne "/"
length $_[0] substr($_[0], 0, 1) ne "/"
Truth table66%2/3
A and B
A= length $_[0]B= substr($_[0], 0, 1) ne "/"
ABresult
0X0
100
111
335 52288 return join('/', shift, map s!~!~0!gr =~ s!/!~1!gr, grep defined, @_);
336 }
337  
338 # splits a json pointer apart into its path segments
339 0 ▶sub unjsonp {
Subroutine unjsonp: not called CC=3 SCAR=24.8
Pod documented
340 0 ▶ carp q{argument to unjsonp should be '' or start with '/'} if length($_[0]) and substr($_[0],0,1) ne '/';
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] and substr($_[0], 0, 1) ne "/" 0 0 0
Condition0%0/3
length $_[0] and substr($_[0], 0, 1) ne "/"
!ll&&!rl&&r
000
MC/DC0%0/2
length $_[0] and substr($_[0], 0, 1) ne "/"
length $_[0] substr($_[0], 0, 1) ne "/"
Truth table0%0/3
A and B
A= length $_[0]B= substr($_[0], 0, 1) ne "/"
ABresult
0X0
100
111
341 0 return map s!~0!~!gr =~ s!~1!/!gr, split m!/!, $_[0];
342 }
343  
344 0 ▶sub jsonp_get ($data, $pointer) {
Subroutine jsonp_get: not called CC=1 SCAR=6.9
Pod documented
345 0 Mojo::JSON::Pointer->new($data)->get($pointer);
346 }
347  
348 # flatten the data structure into a hashref of { pointer => value, ... }
349 # (essentially the reverse of jsonp_set($data, $foo->%{$_}) foreach keys $foo)
350 26 ▶sub jsonp_elements ($data, $prefix = '') {
Subroutine jsonp_elements: called CC=5 SCAR=16.2
Pod documented
351 # recursively walk the structure..
352 my $hash = +{
353 ref $data eq '' ? ($prefix => $data)
354 26 ▶ : ref $data eq 'HASH' ? map jsonp_elements($data->{$_}, $prefix.'/'.(s!~!~0!gr =~ s!/!~1!gr))->%*, keys %$data
Branch: 3 branches
BranchTrueFalseTotal
ref $data eq '' ? : 14 12 26
ref $data eq 'HASH' ? : 9 3 12
ref $data eq 'ARRAY' ? : 3 0 3
355 : ref $data eq 'ARRAY' ? map jsonp_elements($data->[$_], $prefix.'/'.$_)->%*, 0..$data->$#*
356 : croak 'unrecognized type: '. ref $data
357 };
358 }
359  
360 # assigns a value to a data structure at a specific json pointer location
361 # operates destructively, in place, unless the root data or type is being modified
362 17461 ▶sub jsonp_set ($data, $pointer, $value) {
Subroutine jsonp_set: called CC=23 SCAR=31.4
Pod documented
363 17461 ▶ croak 'cannot write into a non-reference in void context'
Branch: 1 branch
BranchTrueFalseTotal
unless grep((ref $data eq $_), ("HASH", "ARRAY")) or defined wantarray 2 17459 17461
Condition100%3/3
grep((ref $data eq $_), ("HASH", "ARRAY")) or defined wantarray
l!l&&r!l&&!r
58174012
MC/DC100%2/2
grep((ref $data eq $_), ("HASH", "ARRAY")) or defined wantarray
grep((ref $data eq $_), ("HASH", "ARRAY")) defined wantarray
Truth table100%3/3
A or B
A= grep((ref $data eq $_), ("HASH", "ARRAY"))B= defined wantarray
ABresult
1X1
011
000
364 if not grep ref $data eq $_, qw(HASH ARRAY) and not defined wantarray;
365  
366 # assigning to the root overwrites existing data
367 17459 ▶ if (not length $pointer) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $pointer) 17402 57 17459
368 17402 ▶ if (not ref $data or ref $data ne ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $data or ref $data ne ref $value) 17400 2 17402
Condition100%3/3
not ref $data or ref $data ne ref $value
l!l&&r!l&&!r
1739642
MC/DC100%2/2
not ref $data or ref $data ne ref $value
not ref $data ref $data ne ref $value
Truth table100%3/3
not(A) or B
A= not ref $dataB= ref $data ne ref $value
ABresult
1X1
011
000
369 17400 ▶ return $value if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 17398 2 17400
370 2 croak 'cannot write into a reference of a different type in void context';
371 }
372  
373 2 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 1 2
374 1 ▶ $data = {} if not ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 1 1
375 1 $data->%* = $value->%*;
376 }
377 2 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 1 2
378 1 ▶ $data = [] if not ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 1 1
379 1 $data->@* = $value->@*;
380 }
381  
382 2 return $data;
383 }
384  
385 57 ▶ my @keys = map +(s!~0!~!gr =~ s!~1!/!gr),
Branch: 1 branch
BranchTrueFalseTotal
length $pointer ? : 57 0 57
386 (length $pointer ? (split /\//, $pointer, -1) : ($pointer));
387  
388 57 ▶ croak 'cannot write a hashref into a reference to an array in void context'
Branch: 1 branch
BranchTrueFalseTotal
if @keys >= 2 and not $keys[1] =~ /^(?:\d+|-)\z/a and ref $data eq "ARRAY" and not defined wantarray 1 56 57
Condition77%7/9
@keys >= 2 and not $keys[1] =~ /^(?:\d+|-)\z/a and ref $data eq "ARRAY" and not defined wantarray
!ll&&!rl&&r
5601
@keys >= 2 and not $keys[1] =~ /^(?:\d+|-)\z/a and ref $data eq "ARRAY"
!ll&&!rl&&r
9471
@keys >= 2 and not $keys[1] =~ /^(?:\d+|-)\z/a
!ll&&!rl&&r
0948
MC/DC50%2/4
@keys >= 2 and not $keys[1] =~ /^(?:\d+|-)\z/a and ref $data eq "ARRAY" and not defined wantarray
@keys >= 2 not $keys[1] =~ /^(?:\d+|-)\z/a ref $data eq "ARRAY" not defined wantarray
Truth table60%3/5
A and not(B) and C and not(D)
A= @keys >= 2B= not $keys[1] =~ /^(?:\d+|-)\z/aC= ref $data eq "ARRAY"D= not defined wantarray
ABCDresult
0XXX0
10XX0
110X0
11100
11111
389 if @keys >= 2 and $keys[1] !~ /^(?:\d+|-)\z/a and ref $data eq 'ARRAY' and not defined wantarray;
390  
391 56 shift @keys; # always '', indicating the root
392 56 my $curp = \$data;
393  
394 56 foreach my $key (@keys) {
395 # if needed, first remove the existing data so we can replace with a new hash key or array index
396 115 ▶ undef $curp->$*
Branch: 1 branch
BranchTrueFalseTotal
if not ref $$curp or ref $$curp eq "ARRAY" and not $key =~ /^(?:\d+|-)\z/a 47 68 115
Condition100%6/6
not ref $$curp or ref $$curp eq "ARRAY" and not $key =~ /^(?:\d+|-)\z/a
l!l&&r!l&&!r
45268
ref $$curp eq "ARRAY" and not $key =~ /^(?:\d+|-)\z/a
!ll&&!rl&&r
53152
MC/DC100%3/3
not ref $$curp or ref $$curp eq "ARRAY" and not $key =~ /^(?:\d+|-)\z/a
not ref $$curp ref $$curp eq "ARRAY" not $key =~ /^(?:\d+|-)\z/a
Truth table100%4/4
not(A) or B and not(C)
A= not ref $$curpB= ref $$curp eq "ARRAY"C= not $key =~ /^(?:\d+|-)\z/a
ABCresult
1XX1
0111
00X0
0100
397 if not ref $curp->$*
398 or ref $curp->$* eq 'ARRAY' and $key !~ /^(?:\d+|-)\z/a;
399  
400 # use this existing hash key or array index location, or create new position
401 46 ▶ use autovivification 'store';
Subroutine BEGIN: called CC=1 SCAR=0.0
402 $curp = \(
403 115 ▶ ref $curp->$* eq 'HASH' || $key !~ /^(?:\d+|-)\z/a ? $curp->$*->{$key}
Branch: 2 branches
BranchTrueFalseTotal
ref $$curp eq 'HASH' || !($key =~ /^(?:\d+|-)\z/a) ? : 88 27 115
$key =~ /^\d+\z/a ? : 25 2 27
Condition100%3/3
ref $$curp eq "HASH" or not $key =~ /^(?:\d+|-)\z/a
l!l&&r!l&&!r
533527
MC/DC100%2/2
ref $$curp eq "HASH" or not $key =~ /^(?:\d+|-)\z/a
ref $$curp eq "HASH" not $key =~ /^(?:\d+|-)\z/a
Truth table100%3/3
A or not(B)
A= ref $$curp eq "HASH"B= not $key =~ /^(?:\d+|-)\z/a
ABresult
1X1
011
000
404 : $key =~ /^\d+\z/a ? $curp->$*->[$key]
405 : $curp->$*->[$curp->$*->$#* + 1]);
406 }
407  
408 56 $curp->$* = $value;
409 56 return $data;
410 }
411  
412 # returns a reusable Types::Standard type for json pointers
413 # TODO: move this off into its own distribution, see JSON::Schema::Types
414 414 ▶sub json_pointer_type () { Str->where('!length || m{^/} && !m{~(?![01])}'); }
Subroutine json_pointer_type: called CC=1 SCAR=0.0
Pod documented
415  
416 # a URI without a fragment, or with a json pointer fragment
417 92 ▶sub canonical_uri_type () {
Subroutine canonical_uri_type: called CC=1 SCAR=0.0
Pod documented
418 92 (InstanceOf['Mojo::URL'])->where(q{!defined($_->fragment) || $_->fragment =~ m{^/} && $_->fragment !~ m{~(?![01])}});
419 }
420  
421 # Validation §7.1-2: "Note that the "type" keyword in this specification defines an "integer" type
422 # which is not part of the data model. Therefore a format attribute can be limited to numbers, but
423 # not specifically to integers."
424 92 ▶sub core_types_type () {
Subroutine core_types_type: called CC=1 SCAR=0.0
Pod documented
425 92 Enum[qw(null object array boolean string number)];
426 }
427  
428 4 ▶sub core_formats_type () {
Subroutine core_formats_type: called CC=1 SCAR=0.0
Pod documented
429 4 Enum[qw(date-time date time duration email idn-email hostname idn-hostname ipv4 ipv6 uri uri-reference iri iri-reference uuid uri-template json-pointer relative-json-pointer regex)];
430 }
431  
432 # simple runtime-wide cache of $ids to schema document objects that are sourced from disk
433 {
434 46 my $document_cache = {};
435  
436 # adds a document to the cache, if its URI is in the cacheable list
437 # for internal use only!!
438 12 ▶ sub __populate_cached_document ($evaluator, $uri, $doc) {
Subroutine __populate_cached_document: called CC=3 SCAR=14.2
439 $document_cache->{$uri} = $doc
440 12 ▶ if not exists $document_cache->{$uri} and get_schema_filename($uri);
Branch: 1 branch
BranchTrueFalseTotal
if not exists $$document_cache{$uri} and get_schema_filename($uri) 0 12 12
Condition33%1/3
not exists $$document_cache{$uri} and get_schema_filename($uri)
!ll&&!rl&&r
0120
MC/DC0%0/2
not exists $$document_cache{$uri} and get_schema_filename($uri)
not exists $$document_cache{$uri} get_schema_filename($uri)
Truth table33%1/3
not(A) and B
A= not exists $$document_cache{$uri}B= get_schema_filename($uri)
ABresult
0X0
100
111
441 }
442  
443 # Fetches a document from the cache (reading it from disk and creating the document if necessary),
444 # and add it to the evaluator.
445 # Normally this will just be a cache of schemas that are bundled with this distribution or a related
446 # distribution (such as OpenAPI-Modern), as duplicate identifiers are not checked for, unlike for
447 # normal schema additions.
448 # Only JSON-encoded files are supported at this time.
449 126 ▶ sub load_cached_document ($evaluator, $uri) {
Subroutine load_cached_document: called CC=6 SCAR=18.0
Pod documented
450 126 $uri =~ s/#\z//; # older draft $ids use an empty fragment
451  
452 # see if it already exists as a document in the cache
453 126 my $document = $document_cache->{$uri};
454  
455 # otherwise, load it from disk using our filename cache and create the document
456 126 ▶ if (not $document and my $filename = get_schema_filename($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $document and my $filename = get_schema_filename($uri)) 68 58 126
Condition100%3/3
not $document and my $filename = get_schema_filename($uri)
!ll&&!rl&&r
352368
MC/DC100%2/2
not $document and my $filename = get_schema_filename($uri)
not $document my $filename = get_schema_filename($uri)
Truth table100%3/3
not(A) and B
A= not $documentB= my $filename = get_schema_filename($uri)
ABresult
0X0
100
111
457 68 my $file = path($filename);
458 68 ▶ croak "uri $uri maps to file $file which does not exist" if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 68 68
459 68 my $schema = $evaluator->_json_decoder->decode($file->slurp);
460  
461 # avoid calling add_schema, which checksums the file to look for duplicates
462 68 $document = JSON::Schema::Modern::Document->new(
463 schema => $schema,
464 evaluator => $evaluator,
465 skip_ref_checks => 1,
466 );
467  
468 # avoid calling add_document, which checks for duplicate identifiers (and would result in an
469 # infinite loop)
470 68 ▶ die JSON::Schema::Modern::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
if $document->has_errors 0 68 68
471 output_format => $evaluator->output_format,
472 valid => 0,
473 errors => [ $document->errors ],
474 exception => 1,
475 ) if $document->has_errors;
476  
477 68 $document_cache->{$uri} = $document;
478 }
479  
480 126 ▶ return if not $document;
Branch: 1 branch
BranchTrueFalseTotal
unless $document 23 103 126
481  
482 # bypass the normal collision checks, to avoid an infinite loop: these documents are presumed safe
483 103 $evaluator->_add_resources_unsafe(
484 map +($_->[0] => +{ $_->[1]->%*, document => $document }),
485 $document->resource_pairs
486 );
487  
488 103 return $document;
489 }
490 }
491  
492 ###### media-type mayhem below!
493  
494 {
495 # a hashref that indexes a media-type or media-range string to a decoder, encoder, and
496 # denormalized representation of the string.
497 # prepopulated list must match the list of definitions in _predefined_media_types
498 46 my $MEDIA_TYPES = {
499 map +(join('/', @$_) => { type => $_->[0], subtype => $_->[1] }), (
500 [ qw(application json) ],
501 [ qw(application octet-stream) ],
502 [ qw(text *) ],
503 [ qw(application x-www-form-urlencoded) ],
504 [ qw(application x-ndjson) ],
505 # multipart/form-data and multipart/* are special-cased in OpenAPI::Modern: do not add here
506 )
507 };
508  
509 # see RFC9110 §8.3.1 for ABNF
510 46 my $OWS = q{[\x09\x20]*};
511 46 my $TOKEN = q{(?a:[[:alnum:]!#$%&'*+.^_`|~-]+)};
512 46 my $QUOTED_STRING = q{"((?:[\x09\20\x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\x5C[\x09\x20-\x7E\x80-\xFF])*)"};
513  
514 # parses into hashref: { type => .., subtype => .., parameters => { .. } }
515 322 ▶ sub parse_media_type ($media_type_string) {
Subroutine parse_media_type: called CC=10 SCAR=23.1
Pod documented
516 322 ▶ my ($type, $subtype) = (($media_type_string//'') =~ m{^($TOKEN)/($TOKEN)});
Condition50%1/2
$media_type_string // ""
l!l
3220
MC/DC0%0/1
$media_type_string // ""
$media_type_string
Truth table50%1/2
A
A= $media_type_string
Aresult
11
00
517 322 ▶ return if not defined $type or not defined $subtype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and defined $subtype 8 314 322
Condition66%2/3
defined $type and defined $subtype
!ll&&!rl&&r
80314
MC/DC50%1/2
defined $type and defined $subtype
defined $type defined $subtype
Truth table66%2/3
A and B
A= defined $typeB= defined $subtype
ABresult
0X0
100
111
518 314 pos($media_type_string) = length $&;
519 314 my $leftovers = $';
520  
521 # RFC9110 §5.6.4: "The backslash octet ("\") can be used as a single-octet quoting mechanism
522 # within quoted-string and comment constructs. Recipients that process the value of a
523 # quoted-string MUST handle a quoted-pair as if it were replaced by the octet following the
524 # backslash."
525 # RFC9110 §5.6.6: "Parameter names are case-insensitive. Parameter values might or might not be
526 # case-sensitive, depending on the semantics of the parameter name."
527 # RFC9110 §5.5: "A recipient SHOULD treat other allowed octets in field content (i.e., obs-text)
528 # as opaque data."
529 314 my $params = {};
530 314 ▶ while ($media_type_string =~ /\G$OWS;$OWS($TOKEN)=($TOKEN|$QUOTED_STRING)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($media_type_string =~ /\G$OWS;$OWS($TOKEN)=($TOKEN|$QUOTED_STRING)/gu) 124 314 438
531 124 my ($name, $value, $qs_value) = ($1, $2, $3);
532 124 $leftovers = $';
533 124 ▶ $params->{fc($name)} = defined $qs_value ? ($qs_value =~ s/\x5C(.)/$1/gr) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $qs_value ? : 24 100 124
534 }
535  
536 314 ▶ return if length $leftovers; # junk was left over
Branch: 1 branch
BranchTrueFalseTotal
if length $leftovers 20 294 314
537 294 ▶ croak 'cannot parse more than 64 parameters' if keys $params->%* > 64;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$params > 64 0 294 294
538  
539 # some parameter values are case-insensitive; enumerate them here
540 294 $params->{$_} = fc($params->{$_}) foreach grep exists $params->{$_}, qw(charset);
541  
542 +{
543 294 ▶ type => fc($type),
Branch: 1 branch
BranchTrueFalseTotal
keys %$params ? : 101 193 294
544 subtype => fc($subtype),
545 keys %$params ? (parameters => $params) : (),
546 };
547 }
548  
549 # wrapped in a sub so we don't define them until needed
550 25 ▶ my sub _predefined_media_types ($media_type_string) {
Subroutine _predefined_media_types: called CC=6 SCAR=18.0
551 return +{
552 type => 'application',
553 subtype => 'json',
554 # UTF-8 decoding and encoding is always done, as per the JSON spec.
555 # other charsets are not supported: see RFC8259 §11
556 22 ▶ decode => sub ($content_ref, @) {
Subroutine __ANON__: called CC=1 SCAR=0.0
557 22 \ _JSON_BACKEND->new->allow_nonref(1)->utf8(1)->decode($content_ref->$*);
558 },
559 3 ▶ encode => sub ($content_ref, @) {
Subroutine __ANON__: called CC=1 SCAR=0.0
560 3 \ _JSON_BACKEND->new->allow_nonref(1)->utf8(1)->allow_blessed(1)->convert_blessed(1)->encode($content_ref->$*);
561 },
562 25 ▶ owner_addr => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $media_type_string eq "application/json" 4 21 25
563 }
564 if $media_type_string eq 'application/json';
565  
566 return +{
567 type => 'application',
568 subtype => 'octet-stream',
569 0 ▶ (map +($_ => sub ($content_ref, @) { $content_ref }), qw(decode encode)),
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $media_type_string eq "application/octet-stream" 0 21 21
570 owner_addr => 1,
571 }
572 if $media_type_string eq 'application/octet-stream';
573  
574 # identity function, with charset support
575 return +{
576 5 type => 'text', subtype => '*',
577 5 ▶ decode => sub ($content_ref, $parameters = {}, @) {
Subroutine __ANON__: called CC=3 SCAR=11.0
578 # RFC2046 §4.1.2: charset is case-insensitive
579 # RFC6657 §4: default charset for text/plain is "US-ASCII"
580 return $parameters->{charset} ?
581 5 ▶ \ Encode::decode($parameters->{charset}, $content_ref->$*, Encode::DIE_ON_ERR | Encode::LEAVE_SRC)
Branch: 1 branch
BranchTrueFalseTotal
$$parameters{'charset'} ? : 2 3 5
582 : $content_ref;
583 },
584 2 ▶ encode => sub ($content_ref, $parameters = {}, @) {
Subroutine __ANON__: called CC=3 SCAR=11.4
585 return $parameters->{charset} ?
586 2 ▶ \ Encode::encode($parameters->{charset}, $content_ref->$*, Encode::DIE_ON_ERR | Encode::LEAVE_SRC)
Branch: 1 branch
BranchTrueFalseTotal
$$parameters{'charset'} ? : 2 0 2
587 : $content_ref;
588 },
589 21 ▶ owner_addr => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $media_type_string eq "text/*" 2 19 21
590 }
591 if $media_type_string eq 'text/*';
592  
593 return +{
594 4 type => 'application',
595 subtype => 'x-www-form-urlencoded',
596 4 ▶ decode => sub ($content_ref, $parameters = {}) {
Subroutine __ANON__: called CC=6 SCAR=18.5
597 4 my $parsed = Mojo::Parameters->new->charset('UTF-8')->parse($content_ref->$*);
598  
599 4 ▶ if (($parameters->{type}//'object') eq 'array') {
Branch: 2 branches
BranchTrueFalseTotal
if (($$parameters{'type'} // 'object') eq 'array') { } 1 3 4
elsif (($$parameters{'type'} // 'object') eq 'object') { } 3 0 3
Condition100%2/2
$$parameters{"type"} // "object"
l!l
13
MC/DC100%1/1
$$parameters{"type"} // "object"
$$parameters{"type"}
Truth table100%2/2
A
A= $$parameters{"type"}
Aresult
11
00
600 1 ▶ \ [ pairmap { +{ $a, $b } } $parsed->pairs->@* ];
Subroutine __ANON__: called CC=1 SCAR=0.0
601 }
602 ▶ elsif (($parameters->{type}//'object') eq 'object') {
Condition50%1/2
$$parameters{"type"} // "object"
l!l
03
MC/DC0%0/1
$$parameters{"type"} // "object"
$$parameters{"type"}
Truth table50%1/2
A
A= $$parameters{"type"}
Aresult
11
00
603 3 \ $parsed->to_hash;
604 }
605 else {
606 0 die 'unrecognized "type": ', $parameters->{type};
607 }
608 },
609 6 ▶ encode => sub ($content_ref, @) {
Subroutine __ANON__: called CC=4 SCAR=14.0
610 # WHATWG §1.3: "The application/x-www-form-urlencoded percent-encode set contains all code
611 # points, except the ASCII alphanumeric, U+002A (*), U+002D (-), U+002E (.), and U+005F
612 # (_)."
613 787 ▶ state sub escape ($str) {
Subroutine escape: called CC=1 SCAR=0.0
614 787 url_escape(Encode::encode('UTF-8', $str, Encode::DIE_ON_ERR), '^A-Za-z0-9*\-._');
615 }
616  
617 6 ▶ if (ref $content_ref->$* eq 'ARRAY') { # arrayref of hashref tuples
Branch: 2 branches
BranchTrueFalseTotal
if (ref $$content_ref eq 'ARRAY') { } 2 4 6
elsif (ref $$content_ref eq 'HASH') { } 4 0 4
618 2 \ join '&', map +(join '=', (map +(escape $_), %$_)), $content_ref->$*->@*;
619 }
620 elsif (ref $content_ref->$* eq 'HASH') { # hashref of strings or arrayref of strings
621 \ join '&',
622 4 map do {
623 259 my ($k, $ek) = ($_, escape($_));
624 ref $content_ref->$*->{$k} eq 'ARRAY'
625 ? (map +($ek.'='.escape($_)), $content_ref->$*->{$k}->@*)
626 259 ▶ : $ek.'='.escape($content_ref->$*->{$k})
Branch: 1 branch
BranchTrueFalseTotal
? : 2 257 259
627 },
628 sort keys $content_ref->$*->%*;
629 }
630 else {
631 0 die 'unrecognized data type: ', ref $content_ref->$*;
632 }
633 },
634 19 ▶ owner_addr => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $media_type_string eq "application/x-www-form-urlencoded" 2 17 19
635 }
636 if $media_type_string eq 'application/x-www-form-urlencoded';
637  
638 return +{
639 type => 'application',
640 subtype => 'x-ndjson',
641 3 ▶ decode => sub ($content_ref, @) {
Subroutine __ANON__: called CC=1 SCAR=0.0
642 3 my $decoder = _JSON_BACKEND->new->allow_nonref(1)->utf8(1);
643 3 my $line = 0; # line numbers start at 1
644 3 \[ map do {
645 7 try { ++$line; $decoder->decode($_) }
646 1 catch ($e) { croak 'parse error at line '.$line.': '.$e }
647 },
648 split(/\r?\n/, $content_ref->$*)
649 ];
650 },
651 1 ▶ encode => sub ($content_ref, @) {
Subroutine __ANON__: called CC=1 SCAR=0.0
652 1 my $encoder = _JSON_BACKEND->new->allow_nonref(1)->utf8(1)->allow_blessed(1)->convert_blessed(1);
653 1 \ join "\n", map $encoder->encode($_), $content_ref->$*->@*;
654 },
655 17 ▶ owner_addr => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $media_type_string eq "application/x-ndjson" 2 15 17
656 }
657 if $media_type_string eq 'application/x-ndjson';
658 }
659  
660 # for internal use by JSON::Schema::Modern only! may be removed without notice!
661 24 ▶ sub _get_media_type_decoder ($media_type_string) {
Subroutine _get_media_type_decoder: called CC=3 SCAR=11.0
662 24 my $matched_string = match_media_type($media_type_string);
663 24 ▶ return undef if not defined $matched_string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matched_string 4 20 24
664  
665 20 my $definition = $MEDIA_TYPES->{$matched_string};
666 $definition = $MEDIA_TYPES->{$matched_string} = _predefined_media_types($matched_string)
667 20 ▶ if not exists $definition->{decode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$definition{"decode"} 5 15 20
668  
669 20 return $definition->{decode};
670 }
671  
672 17 ▶ sub add_media_type ($media_type_string, $decoder_sub = undef, $encoder_sub = undef, $owner_addr = 1) {
Subroutine add_media_type: called CC=13 SCAR=27.5
Pod documented
673 17 ▶ croak 'decoder is not a subref' if defined $decoder_sub and ref $decoder_sub ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $decoder_sub and ref $decoder_sub ne "CODE" 0 17 17
Condition66%2/3
defined $decoder_sub and ref $decoder_sub ne "CODE"
!ll&&!rl&&r
1070
MC/DC0%0/2
defined $decoder_sub and ref $decoder_sub ne "CODE"
defined $decoder_sub ref $decoder_sub ne "CODE"
Truth table66%2/3
A and B
A= defined $decoder_subB= ref $decoder_sub ne "CODE"
ABresult
0X0
100
111
674 17 ▶ croak 'encoder is not a subref' if defined $encoder_sub and ref $encoder_sub ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoder_sub and ref $encoder_sub ne "CODE" 0 17 17
Condition33%1/3
defined $encoder_sub and ref $encoder_sub ne "CODE"
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $encoder_sub and ref $encoder_sub ne "CODE"
defined $encoder_sub ref $encoder_sub ne "CODE"
Truth table33%1/3
A and B
A= defined $encoder_subB= ref $encoder_sub ne "CODE"
ABresult
0X0
100
111
675  
676 17 my $type = parse_media_type($media_type_string);
677 17 ▶ croak "bad media-type string \"$media_type_string\"" if not $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 16 17
678  
679 16 ▶ croak 'multipart encoders/decoders cannot be defined here: use OpenAPI::Modern'
Branch: 1 branch
BranchTrueFalseTotal
if match_media_type($media_type_string, ["multipart/*"]) 1 15 16
680 if match_media_type($media_type_string, ['multipart/*']);
681  
682 # populate the cache if it's a bundled type that hasn't been defined yet
683 15 ▶ _predefined_media_types($media_type_string) if not exists $MEDIA_TYPES->{$media_type_string};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$MEDIA_TYPES{$media_type_string} 15 0 15
684  
685 15 ▶ if (any { is_equal($type, { $_->%{qw(type subtype parameters)} }) } values %$MEDIA_TYPES) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::any {is_equal($type, {%our $_{"type", "subtype", "parameters"}});} values %$MEDIA_TYPES) 1 14 15
686 1 ▶ croak 'duplicate media-type found' if $owner_addr == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $owner_addr == 1 1 0 1
687  
688 # track evaluator object that used the deprecated add_media_type interface
689 0 push $MEDIA_TYPES->{$media_type_string}{owner_addr}->@*, $owner_addr;
690 0 return;
691 }
692  
693 14 $MEDIA_TYPES->{$media_type_string} = {
694 decode => $decoder_sub,
695 encode => $encoder_sub,
696 %$type,
697 owner_addr => [ $owner_addr ], # refaddr of the evaluator object that added us
698 };
699  
700 14 return;
701 }
702  
703 7 ▶ sub delete_media_type ($media_type_string, $owner_addr = 1) {
Subroutine delete_media_type: called CC=6 SCAR=18.8
Pod documented
704 7 ▶ return if not exists $MEDIA_TYPES->{$media_type_string};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$MEDIA_TYPES{$media_type_string} 2 5 7
705  
706 delete $MEDIA_TYPES->{$media_type_string}
707 if $owner_addr == 1
708 or $MEDIA_TYPES->{$media_type_string}{owner_addr} = [
709 grep +($_ != 1 && $_ != $owner_addr), $MEDIA_TYPES->{$media_type_string}{owner_addr}->@*
710 5 ▶ ];
Branch: 1 branch
BranchTrueFalseTotal
if $owner_addr == 1 or $$MEDIA_TYPES{$media_type_string}{"owner_addr"} = [grep(($_ != 1 && $_ != $owner_addr), @{$$MEDIA_TYPES{$media_type_string}{"owner_addr"}})] 5 0 5
Condition60%3/5
$owner_addr == 1 or $$MEDIA_TYPES{$media_type_string}{"owner_addr"} = [grep(($_ != 1 && $_ != $owner_addr), @{$$MEDIA_TYPES{$media_type_string}{"owner_addr"}})]
l!l
32
$_ != 1 && $_ != $owner_addr
!ll&&!rl&&r
020
MC/DC100%1/1
$owner_addr == 1 or $$MEDIA_TYPES{$media_type_string}{"owner_addr"} = [grep(($_ != 1 && $_ != $owner_addr), @{$$MEDIA_TYPES{$media_type_string}{"owner_addr"}})]
$owner_addr == 1
MC/DC0%0/2
$_ != 1 && $_ != $owner_addr
$_ != 1 $_ != $owner_addr
Truth table100%2/2
A
A= $owner_addr == 1
Aresult
11
00
Truth table33%1/3
A && B
A= $_ != 1B= $_ != $owner_addr
ABresult
0X0
100
111
711 }
712  
713 # wildcards, parameters supported
714 # always returns a reference to the decoded data, or undef if no decoder is found
715 31 ▶ sub decode_media_type ($media_type_string, $content_ref) {
Subroutine decode_media_type: called CC=7 SCAR=19.9
Pod documented
716 31 ▶ croak 'decoder payload must be a reference to a string (not ',
Branch: 2 branches
BranchTrueFalseTotal
if ref $content_ref ne "SCALAR" 0 31 31
length ref $content_ref ? : 0 0 0
717 (length ref $content_ref ? ref $content_ref : 'a non-reference')
718 if ref $content_ref ne 'SCALAR';
719  
720 31 my $matched_string = match_media_type($media_type_string);
721 31 ▶ return if not $matched_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched_string 2 29 31
722  
723 29 my $definition = $MEDIA_TYPES->{$matched_string};
724 $definition = $MEDIA_TYPES->{$matched_string} = _predefined_media_types($matched_string)
725 29 ▶ if not exists $definition->{decode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$definition{"decode"} 5 24 29
726  
727 29 ▶ return if not $definition->{decode};
Branch: 1 branch
BranchTrueFalseTotal
unless $$definition{"decode"} 1 28 29
728  
729 28 my $type = parse_media_type($media_type_string);
730 28 ▶ $definition->{decode}->($content_ref, $type->{parameters}//());
Condition66%2/3
$$type{"parameters"} // ()
l!l&&r!l&&!r
4240
MC/DC0%0/2
$$type{"parameters"} // ()
$$type{"parameters"} ()
Truth table66%2/3
A // B
A= $$type{"parameters"}B= ()
ABresult
1X1
011
000
731 }
732  
733 # wildcards, parameters supported
734 13 ▶ sub encode_media_type ($media_type_string, $content_ref) {
Subroutine encode_media_type: called CC=7 SCAR=20.2
Pod documented
735 13 ▶ croak 'encoder payload must be a reference' if ref $content_ref ne 'REF' and ref $content_ref ne 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $content_ref ne "REF" and ref $content_ref ne "SCALAR" 0 13 13
Condition66%2/3
ref $content_ref ne "REF" and ref $content_ref ne "SCALAR"
!ll&&!rl&&r
1030
MC/DC0%0/2
ref $content_ref ne "REF" and ref $content_ref ne "SCALAR"
ref $content_ref ne "REF" ref $content_ref ne "SCALAR"
Truth table66%2/3
A and B
A= ref $content_ref ne "REF"B= ref $content_ref ne "SCALAR"
ABresult
0X0
100
111
736  
737 13 my $matched_string = match_media_type($media_type_string);
738 13 ▶ return if not $matched_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched_string 0 13 13
739  
740 13 my $definition = $MEDIA_TYPES->{$matched_string};
741 $definition = $MEDIA_TYPES->{$matched_string} = _predefined_media_types($matched_string)
742 13 ▶ if not exists $definition->{encode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$definition{"encode"} 0 13 13
743  
744 13 ▶ return if not $definition->{encode};
Branch: 1 branch
BranchTrueFalseTotal
unless $$definition{"encode"} 1 12 13
745  
746 12 my $type = parse_media_type($media_type_string);
747 12 ▶ $definition->{encode}->($content_ref, $type->{parameters}//());
Condition66%2/3
$$type{"parameters"} // ()
l!l&&r!l&&!r
390
MC/DC0%0/2
$$type{"parameters"} // ()
$$type{"parameters"} ()
Truth table66%2/3
A // B
A= $$type{"parameters"}B= ()
ABresult
1X1
011
000
748 }
749  
750 # finds best match for a media-type against a list of media-types. if parameter(s) are included in
751 # the media-type to be matched, all parameters must be present in the match value.
752 133 ▶ sub match_media_type ($media_type_string, $media_types = []) {
Subroutine match_media_type: called CC=23 SCAR=31.4
Pod documented
753 133 ▶ return if not length $media_type_string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $media_type_string 0 133 133
754  
755 # return immediately if exact match exists
756 return $media_type_string
757 241 ▶ if @$media_types and any { $_ eq $media_type_string } @$media_types
Condition100%3/3
not @$media_types and exists $$MEDIA_TYPES{$media_type_string}
!ll&&!rl&&r
385142
MC/DC100%2/2
not @$media_types and exists $$MEDIA_TYPES{$media_type_string}
not @$media_types exists $$MEDIA_TYPES{$media_type_string}
Truth table100%3/3
not(A) and B
A= not @$media_typesB= exists $$MEDIA_TYPES{$media_type_string}
ABresult
0X0
100
111
758 133 ▶ or not @$media_types and exists $MEDIA_TYPES->{$media_type_string};
Branch: 1 branch
BranchTrueFalseTotal
if @$media_types and CORE::any {$_ eq $media_type_string;} @$media_types or not @$media_types and exists $$MEDIA_TYPES{$media_type_string} 44 89 133
Condition100%6/6
@$media_types and CORE::any {$_ eq $media_type_string;} @$media_types or not @$media_types and exists $$MEDIA_TYPES{$media_type_string}
l!l&&r!l&&!r
24289
@$media_types and CORE::any {$_ eq $media_type_string;} @$media_types
!ll&&!rl&&r
93382
MC/DC33%1/3
@$media_types and CORE::any {$_ eq $media_type_string;} @$media_types or not @$media_types and exists $$MEDIA_TYPES{$media_type_string}
@$media_types CORE::any {$_ eq $media_type_string;} @$media_types not @$media_types and exists $$MEDIA_TYPES{$media_type_string}
Truth table60%3/5
A and B or C
A= @$media_typesB= CORE::any {$_ eq $media_type_string;} @$media_typesC= not @$media_types and exists $$MEDIA_TYPES{$media_type_string}
ABCresult
11X1
0X11
1011
0X00
1000
759  
760 89 my $mt = parse_media_type($media_type_string);
761 89 ▶ return if not $mt;
Branch: 1 branch
BranchTrueFalseTotal
unless $mt 17 72 89
762  
763 72 ▶ my $types = @$media_types ? +{ map +($_ => parse_media_type($_)), @$media_types } : $MEDIA_TYPES;
Branch: 1 branch
BranchTrueFalseTotal
@$media_types ? : 30 42 72
764  
765 72 my @matches; # [ rank, candidate ]
766  
767 # iterate through each provided MT and compare it to the string for matchability..
768 CANDIDATE:
769 72 foreach my $candidate (keys %$types) {
770 # if candidate has parameters, all parameters must match; missing parameters ok.
771 # the more parameters match the higher the score.
772 628 my $params_matched = 0;
773 628 ▶ foreach my $param (keys(($types->{$candidate}{parameters}//{})->%*)) {
Condition100%2/2
$$types{$candidate}{"parameters"} // {}
l!l
148480
MC/DC100%1/1
$$types{$candidate}{"parameters"} // {}
$$types{$candidate}{"parameters"}
Truth table100%2/2
A
A= $$types{$candidate}{"parameters"}
Aresult
11
00
774 next CANDIDATE if not exists(($mt->{parameters}//{})->{$param})
775 152 ▶ or $types->{$candidate}{parameters}{$param} ne $mt->{parameters}{$param};
Branch: 1 branch
BranchTrueFalseTotal
if not exists(($$mt{"parameters"} // {})->{$param}) or $$types{$candidate}{"parameters"}{$param} ne $$mt{"parameters"}{$param} 132 20 152
Condition100%5/5
not exists(($$mt{"parameters"} // {})->{$param}) or $$types{$candidate}{"parameters"}{$param} ne $$mt{"parameters"}{$param}
l!l&&r!l&&!r
1082420
$$mt{"parameters"} // {}
l!l
9656
MC/DC100%2/2
not exists(($$mt{"parameters"} // {})->{$param}) or $$types{$candidate}{"parameters"}{$param} ne $$mt{"parameters"}{$param}
$$mt{"parameters"} $$types{$candidate}{"parameters"}{$param} ne $$mt{"parameters"}{$param}
Truth table100%3/3
not(A) or B
A= $$mt{"parameters"}B= $$types{$candidate}{"parameters"}{$param} ne $$mt{"parameters"}{$param}
ABresult
0X1
111
100
776  
777 20 ++$params_matched;
778 }
779  
780 496 ▶ push(@matches, [ 0+$params_matched, $candidate ]), next if $candidate eq '*/*';
Branch: 1 branch
BranchTrueFalseTotal
if $candidate eq "*/*" 34 462 496
781 push(@matches, [ 2**8 + $params_matched, $candidate ]), next
782 if $types->{$candidate}{subtype} eq '*'
783 462 ▶ and $types->{$candidate}{type} eq $mt->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $$types{$candidate}{"subtype"} eq "*" and $$types{$candidate}{"type"} eq $$mt{"type"} 36 426 462
Condition100%3/3
$$types{$candidate}{"subtype"} eq "*" and $$types{$candidate}{"type"} eq $$mt{"type"}
!ll&&!rl&&r
3438336
MC/DC100%2/2
$$types{$candidate}{"subtype"} eq "*" and $$types{$candidate}{"type"} eq $$mt{"type"}
$$types{$candidate}{"subtype"} eq "*" $$types{$candidate}{"type"} eq $$mt{"type"}
Truth table100%3/3
A and B
A= $$types{$candidate}{"subtype"} eq "*"B= $$types{$candidate}{"type"} eq $$mt{"type"}
ABresult
0X0
100
111
784  
785 # exact type + subtype match: best overall
786 426 ▶ if ($types->{$candidate}{type} eq $mt->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$types{$candidate}{"type"} eq $$mt{"type"}) 90 336 426
787 push(@matches, [ 2**10 + $params_matched, $candidate ]), next
788 90 ▶ if $types->{$candidate}{subtype} eq $mt->{subtype};
Branch: 1 branch
BranchTrueFalseTotal
if $$types{$candidate}{"subtype"} eq $$mt{"subtype"} 31 59 90
789  
790 # text/foo+plain matches text/plain but not text/bar+plain
791 push(@matches, [ 2**9 + $params_matched, $candidate ]), next
792 59 ▶ if $mt->{subtype} =~ m{^.+\+(.+)\z} and $types->{$candidate}{subtype} eq $1;
Branch: 1 branch
BranchTrueFalseTotal
if $$mt{"subtype"} =~ /^.+\+(.+)\z/u and $$types{$candidate}{"subtype"} eq $1 2 57 59
Condition100%3/3
$$mt{"subtype"} =~ /^.+\+(.+)\z/u and $$types{$candidate}{"subtype"} eq $1
!ll&&!rl&&r
5342
MC/DC100%2/2
$$mt{"subtype"} =~ /^.+\+(.+)\z/u and $$types{$candidate}{"subtype"} eq $1
$$mt{"subtype"} =~ /^.+\+(.+)\z/u $$types{$candidate}{"subtype"} eq $1
Truth table100%3/3
A and B
A= $$mt{"subtype"} =~ /^.+\+(.+)\z/uB= $$types{$candidate}{"subtype"} eq $1
ABresult
0X0
100
111
793 }
794 }
795  
796 72 ▶ return if not @matches;
Branch: 1 branch
BranchTrueFalseTotal
unless @matches 21 51 72
797 51 my @sorted = sort { $b->[0] <=> $a->[0] } @matches;
798 51 return $sorted[0]->[1];
799 }
800 }
801  
802 ######## NO PUBLIC INTERFACES FOLLOW THIS POINT ########
803  
804 # get all annotations produced for the current instance data location (that are visible to this
805 # schema location) - remember these are hashrefs, not Annotation objects
806 46 ▶sub local_annotations ($state) {
Subroutine local_annotations: called CC=1 SCAR=0.0
Pod undocumented
807 1416 grep $_->{instance_location} eq $state->{data_path}, $state->{annotations}->@*;
808 }
809  
810 # shorthand for finding the current uri of the present schema location
811 # ensure that this code is kept consistent with the absolute_keyword_location builder in ResultNode
812 # Note that this may not be canonical if keyword_path has not yet been reset via the processing of a
813 # local identifier keyword (e.g. '$id').
814 34198 ▶sub canonical_uri ($state, @extra_path) {
Subroutine canonical_uri: called CC=6 SCAR=18.2
Pod undocumented
815 34198 ▶ return $state->{initial_schema_uri} if not @extra_path and not length($state->{keyword_path});
Branch: 1 branch
BranchTrueFalseTotal
unless @extra_path or length $$state{"keyword_path"} 21338 12860 34198
Condition66%2/3
@extra_path or length $$state{"keyword_path"}
l!l&&r!l&&!r
01286021338
MC/DC50%1/2
@extra_path or length $$state{"keyword_path"}
@extra_path length $$state{"keyword_path"}
Truth table66%2/3
A or B
A= @extra_pathB= length $$state{"keyword_path"}
ABresult
1X1
011
000
816 12860 my $uri = $state->{initial_schema_uri}->clone;
817 12860 ▶ my $fragment = ($uri->fragment//'').(@extra_path ? jsonp($state->{keyword_path}, @extra_path) : $state->{keyword_path});
Branch: 1 branch
BranchTrueFalseTotal
@extra_path ? : 0 12860 12860
Condition100%2/2
$uri->fragment // ""
l!l
115011710
MC/DC100%1/1
$uri->fragment // ""
$uri->fragment
Truth table100%2/2
A
A= $uri->fragment
Aresult
11
00
818 12860 ▶ undef $fragment if not length($fragment);
Branch: 1 branch
BranchTrueFalseTotal
unless length $fragment 0 12860 12860
819 12860 $uri->fragment($fragment);
820 12860 $uri;
821 }
822  
823 # shorthand for creating error objects
824 # uses these keys from $state:
825 # - initial_schema_uri
826 # - keyword (optional)
827 # - data_path
828 # - traversed_keyword_path
829 # - keyword_path
830 # - _keyword_path_suffix (optional)
831 # - errors
832 # - exception (optional; set by abort())
833 # - recommended_response (optional)
834 # - depth
835 # - traverse (boolean, used for mode)
836 # returns defined-false, so callers can use 'return;' to differentiate between
837 # failed-with-no-error from failed-with-error.
838 14110 ▶sub E ($state, $error_string, @args) {
Subroutine E: called CC=11 SCAR=24.1
Pod undocumented
839 14110 ▶ croak 'E called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 14110 14110
840  
841 # sometimes the keyword shouldn't be at the very end of the schema path
842 14110 my $sps = delete $state->{_keyword_path_suffix};
843 14110 ▶ my @keyword_path_suffix = defined $sps && ref $sps eq 'ARRAY' ? $sps->@* : $sps//();
Branch: 1 branch
BranchTrueFalseTotal
defined $sps && ref $sps eq 'ARRAY' ? : 1 14109 14110
Condition100%6/6
defined $sps and ref $sps eq "ARRAY"
!ll&&!rl&&r
136045051
$sps // ()
l!l&&r!l&&!r
505285810746
MC/DC100%2/2
defined $sps and ref $sps eq "ARRAY"
defined $sps ref $sps eq "ARRAY"
MC/DC100%2/2
$sps // ()
$sps ()
Truth table100%3/3
A and B
A= defined $spsB= ref $sps eq "ARRAY"
ABresult
0X0
100
111
Truth table100%3/3
A // B
A= $spsB= ()
ABresult
1X1
011
000
844  
845 # we store the absolute uri in unresolved form until needed,
846 # and perform the rest of the calculations later.
847 14110 ▶ my $uri = [ $state->@{qw(initial_schema_uri keyword_path)}, $state->{keyword}//(), @keyword_path_suffix ];
Condition66%2/3
$$state{"keyword"} // ()
l!l&&r!l&&!r
136184920
MC/DC0%0/2
$$state{"keyword"} // ()
$$state{"keyword"} ()
Truth table66%2/3
A // B
A= $$state{"keyword"}B= ()
ABresult
1X1
011
000
848  
849 my $keyword_location = $state->{traversed_keyword_path}
850 14110 ▶ .jsonp($state->{keyword_path}, $state->{keyword}//(), @keyword_path_suffix);
Condition66%2/3
$$state{"keyword"} // ()
l!l&&r!l&&!r
136184920
MC/DC0%0/2
$$state{"keyword"} // ()
$$state{"keyword"} ()
Truth table66%2/3
A // B
A= $$state{"keyword"}B= ()
ABresult
1X1
011
000
851  
852 14110 require JSON::Schema::Modern::Error;
853 push $state->{errors}->@*, JSON::Schema::Modern::Error->new(
854 depth => $state->{depth} // 0,
855 keyword => $state->{keyword},
856 $state->{traverse} ? () : (instance_location => $state->{data_path}),
857 keyword_location => $keyword_location,
858 # we calculate absolute_keyword_location when instantiating the Error object for Result
859 _uri => $uri,
860 error => @args ? sprintf($error_string, @args) : $error_string,
861 exception => $state->{exception},
862 ($state->%{recommended_response})x!!$state->{recommended_response},
863 14110 ▶ mode => $state->{traverse} ? 'traverse' : 'evaluate',
Branch: 3 branches
BranchTrueFalseTotal
$$state{'traverse'} ? : 229 13881 14110
@args ? : 7142 6968 14110
$$state{'traverse'} ? : 229 13881 14110
Condition100%2/2
$$state{"depth"} // 0
l!l
141082
MC/DC100%1/1
$$state{"depth"} // 0
$$state{"depth"}
Truth table100%2/2
A
A= $$state{"depth"}
Aresult
11
00
864 );
865  
866 14110 return 0;
867 }
868  
869 # shorthand for creating annotations
870 # uses these keys from $state:
871 # - initial_schema_uri
872 # - keyword (mandatory)
873 # - data_path
874 # - traversed_keyword_path
875 # - keyword_path
876 # - annotations
877 # - collect_annotations
878 # - _unknown (boolean)
879 # - depth
880 12144 ▶sub A ($state, $annotation) {
Subroutine A: called CC=5 SCAR=16.1
Pod undocumented
881 # even if the user requested annotations, we only collect them for later drafts
882 # ..but we always collect them if the lowest bit is set, indicating the presence of unevaluated*
883 # keywords necessary for accurate validation
884 return 1 if not ($state->{collect_annotations}
885 12144 ▶ & ($state->{specification_version} =~ /^draft[467]\z/ ? ~(1<<8) : ~0));
Branch: 2 branches
BranchTrueFalseTotal
unless $$state{"collect_annotations"} & ($$state{"specification_version"} =~ /^draft[467]\z/u ? '18446744073709551359' : '18446744073709551615') 9452 2692 12144
$$state{'specification_version'} =~ /^draft[467]\z/u ? : 3686 8458 12144
886  
887 # we store the absolute uri in unresolved form until needed,
888 # and perform the rest of the calculations later.
889 2692 my $uri = [ $state->@{qw(initial_schema_uri keyword_path keyword)} ];
890  
891 2692 my $keyword_location = $state->{traversed_keyword_path}.jsonp($state->@{qw(keyword_path keyword)});
892  
893 push $state->{annotations}->@*, {
894 depth => $state->{depth} // 0,
895 keyword => $state->{keyword},
896 instance_location => $state->{data_path},
897 keyword_location => $keyword_location,
898 # we calculate absolute_keyword_location when instantiating the Annotation object for Result
899 _uri => $uri,
900 annotation => $annotation,
901 2692 ▶ $state->{_unknown} ? (unknown => 1) : (),
Branch: 1 branch
BranchTrueFalseTotal
$$state{'_unknown'} ? : 37 2655 2692
Condition50%1/2
$$state{"depth"} // 0
l!l
26920
MC/DC0%0/1
$$state{"depth"} // 0
$$state{"depth"}
Truth table50%1/2
A
A= $$state{"depth"}
Aresult
11
00
902 };
903  
904 2692 return 1;
905 }
906  
907 # creates an error object, but also aborts evaluation immediately
908 # only this error is returned, because other errors on the stack might not actually be "real"
909 # errors (consider if we were in the middle of evaluating a "not" or "if").
910 # Therefore this is only appropriate during the evaluation phase, not the traverse phase.
911 34 ▶sub abort ($state, $error_string, @args) {
Subroutine abort: called CC=2 SCAR=7.1
Pod undocumented
912 34 ()= E({ %$state, exception => 1 }, $error_string, @args);
913 34 ▶ croak 'abort() called during traverse' if $state->{traverse};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"traverse"} 0 34 34
914 34 die pop $state->{errors}->@*;
915 }
916  
917 0 ▶sub assert_keyword_exists ($state, $schema) {
Subroutine assert_keyword_exists: not called CC=3 SCAR=24.8
Pod undocumented
918 0 ▶ croak 'assert_keyword_exists called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
919 0 ▶ return E($state, '%s keyword is required', $state->{keyword}) if not exists $schema->{$state->{keyword}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$$state{"keyword"}} 0 0 0
920 0 return 1;
921 }
922  
923 42570 ▶sub assert_keyword_type ($state, $schema, $type) {
Subroutine assert_keyword_type: called CC=4 SCAR=13.9
Pod undocumented
924 42570 ▶ croak 'assert_keyword_type called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 42570 42570
925 42570 ▶ return 1 if is_type($type, $schema->{$state->{keyword}});
Branch: 1 branch
BranchTrueFalseTotal
if is_type($type, $$schema{$$state{"keyword"}}) 42553 17 42570
926 17 ▶ E($state, '%s value is not a%s %s', $state->{keyword}, ($type =~ /^[aeiou]/ ? 'n' : ''), $type);
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^[aeiou]/u ? : 3 14 17
927 }
928  
929 2902 ▶sub assert_pattern ($state, $pattern) {
Subroutine assert_pattern: called CC=2 SCAR=7.1
Pod undocumented
930 2902 ▶ croak 'assert_pattern called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 2902 2902
931 2902 try {
932 0 ▶ local $SIG{__WARN__} = sub { die @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
933 2902 qr/$pattern/;
934 }
935 4 catch ($e) { return E($state, $e); };
936 2898 return 1;
937 }
938  
939 # this is only suitable for checking URIs within schemas themselves
940 # note that we cannot use $state->{specification_version} to more tightly constrain the plain-name
941 # fragment syntax, as we could be checking a $ref to a schema using a different version
942 5849 ▶sub assert_uri_reference ($state, $schema) {
Subroutine assert_uri_reference: called CC=8 SCAR=20.8
Pod undocumented
943 5849 ▶ croak 'assert_uri_reference called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 5849 5849
944  
945 5849 my $string = $schema->{$state->{keyword}};
946 return E($state, '%s value is not a valid URI-reference', $state->{keyword})
947 # see also uri-reference format sub
948 5849 ▶ if fc(Mojo::URL->new($string)->to_unsafe_string) ne fc($string)
Branch: 1 branch
BranchTrueFalseTotal
if fc "Mojo::URL"->new($string)->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or $string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u 34 5815 5849
Condition93%14/15
fc "Mojo::URL"->new($string)->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or $string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u
l!l&&r!l&&!r
8265815
fc "Mojo::URL"->new($string)->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u
l!l&&r!l&&!r
805841
$string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u
!ll&&!rl&&r
3762205326
$string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/u
!ll&&!rl&&r
32545082079
$string =~ /#/u and not $string =~ /#\z/u
!ll&&!rl&&r
28504042587
MC/DC83%5/6
fc "Mojo::URL"->new($string)->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or $string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u
fc "Mojo::URL"->new($string)->to_unsafe_string ne fc $string $string =~ /[^[:ascii:]]/u $string =~ /#/u not $string =~ /#\z/u not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/u not $string =~ m[#/(?:[^~]|~[01])*\z]u
Truth table85%6/7
A or B or C and not(D) and not(E) and not(F)
A= fc "Mojo::URL"->new($string)->to_unsafe_string ne fc $stringB= $string =~ /[^[:ascii:]]/uC= $string =~ /#/uD= not $string =~ /#\z/uE= not $string =~ /#[A-Za-z_][A-Za-z0-9_:.-]*\z/uF= not $string =~ m[#/(?:[^~]|~[01])*\z]u
ABCDEFresult
1XXXXX1
01XXXX1
0011111
000XXX0
0010XX0
00110X0
0011100
949 or $string =~ /[^[:ascii:]]/ # ascii characters only
950 or $string =~ /#/ # no fragment, except...
951 and $string !~ m{#\z} # allow empty fragment
952 and $string !~ m{#[A-Za-z_][A-Za-z0-9_:.-]*\z} # allow plain-name fragment, superset of all drafts
953 and $string !~ m{#/(?:[^~]|~[01])*\z}; # allow json pointer fragment
954  
955 5815 return 1;
956 }
957  
958 # this is only suitable for checking URIs within schemas themselves,
959 # which have fragments consisting of plain names (anchors) or json pointers
960 6137 ▶sub assert_uri ($state, $schema, $override = undef) {
Subroutine assert_uri: called CC=11 SCAR=25.2
Pod undocumented
961 6137 ▶ croak 'assert_uri called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 6137 6137
962  
963 6137 ▶ my $string = $override // $schema->{$state->{keyword}};
Condition33%1/3
$override // $$schema{$$state{"keyword"}}
l!l&&r!l&&!r
613700
MC/DC0%0/2
$override // $$schema{$$state{"keyword"}}
$override $$schema{$$state{"keyword"}}
Truth table33%1/3
A // B
A= $overrideB= $$schema{$$state{"keyword"}}
ABresult
1X1
011
000
964 6137 my $uri = Mojo::URL->new($string);
965  
966 6137 ▶ return E($state, '"%s" is not a valid URI', $string)
Branch: 1 branch
BranchTrueFalseTotal
if fc $uri->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or not $uri->is_abs or $string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u 16 6121 6137
Condition77%14/18
fc $uri->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or not $uri->is_abs or $string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u
l!l&&r!l&&!r
1606121
fc $uri->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or not $uri->is_abs
l!l&&r!l&&!r
886121
fc $uri->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u
l!l&&r!l&&!r
806129
$string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u
!ll&&!rl&&r
612010
$string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/u
!ll&&!rl&&r
612001
$string =~ /#/u and not $string =~ /#\z/u
!ll&&!rl&&r
6078421
MC/DC28%2/7
fc $uri->to_unsafe_string ne fc $string or $string =~ /[^[:ascii:]]/u or not $uri->is_abs or $string =~ /#/u and not $string =~ /#\z/u and not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/u and not $string =~ m[#/(?:[^~]|~[01])*\z]u
fc $uri->to_unsafe_string ne fc $string $string =~ /[^[:ascii:]]/u not $uri->is_abs $string =~ /#/u not $string =~ /#\z/u not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/u not $string =~ m[#/(?:[^~]|~[01])*\z]u
Truth table62%5/8
A or B or not(C) or D and not(E) and not(F) and not(G)
A= fc $uri->to_unsafe_string ne fc $stringB= $string =~ /[^[:ascii:]]/uC= not $uri->is_absD= $string =~ /#/uE= not $string =~ /#\z/uF= not $string =~ /#[A-Za-z][A-Za-z0-9_:.-]*\z/uG= not $string =~ m[#/(?:[^~]|~[01])*\z]u
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
00011111
0000XXX0
00010XX0
000110X0
00011100
967 # see also uri format sub
968 if fc($uri->to_unsafe_string) ne fc($string)
969 or $string =~ /[^[:ascii:]]/ # ascii characters only
970 or not $uri->is_abs # must have a scheme
971 or $string =~ /#/ # no fragment, except...
972 and $string !~ m{#\z} # empty fragment
973 and $string !~ m{#[A-Za-z][A-Za-z0-9_:.-]*\z} # plain-name fragment
974 and $string !~ m{#/(?:[^~]|~[01])*\z}; # json pointer fragment
975  
976 6121 return 1;
977 }
978  
979 # produces an annotation whose value is the same as that of the current schema keyword
980 # makes a copy as this is passed back to the user, who cannot be trusted to not mutate it
981 1146 ▶sub annotate_self ($state, $schema) {
Subroutine annotate_self: called CC=2 SCAR=6.9
Pod undocumented
982 A($state, ref $schema->{$state->{keyword}} ? clone($schema->{$state->{keyword}})
983 1146 ▶ : $schema->{$state->{keyword}});
Branch: 1 branch
BranchTrueFalseTotal
ref $$schema{$$state{'keyword'}} ? : 102 1044 1146
984 }
985  
986 # use original value as stored in the NV, without losing precision
987 1325 ▶sub sprintf_num ($value) {
Subroutine sprintf_num: called CC=2 SCAR=6.9
Pod undocumented
988 1325 ▶ is_bignum($value) ? $value->bstr : sprintf('%s', $value);
Branch: 1 branch
BranchTrueFalseTotal
is_bignum($value) ? : 131 1194 1325
989 }
990  
991 {
992 # simple runtime-wide cache of $ids to filenames that are sourced from disk
993 46 my $schema_filename_cache = {};
994  
995 # adds a mapping from a URI to an absolute filename in the global runtime
996 # (available to all instances of the evaluator running in the same process).
997 966 ▶ sub register_schema ($uri, $filename) {
Subroutine register_schema: called CC=1 SCAR=0.0
Pod undocumented
998 966 $schema_filename_cache->{$uri} = $filename;
999 }
1000  
1001 2014 ▶ sub get_schema_filename ($uri) {
Subroutine get_schema_filename: called CC=1 SCAR=0.0
Pod undocumented
1002 2014 $schema_filename_cache->{$uri};
1003 }
1004 }
1005  
1006 46 1;
1007  
1008 __END__
« Modern/Result.pm ↑ Summary Modern/Vocabulary/Applicator.pm »