Modern.pm

filter: Statementstmt 98.9% 993 / 1004 Branchbran 83.6% 291 / 348 Conditioncond 86.1% 192 / 223 MC/DCmcdc 67.7% 84 / 124 Subroutinesub 99.1% 242 / 244 Podpod 82.3% 14 / 17 totaltotal 92.6% 1816 / 1960 CC 282 SCAR 23.6
CC
282
Cov
94%
_evaluate_subschema
57.6
evaluate
36.8
_traverse_subschema
32.8
CRAP 10.6
« Modern/Vocabulary/Applicator.pm ↑ Summary Modern/Vocabulary/Unevaluated.pm »
1 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package JSON::Schema::Modern; # git description: v0.653-7-g79ba092a
4 # vim: set ts=8 sts=2 sw=2 tw=100 et :
5 # ABSTRACT: Validate data against a schema using a JSON Schema
6 # KEYWORDS: JSON Schema validator data validation structure specification
7  
8 46 our $VERSION = '0.654';
9  
10 51 ▶use 5.020; # for fc, unicode_strings features
Subroutine BEGIN: called CC=1 SCAR=0.0
11 51 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 51 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 51 ▶use stable 0.031 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 51 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 51 ▶no autovivification warn => qw(fetch store exists delete);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 51 ▶use if "$]" >= 5.022, experimental => 're_strict';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 50 ▶no if "$]" >= 5.031009, feature => 'indirect';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 50 ▶no if "$]" >= 5.033001, feature => 'multidimensional';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 50 ▶no if "$]" >= 5.033006, feature => 'bareword_filehandles';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 50 ▶no if "$]" >= 5.041009, feature => 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 50 ▶no feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 50 ▶use Mojo::JSON (); # for JSON_XS, MOJO_NO_JSON_XS environment variables
Subroutine BEGIN: called CC=1 SCAR=0.0
23 50 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 50 ▶use List::Util 1.55 qw(pairs first uniqint pairmap uniqstr min);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 48 ▶use if "$]" < 5.041010, 'List::Util' => 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 49 ▶use if "$]" >= 5.041010, experimental => 'keyword_any';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 48 ▶use builtin::compat qw(refaddr load_module blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 49 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 49 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 49 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 49 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 48 ▶use MooX::TypeTiny 0.002002;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 48 ▶use Types::Standard 1.016003 qw(Bool Int Str HasMethods Enum InstanceOf HashRef Dict CodeRef Optional Slurpy ArrayRef Undef ClassName Tuple Map);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 48 ▶use Digest::MD5 'md5';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 48 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 48 ▶use JSON::Schema::Modern::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 48 ▶use JSON::Schema::Modern::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 48 ▶use JSON::Schema::Modern::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 48 ▶use JSON::Schema::Modern::Utilities qw(get_type canonical_uri E abort annotate_self jsonp is_type assert_uri local_annotations is_schema json_pointer_type canonical_uri_type core_types_type core_formats_type load_cached_document jsonp_set);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 48 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 46 our @CARP_NOT = qw(
43 JSON::Schema::Modern::Document
44 JSON::Schema::Modern::Vocabulary
45 JSON::Schema::Modern::Vocabulary::Applicator
46 JSON::Schema::Modern::Document::OpenAPI
47 OpenAPI::Modern
48 );
49  
50 48 ▶use constant SPECIFICATION_VERSION_DEFAULT => 'draft2020-12';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 48 ▶use constant SPECIFICATION_VERSIONS_SUPPORTED => [qw(draft4 draft6 draft7 draft2019-09 draft2020-12)];
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 has specification_version => (
54 is => 'ro',
55 isa => Enum(SPECIFICATION_VERSIONS_SUPPORTED),
56 71 ▶ coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
57 71 ▶ return $_[0] if any { $_[0] eq $_ } SPECIFICATION_VERSIONS_SUPPORTED->@*;
Branch: 1 branch
BranchTrueFalseTotal
if 66 4 70
58 5 ▶ my $real = 'draft'.($_[0]//'');
Condition50%1/2
$_[0] // ""
l!l
40
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
59 5 ▶ (any { $real eq $_ } SPECIFICATION_VERSIONS_SUPPORTED->@*) ? $real : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
? : 3 1 4
60 },
61 46 );
62  
63 46 has output_format => (
64 is => 'ro',
65 isa => Enum(JSON::Schema::Modern::Result->OUTPUT_FORMATS),
66 default => 'basic',
67 );
68  
69 has short_circuit => (
70 is => 'ro',
71 isa => Bool,
72 lazy => 1,
73 127 ▶ default => sub { $_[0]->output_format eq 'flag' && !$_[0]->collect_annotations },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->output_format eq "flag" and not $_[0]->collect_annotations 0 126 126
74 46 );
75  
76 46 has max_depth => (
77 is => 'ro',
78 isa => Int,
79 default => 50,
80 );
81  
82 1 ▶sub max_traversal_depth ($self) {
Subroutine max_traversal_depth: called CC=1 SCAR=0.0
Pod undocumented
83 1 carp 'max_traversal_depth is deprecated and could be removed anytime after 2026-05-09; use max_depth instead';
84 1 $self->max_depth;
85 }
86  
87 has validate_formats => (
88 is => 'ro',
89 isa => Bool,
90 lazy => 1,
91 # as specified by https://json-schema.org/draft/<version>/schema#/$vocabulary
92 102 ▶ default => sub { ($_[0]->specification_version//SPECIFICATION_VERSION_DEFAULT) =~ /^draft[467]\z/ ? 1 : 0 },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
($_[0]->specification_version // 'draft2020-12') =~ /^draft[467]\z/u ? : 12 89 101
Condition100%2/2
$_[0]->specification_version // "draft2020-12"
l!l
2972
MC/DC100%1/1
$_[0]->specification_version // "draft2020-12"
$_[0]->specification_version
Truth table100%2/2
A
A= $_[0]->specification_version
Aresult
11
00
93 46 );
94  
95 has validate_content_schemas => (
96 is => 'ro',
97 isa => Bool,
98 lazy => 1,
99 # defaults to false in latest versions, as specified by
100 # https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.8.2
101 143 ▶ default => sub { ($_[0]->specification_version//'') eq 'draft7' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->specification_version // ""
l!l
5983
MC/DC100%1/1
$_[0]->specification_version // ""
$_[0]->specification_version
Truth table100%2/2
A
A= $_[0]->specification_version
Aresult
11
00
102 46 );
103  
104 46 has [qw(collect_annotations scalarref_booleans stringy_numbers strict with_defaults)] => (
105 is => 'ro',
106 isa => Bool,
107 );
108  
109  
110 # { $format_name => { type => ..., sub => ... }, ... }
111 46 has _format_validations => (
112 is => 'bare',
113 isa => my $format_type = HashRef[Dict[
114 type => core_types_type|ArrayRef[core_types_type],
115 sub => CodeRef,
116 ]],
117 init_arg => 'format_validations',
118 );
119  
120 997 ▶sub _get_format_validation ($self, $format) { ($self->{_format_validations}//{})->{$format} }
Subroutine _get_format_validation: called CC=2 SCAR=6.9
Condition100%2/2
$$self{"_format_validations"} // {}
l!l
78918
MC/DC100%1/1
$$self{"_format_validations"} // {}
$$self{"_format_validations"}
Truth table100%2/2
A
A= $$self{"_format_validations"}
Aresult
11
00
121  
122 12 ▶sub add_format_validation ($self, $format, $definition) {
Subroutine add_format_validation: called CC=6 SCAR=17.9
Pod documented
123 12 ▶ return if exists(($self->{_format_validations}//{})->{$format});
Branch: 1 branch
BranchTrueFalseTotal
if exists(($$self{"_format_validations"} // {})->{$format}) 0 11 11
Condition100%2/2
$$self{"_format_validations"} // {}
l!l
38
MC/DC100%1/1
$$self{"_format_validations"} // {}
$$self{"_format_validations"}
Truth table100%2/2
A
A= $$self{"_format_validations"}
Aresult
11
00
124  
125 12 ▶ $definition = { type => 'string', sub => $definition } if ref $definition ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $definition ne "HASH" 6 5 11
126 12 $format_type->({ $format => $definition });
127  
128 # all core formats are of type string (so far); changing type of custom format is permitted
129 croak "Type for override of format $format does not match original type"
130 5 ▶ if core_formats_type->check($format) and $definition->{type} ne 'string';
Branch: 1 branch
BranchTrueFalseTotal
if core_formats_type()->check($format) and $$definition{"type"} ne "string" 1 3 4
Condition100%3/3
core_formats_type()->check($format) and $$definition{"type"} ne "string"
!ll&&!rl&&r
121
MC/DC100%2/2
core_formats_type()->check($format) and $$definition{"type"} ne "string"
core_formats_type()->check($format) $$definition{"type"} ne "string"
Truth table100%3/3
A and B
A= core_formats_type()->check($format)B= $$definition{"type"} ne "string"
ABresult
0X0
100
111
131  
132 48 ▶ use autovivification 'store';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 4 $self->{_format_validations}{$format} = $definition;
134 }
135  
136 247 ▶around BUILDARGS => sub ($orig, $class, @args) {
Subroutine __ANON__: called CC=11 SCAR=24.3
137 247 my $args = $class->$orig(@args);
138 croak 'output_format: strict_basic can only be used with specification_version: draft2019-09'
139 if ($args->{output_format}//'') eq 'strict_basic'
140 247 ▶ and ($args->{specification_version}//'') ne 'draft2019-09';
Branch: 1 branch
BranchTrueFalseTotal
if ($$args{"output_format"} // "") eq "strict_basic" and ($$args{"specification_version"} // "") ne "draft2019-09" 0 246 246
Condition71%5/7
($$args{"output_format"} // "") eq "strict_basic" and ($$args{"specification_version"} // "") ne "draft2019-09"
!ll&&!rl&&r
24510
$$args{"output_format"} // ""
l!l
1245
$$args{"specification_version"} // ""
l!l
10
MC/DC50%1/2
($$args{"output_format"} // "") eq "strict_basic" and ($$args{"specification_version"} // "") ne "draft2019-09"
$$args{"output_format"} $$args{"specification_version"}
Truth table66%2/3
A and B
A= $$args{"output_format"}B= $$args{"specification_version"}
ABresult
0X0
100
111
141  
142 croak 'collect_annotations cannot be used with specification_version '.$args->{specification_version}
143 247 ▶ if $args->{collect_annotations} and ($args->{specification_version}//'') =~ /^draft[467]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $$args{"collect_annotations"} and ($$args{"specification_version"} // "") =~ /^draft[467]\z/u 1 245 246
Condition100%5/5
$$args{"collect_annotations"} and ($$args{"specification_version"} // "") =~ /^draft[467]\z/u
!ll&&!rl&&r
226191
$$args{"specification_version"} // ""
l!l
614
MC/DC100%2/2
$$args{"collect_annotations"} and ($$args{"specification_version"} // "") =~ /^draft[467]\z/u
$$args{"collect_annotations"} $$args{"specification_version"}
Truth table100%3/3
A and B
A= $$args{"collect_annotations"}B= $$args{"specification_version"}
ABresult
0X0
100
111
144  
145 246 ▶ $args->{max_depth} = delete $args->{max_traversal_depth} if exists $args->{max_traversal_depth};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$args{"max_traversal_depth"} 0 245 245
146  
147 $args->{format_validations} = +{
148 map +($_->[0] => ref $_->[1] eq 'HASH' ? $_->[1] : +{ type => 'string', sub => $_->[1] }),
149 pairs $args->{format_validations}->%*
150 246 ▶ } if $args->{format_validations};
Branch: 2 branches
BranchTrueFalseTotal
if $$args{"format_validations"} 7 238 245
ref $our $_[1] eq 'HASH' ? : 5 3 8
151  
152 246 return $args;
153 46 };
154  
155 88785 ▶my $_isa = sub ($obj, $class) { blessed($obj) && $obj->isa($class) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) and $obj->isa($class) 36310 52474 88784
156  
157 17384 ▶sub add_schema {
Subroutine add_schema: called CC=9 SCAR=22.1
Pod documented
158 17384 ▶ croak 'insufficient arguments' if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 17383 17383
159 17384 my $self = shift;
160  
161 17384 ▶ if ($_[0]->$_isa('JSON::Schema::Modern::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->$_isa("JSON::Schema::Modern::Document")) 1 17382 17383
162 2 Carp::carp('use of deprecated form of add_schema with document');
163 2 return $self->add_document($_[0]);
164 }
165  
166 # TODO: resolve $uri against $self->base_uri
167 17383 ▶ my $uri = !is_schema($_[0]) ? Mojo::URL->new(shift)
Branch: 2 branches
BranchTrueFalseTotal
!is_schema($_[0]) ? : 33 17349 17382
$_[0]->$_isa('Mojo::URL') ? : 0 17349 17349
168 : $_[0]->$_isa('Mojo::URL') ? shift : Mojo::URL->new;
169  
170 17383 ▶ croak 'cannot add a schema with a uri with a fragment' if defined $uri->fragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->fragment 1 17381 17382
171 17382 ▶ croak 'insufficient arguments' if not @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 17381 17381
172  
173 17382 ▶ if ($_[0]->$_isa('JSON::Schema::Modern::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->$_isa("JSON::Schema::Modern::Document")) 1 17380 17381
174 2 Carp::carp('use of deprecated form of add_schema with document');
175 2 return $self->add_document($uri, $_[0]);
176 }
177  
178 # document BUILD will trigger $self->traverse($schema)
179 # Note we do not pass the uri to the document constructor, so resources in that document may still
180 # be relative
181 17381 my $document = JSON::Schema::Modern::Document->new(
182 schema => $_[0],
183 evaluator => $self, # used mainly for traversal during document construction
184 );
185  
186 # try to reuse the same document, if the same schema is being added twice:
187 # this results in _add_resource silently ignoring the duplicate add, rather than erroring.
188 17381 my $schema_checksum = $document->_checksum(md5($self->_json_decoder->encode($document->schema)));
189 17381 ▶ if (my $existing_doc = first {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing_doc = &first(sub { my $existing_checksum = $_->_checksum // $_->_checksum(md5($self->_json_decoder->encode($_->schema))); $existing_checksum eq $schema_checksum and $_->canonical_uri eq $document->canonical_uri; } , &uniqint(map($our $_{"document"}, $self->_canonical_resources)))) 12300 5080 17380
190 731885 ▶ my $existing_checksum = $_->_checksum
Condition66%2/3
$_->_checksum // $_->_checksum(md5($self->_json_decoder->encode($_->schema)))
l!l&&r!l&&!r
7314314530
MC/DC0%0/2
$_->_checksum // $_->_checksum(md5($self->_json_decoder->encode($_->schema)))
$_->_checksum $_->_checksum(md5($self->_json_decoder->encode($_->schema)))
Truth table66%2/3
A // B
A= $_->_checksumB= $_->_checksum(md5($self->_json_decoder->encode($_->schema)))
ABresult
1X1
011
000
191 // $_->_checksum(md5($self->_json_decoder->encode($_->schema)));
192 731885 ▶ $existing_checksum eq $schema_checksum
Branch: 1 branch
BranchTrueFalseTotal
$existing_checksum eq $schema_checksum and $_->canonical_uri eq $document->canonical_uri 12300 719584 731884
193 and $_->canonical_uri eq $document->canonical_uri
194 # FIXME: must also check spec version/metaschema_uri/vocabularies
195 } uniqint map $_->{document}, $self->_canonical_resources) {
196 12301 $document = $existing_doc;
197 }
198  
199 17381 $self->add_document($uri, $document);
200 }
201  
202 18194 ▶sub add_document {
Subroutine add_document: called CC=17 SCAR=28.6
Pod documented
203 18194 ▶ croak 'insufficient arguments' if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 18193 18193
204 18194 my $self = shift;
205  
206 # TODO: resolve $uri against $self->base_uri
207 18194 ▶ my $base_uri = !$_[0]->$_isa('JSON::Schema::Modern::Document') ? Mojo::URL->new(shift)
Branch: 2 branches
BranchTrueFalseTotal
!$_[0]->$_isa('JSON::Schema::Modern::Document') ? : 18182 11 18193
$_[0]->$_isa('Mojo::URL') ? : 0 11 11
208 : $_[0]->$_isa('Mojo::URL') ? shift : Mojo::URL->new;
209  
210 18194 ▶ croak 'cannot add a schema with a uri with a fragment' if defined $base_uri->fragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $base_uri->fragment 0 18193 18193
211 18194 ▶ croak 'insufficient arguments' if not @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 18193 18193
212  
213 18194 my $document = shift;
214 18194 ▶ croak 'wrong document type' if not $document->$_isa('JSON::Schema::Modern::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless $document->$_isa("JSON::Schema::Modern::Document") 0 18193 18193
215  
216 # we will never add a document to the resource index if it has errors
217 18194 ▶ die JSON::Schema::Modern::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
if $document->has_errors 141 18052 18193
218 output_format => $self->output_format,
219 valid => 0,
220 errors => [ $document->errors ],
221 exception => 1,
222 ) if $document->has_errors;
223  
224 18053 ▶ if (not length $base_uri){
Branch: 1 branch
BranchTrueFalseTotal
unless (length $base_uri) 17219 833 18052
225 17220 foreach my $res_pair ($document->resource_pairs) {
226 18078 my ($uri_string, $doc_resource) = @$res_pair;
227  
228 # this might croak if there are duplicates or malformed entries.
229 18078 $self->_add_resource($uri_string => +{ $doc_resource->%*, document => $document });
230 }
231  
232 17215 return $document;
233 }
234  
235 834 my @root; # uri_string => resource hash of the resource at path ''
236  
237 # document resources are added after resolving each resource against our provided base uri
238 834 foreach my $res_pair ($document->resource_pairs) {
239 864 my ($uri_string, $doc_resource) = @$res_pair;
240 864 $uri_string = Mojo::URL->new($uri_string)->to_abs($base_uri)->to_string;
241  
242 my $new_resource = {
243 canonical_uri => Mojo::URL->new($doc_resource->{canonical_uri})->to_abs($base_uri),
244 864 $doc_resource->%{qw(path specification_version vocabularies)},
245 document => $document,
246 };
247  
248 864 ▶ foreach my $anchor (keys (($doc_resource->{anchors}//{})->%*)) {
Condition100%2/2
$$doc_resource{"anchors"} // {}
l!l
169694
MC/DC100%1/1
$$doc_resource{"anchors"} // {}
$$doc_resource{"anchors"}
Truth table100%2/2
A
A= $$doc_resource{"anchors"}
Aresult
11
00
249 48 ▶ use autovivification 'store';
Subroutine BEGIN: called CC=1 SCAR=0.0
250 $new_resource->{anchors}{$anchor} = {
251 $doc_resource->{anchors}{$anchor}->%{path},
252 (map +($_->[1] ? @$_ : ()), [ $doc_resource->{anchors}{$anchor}->%{dynamic} ]),
253 170 ▶ canonical_uri => Mojo::URL->new($doc_resource->{anchors}{$anchor}{canonical_uri})->to_abs($base_uri),
Branch: 1 branch
BranchTrueFalseTotal
$our $_[1] ? : 73 96 169
254 };
255 }
256  
257 # this might croak if there are duplicates or malformed entries.
258 864 $self->_add_resource($uri_string => $new_resource);
259 857 ▶ @root = ($uri_string => $new_resource) if $new_resource->{path} eq '' and $uri_string !~ /#./;
Branch: 1 branch
BranchTrueFalseTotal
if $$new_resource{"path"} eq "" and not $uri_string =~ /#./u 826 30 856
Condition66%2/3
$$new_resource{"path"} eq "" and not $uri_string =~ /#./u
!ll&&!rl&&r
300826
MC/DC50%1/2
$$new_resource{"path"} eq "" and not $uri_string =~ /#./u
$$new_resource{"path"} eq "" not $uri_string =~ /#./u
Truth table66%2/3
A and not(B)
A= $$new_resource{"path"} eq ""B= not $uri_string =~ /#./u
ABresult
0X0
100
111
260 }
261  
262 # associate the root resource with the base uri we were provided, if it does not already exist
263 827 ▶ $self->_add_resource($base_uri.'' => $root[1]) if $root[0] ne $base_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $root[0] ne $base_uri 54 772 826
264  
265 827 return $document;
266 }
267  
268 4 ▶sub evaluate_json_string ($self, $json_data, $schema, $config_override = {}) {
Subroutine evaluate_json_string: called CC=3 SCAR=11.0
Pod documented
269 4 ▶ croak 'evaluate_json_string called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 3 3
270  
271 4 my $data;
272 4 try {
273 4 $data = $self->_json_decoder->decode($json_data)
274 }
275 catch ($e) {
276 3 return JSON::Schema::Modern::Result->new(
277 output_format => $self->output_format,
278 valid => 0,
279 exception => 1,
280 errors => [
281 JSON::Schema::Modern::Error->new(
282 depth => 0,
283 mode => 'traverse',
284 keyword => undef,
285 keyword_location => '',
286 error => $e,
287 )
288 ],
289 );
290 }
291  
292 2 return $self->evaluate($data, $schema, $config_override);
293 }
294  
295 # this is called whenever we need to walk a document for something.
296 # for now it is just called when a ::Document object is created, to verify the integrity of the
297 # schema structure, to identify the metaschema (via the $schema keyword), and to extract all
298 # embedded resources via $id and $anchor keywords within.
299 # Returns the internal $state object accumulated during the traversal.
300 17934 ▶sub traverse ($self, $schema_reference, $config_override = {}) {
Subroutine traverse: called CC=26 SCAR=34.3
Pod documented
301 17934 my %overrides = %$config_override;
302 17934 delete @overrides{qw(callbacks initial_schema_uri metaschema_uri traversed_keyword_path specification_version skip_ref_checks)};
303 17934 ▶ croak join(', ', sort keys %overrides), ' not supported as a config override in traverse'
Branch: 1 branch
BranchTrueFalseTotal
if keys %overrides 0 17933 17933
304 if keys %overrides;
305  
306 # Note: the starting position is not guaranteed to be at the root of the $document,
307 # nor is the fragment portion of this uri necessarily empty
308 17934 ▶ my $initial_uri = Mojo::URL->new($config_override->{initial_schema_uri} // ());
Condition66%2/3
$$config_override{"initial_schema_uri"} // ()
l!l&&r!l&&!r
33179000
MC/DC0%0/2
$$config_override{"initial_schema_uri"} // ()
$$config_override{"initial_schema_uri"} ()
Truth table66%2/3
A // B
A= $$config_override{"initial_schema_uri"}B= ()
ABresult
1X1
011
000
309 17934 ▶ my $initial_path = $config_override->{traversed_keyword_path} // '';
Condition100%2/2
$$config_override{"traversed_keyword_path"} // ""
l!l
417929
MC/DC100%1/1
$$config_override{"traversed_keyword_path"} // ""
$$config_override{"traversed_keyword_path"}
Truth table100%2/2
A
A= $$config_override{"traversed_keyword_path"}
Aresult
11
00
310 17934 ▶ my $spec_version = $config_override->{specification_version} // $self->specification_version // SPECIFICATION_VERSION_DEFAULT;
Condition100%5/5
($$config_override{"specification_version"} // $self->specification_version) // "draft2020-12"
l!l
17391542
$$config_override{"specification_version"} // $self->specification_version
l!l&&r!l&&!r
29817093542
MC/DC100%2/2
($$config_override{"specification_version"} // $self->specification_version) // "draft2020-12"
$$config_override{"specification_version"} $self->specification_version
Truth table100%3/3
A // B
A= $$config_override{"specification_version"}B= $self->specification_version
ABresult
1X1
011
000
311  
312 17934 ▶ croak 'traversed_keyword_path must be a json pointer' if $initial_path !~ m{^(?:/|\z)};
Branch: 1 branch
BranchTrueFalseTotal
unless $initial_path =~ m[^(?:/|\z)]u 0 17933 17933
313  
314 17934 ▶ if (length(my $uri_path = $initial_uri->fragment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $uri_path = $initial_uri->fragment)) 4 17929 17933
315 5 ▶ croak 'initial_schema_uri fragment must be a json pointer' if $uri_path !~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $uri_path =~ m[^/]u 0 4 4
316  
317 5 ▶ croak 'traversed_keyword_path does not match initial_schema_uri path fragment'
Branch: 1 branch
BranchTrueFalseTotal
if substr($initial_path, -length($uri_path)) ne $uri_path 0 4 4
318 if substr($initial_path, -length($uri_path)) ne $uri_path;
319 }
320  
321 my $state = {
322 depth => 0,
323 data_path => '', # this never changes since we don't have an instance yet
324 initial_schema_uri => $initial_uri, # the canonical URI as of the start of this method or last $id
325 traversed_keyword_path => $initial_path, # the accumulated traversal path as of the start or last $id
326 keyword_path => '', # the rest of the path, since the start of this method or last $id
327 specification_version => $spec_version,
328 errors => [],
329 identifiers => {},
330 subschemas => [],
331 $config_override->{skip_ref_checks} ? () : (references => []),
332 callbacks => $config_override->{callbacks} // {},
333 17934 ▶ evaluator => $self,
Branch: 1 branch
BranchTrueFalseTotal
$$config_override{'skip_ref_checks'} ? : 69 17864 17933
Condition100%2/2
$$config_override{"callbacks"} // {}
l!l
117932
MC/DC100%1/1
$$config_override{"callbacks"} // {}
$$config_override{"callbacks"}
Truth table100%2/2
A
A= $$config_override{"callbacks"}
Aresult
11
00
334 traverse => 1,
335 };
336  
337 17934 my $valid = 1;
338  
339 17934 try {
340 # determine the initial value of specification_version and vocabularies, so we have something to start
341 # with in _traverse_subschema().
342 # a subsequent "$schema" keyword can still change these values, and it is always processed
343 # first, so the override is skipped if the keyword exists in the schema
344 $state->{metaschema_uri} =
345 (ref $schema_reference eq 'HASH' && exists $schema_reference->{'$schema'} ? undef
346 17934 ▶ : $config_override->{metaschema_uri}) // $self->METASCHEMA_URIS->{$spec_version};
Branch: 1 branch
BranchTrueFalseTotal
ref $schema_reference eq 'HASH' && exists $$schema_reference{'$schema'} ? : 5870 12063 17933
Condition83%5/6
(ref $schema_reference eq "HASH" && exists $$schema_reference{"\$schema"} ? undef : $$config_override{"metaschema_uri"}) // $self->METASCHEMA_URIS->{$spec_version}
l!l&&r!l&&!r
6179270
ref $schema_reference eq "HASH" and exists $$schema_reference{"\$schema"}
!ll&&!rl&&r
156119075870
MC/DC0%0/3
(ref $schema_reference eq "HASH" && exists $$schema_reference{"\$schema"} ? undef : $$config_override{"metaschema_uri"}) // $self->METASCHEMA_URIS->{$spec_version}
ref $schema_reference eq "HASH" exists $$schema_reference{"\$schema"} $self->METASCHEMA_URIS->{$spec_version}
Truth table40%2/5
A and B // C
A= ref $schema_reference eq "HASH"B= exists $$schema_reference{"\$schema"}C= $self->METASCHEMA_URIS->{$spec_version}
ABCresult
11X1
0X11
1011
0X00
1000
347  
348 17934 ▶ if (my $metaschema_info = $self->_get_metaschema_vocabulary_classes($state->{metaschema_uri})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $metaschema_info = $self->_get_metaschema_vocabulary_classes($$state{'metaschema_uri'})) { } 17927 6 17933
349 17928 $state->@{qw(specification_version vocabularies)} = @$metaschema_info;
350 }
351 else {
352 # metaschema has not been processed for vocabularies yet...
353  
354 die 'something went wrong - cannot get metaschema data for '.$state->{metaschema_uri}
355 7 ▶ if not $config_override->{metaschema_uri};
Branch: 1 branch
BranchTrueFalseTotal
unless $$config_override{"metaschema_uri"} 0 6 6
356  
357 # use the Core vocabulary to set metaschema info via the '$schema' keyword implementation
358 $valid = $self->_get_metaschema_vocabulary_classes($self->METASCHEMA_URIS->{$spec_version})->[1][0]
359 7 ->_traverse_keyword_schema({ '$schema' => $state->{metaschema_uri}.'' }, $state);
360 }
361  
362 17934 ▶ $valid = $self->_traverse_subschema($schema_reference, $state) if $valid and not $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $valid and not @{$$state{"errors"}} 17929 4 17933
Condition66%2/3
$valid and not @{$$state{"errors"}}
!ll&&!rl&&r
4017929
MC/DC50%1/2
$valid and not @{$$state{"errors"}}
$valid not @{$$state{"errors"}}
Truth table66%2/3
A and not(B)
A= $validB= not @{$$state{"errors"}}
ABresult
0X0
100
111
363 17934 ▶ die 'result is false but there are no errors' if not $valid and not $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid or @{$$state{"errors"}} 0 17933 17933
Condition66%2/3
$valid or @{$$state{"errors"}}
l!l&&r!l&&!r
177671660
MC/DC0%0/2
$valid or @{$$state{"errors"}}
$valid @{$$state{"errors"}}
Truth table66%2/3
A or B
A= $validB= @{$$state{"errors"}}
ABresult
1X1
011
000
364 17934 ▶ die 'result is true but there are errors' if $valid and $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $valid and @{$$state{"errors"}} 0 17933 17933
Condition66%2/3
$valid and @{$$state{"errors"}}
!ll&&!rl&&r
166177670
MC/DC0%0/2
$valid and @{$$state{"errors"}}
$valid @{$$state{"errors"}}
Truth table66%2/3
A and B
A= $validB= @{$$state{"errors"}}
ABresult
0X0
100
111
365 }
366 catch ($e) {
367 1 ▶ if ($e->$_isa('JSON::Schema::Modern::Result')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->$_isa('JSON::Schema::Modern::Result')) { } 0 0 0
elsif ($e->$_isa('JSON::Schema::Modern::Error')) { } 0 0 0
368 1 push $state->{errors}->@*, $e->errors;
369 }
370 elsif ($e->$_isa('JSON::Schema::Modern::Error')) {
371 # note: we should never be here, since traversal subs are no longer fatal
372 1 push $state->{errors}->@*, $e;
373 }
374 else {
375 1 E({ %$state, exception => 1 }, 'EXCEPTION: '.$e);
376 }
377 }
378  
379 17934 return $state;
380 }
381  
382 # the actual runtime evaluation of the schema against input data.
383 17381 ▶sub evaluate ($self, $data, $schema_reference, $config_override = {}) {
Subroutine evaluate: called CC=36 SCAR=36.0
Pod documented
384 17381 ▶ croak 'evaluate called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 17380 17380
385  
386 17381 my %overrides = %$config_override;
387 17381 delete @overrides{qw(validate_formats validate_content_schemas short_circuit collect_annotations scalarref_booleans stringy_numbers strict with_defaults callbacks data_path traversed_keyword_path _strict_schema_data)};
388 17381 ▶ croak join(', ', sort keys %overrides), ' not supported as a config override in evaluate'
Branch: 1 branch
BranchTrueFalseTotal
if keys %overrides 0 17380 17380
389 if keys %overrides;
390  
391 my $state = {
392 data_path => $config_override->{data_path} // '',
393 17381 ▶ traversed_keyword_path => $config_override->{traversed_keyword_path} // '', # the accumulated path as of the start of evaluation or last $id or $ref
Condition100%4/4
$$config_override{"data_path"} // ""
l!l
217378
$$config_override{"traversed_keyword_path"} // ""
l!l
217378
MC/DC100%1/1
$$config_override{"data_path"} // ""
$$config_override{"data_path"}
MC/DC100%1/1
$$config_override{"traversed_keyword_path"} // ""
$$config_override{"traversed_keyword_path"}
Truth table100%2/2
A
A= $$config_override{"data_path"}
Aresult
11
00
Truth table100%2/2
A
A= $$config_override{"traversed_keyword_path"}
Aresult
11
00
394 initial_schema_uri => Mojo::URL->new, # the canonical URI as of the start of evaluation or last $id or $ref
395 keyword_path => '', # the rest of the path, since the start of evaluation or last $id or $ref
396 errors => [],
397 depth => 0,
398 };
399  
400 17381 ▶ $state->{data} = jsonp_set('', $state->{data_path}, ref $data ? clone($data) : $data);
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 10694 6686 17380
401  
402 17381 my $valid;
403 17381 try {
404 17381 ▶ if (is_schema($schema_reference)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_schema($schema_reference)) { } 17304 76 17380
elsif (ref $schema_reference and not $schema_reference->$_isa('Mojo::URL')) { } 4 137 141
405 # traverse is called via add_schema -> ::Document->new -> ::Document->BUILD
406 17305 $schema_reference = $self->add_schema($schema_reference)->canonical_uri;
407 }
408 ▶ elsif (ref $schema_reference and not $schema_reference->$_isa('Mojo::URL')) {
Condition100%3/3
ref $schema_reference and not $schema_reference->$_isa("Mojo::URL")
!ll&&!rl&&r
6574
MC/DC100%2/2
ref $schema_reference and not $schema_reference->$_isa("Mojo::URL")
ref $schema_reference not $schema_reference->$_isa("Mojo::URL")
Truth table100%3/3
A and not(B)
A= ref $schema_referenceB= not $schema_reference->$_isa("Mojo::URL")
ABresult
0X0
100
111
409 5 abort($state, 'invalid schema type: %s', get_type($schema_reference));
410 }
411  
412 17231 my $schema_info = $self->_fetch_from_uri($schema_reference);
413 17231 ▶ abort($state, 'EXCEPTION: unable to find resource "%s"', $schema_reference)
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_info 6 17224 17230
414 if not $schema_info;
415  
416 abort($state, 'EXCEPTION: "%s" is not a schema', $schema_reference)
417 17225 ▶ if not $schema_info->{document}->get_entity_at_location($schema_info->{document_path});
Branch: 1 branch
BranchTrueFalseTotal
unless $$schema_info{"document"}->get_entity_at_location($$schema_info{"document_path"}) 1 17223 17224
418  
419 $state = +{
420 %$state,
421 initial_schema_uri => $schema_info->{canonical_uri}, # the canonical URI as of the start of evaluation, or last $id or $ref
422 $schema_info->%{qw(document specification_version vocabularies)},
423 dynamic_scope => [ $schema_info->{canonical_uri}->clone->fragment(undef) ],
424 annotations => [],
425 seen => {},
426 callbacks => $config_override->{callbacks} // {},
427 evaluator => $self,
428 (map do {
429 120562 ▶ my $val = $config_override->{$_} // $self->$_;
Condition100%3/3
$$config_override{$_} // $self->$_
l!l&&r!l&&!r
376852052004
MC/DC100%2/2
$$config_override{$_} // $self->$_
$$config_override{$_} $self->$_
Truth table100%3/3
A // B
A= $$config_override{$_}B= $self->$_
ABresult
1X1
011
000
430 120562 ▶ defined $val ? ($_ => $val) : ()
Branch: 2 branches
BranchTrueFalseTotal
defined $val ? : 68557 52004 120561
$$config_override{'with_defaults'} // $self->with_defaults ? : 10 17213 17223
Condition100%3/3
$$config_override{"with_defaults"} // $self->with_defaults
l!l&&r!l&&!r
1917213
MC/DC100%2/2
$$config_override{"with_defaults"} // $self->with_defaults
$$config_override{"with_defaults"} $self->with_defaults
Truth table100%3/3
A // B
A= $$config_override{"with_defaults"}B= $self->with_defaults
ABresult
1X1
011
000
431 # note: this is a subset of the allowed overrides defined above
432 }, qw(validate_formats validate_content_schemas short_circuit collect_annotations scalarref_booleans stringy_numbers strict)),
433 17224 ▶ $config_override->{with_defaults} // $self->with_defaults ? (defaults => {}) : (),
Condition100%2/2
$$config_override{"callbacks"} // {}
l!l
817215
MC/DC100%1/1
$$config_override{"callbacks"} // {}
$$config_override{"callbacks"}
Truth table100%2/2
A
A= $$config_override{"callbacks"}
Aresult
11
00
434 };
435  
436 # this hash will be added to at each level of schema evaluation
437 17224 ▶ $state->{seen_data_properties} = {} if $config_override->{_strict_schema_data};
Branch: 1 branch
BranchTrueFalseTotal
if $$config_override{"_strict_schema_data"} 5 17218 17223
438  
439 # we're going to set collect_annotations during evaluation when we see an unevaluated* keyword
440 # (or for object data when the _strict_schema_data configuration is set),
441 # but after we pass to a new data scope we'll clear it again.. unless we've got the config set
442 # globally for the entire evaluation, so we store that value in a high bit.
443 17223 ▶ $state->{collect_annotations} = ($state->{collect_annotations}//0) << 8;
Condition100%2/2
$$state{"collect_annotations"} // 0
l!l
16822401
MC/DC100%1/1
$$state{"collect_annotations"} // 0
$$state{"collect_annotations"}
Truth table100%2/2
A
A= $$state{"collect_annotations"}
Aresult
11
00
444  
445 17223 $valid = $self->_evaluate_subschema($data, $schema_info->{schema}, $state);
446 17200 ▶ warn 'result is false but there are no errors' if not $valid and not $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid or @{$$state{"errors"}} 0 17200 17200
Condition66%2/3
$valid or @{$$state{"errors"}}
l!l&&r!l&&!r
952376770
MC/DC0%0/2
$valid or @{$$state{"errors"}}
$valid @{$$state{"errors"}}
Truth table66%2/3
A or B
A= $validB= @{$$state{"errors"}}
ABresult
1X1
011
000
447 17200 ▶ warn 'result is true but there are errors' if $valid and $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $valid and @{$$state{"errors"}} 0 17200 17200
Condition66%2/3
$valid and @{$$state{"errors"}}
!ll&&!rl&&r
767795230
MC/DC0%0/2
$valid and @{$$state{"errors"}}
$valid @{$$state{"errors"}}
Truth table66%2/3
A and B
A= $validB= @{$$state{"errors"}}
ABresult
0X0
100
111
448 }
449 catch ($e) {
450 180 ▶ if ($e->$_isa('JSON::Schema::Modern::Result')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->$_isa('JSON::Schema::Modern::Result')) { } 141 39 180
elsif ($e->$_isa('JSON::Schema::Modern::Error')) { } 34 5 39
451 141 return $e;
452 }
453 elsif ($e->$_isa('JSON::Schema::Modern::Error')) {
454 34 push $state->{errors}->@*, $e;
455 }
456 else {
457 5 $valid = E({ %$state, exception => 1 }, 'EXCEPTION: '.$e);
458 }
459 }
460  
461 17239 ▶ if ($state->{seen_data_properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$state{"seen_data_properties"}) 5 17234 17239
462 5 my %unknown_keywords;
463 5 foreach my $property (sort grep !$state->{seen_data_properties}{$_},
464 keys $state->{seen_data_properties}->%*) {
465 15 my ($parent, $keyword) = ($property =~ m{^(.*)/([^/]*)\z});
466 15 ▶ push(($unknown_keywords{$parent}//=[])->@*, $keyword);
Condition100%2/2
$unknown_keywords{$parent} //= []
l!l
78
MC/DC100%1/1
$unknown_keywords{$parent} //= []
$unknown_keywords{$parent}
Truth table100%2/2
A
A= $unknown_keywords{$parent}
Aresult
11
00
467 }
468  
469 5 foreach my $parent (sort keys %unknown_keywords) {
470 $valid = E({ %$state, data_path => $parent },
471 'unknown keyword%s seen in schema: %s', $unknown_keywords{$parent}->@* > 1 ? 's' : '',
472 8 ▶ join(', ', sort $unknown_keywords{$parent}->@*));
Branch: 1 branch
BranchTrueFalseTotal
@{$unknown_keywords{$parent}} > 1 ? : 7 1 8
473 }
474 }
475  
476 17239 ▶ die 'evaluate validity inconsistent with error count' if $valid xor !$state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $valid xor not @{$$state{"errors"}} 0 17239 17239
Condition50%2/4
$valid xor not @{$$state{"errors"}}
l&&rl&&!r!l&&r!l&&!r
9519007720
MC/DC0%0/2
$valid xor not @{$$state{"errors"}}
$valid not @{$$state{"errors"}}
Truth table50%2/4
A xor not(B)
A= $validB= not @{$$state{"errors"}}
ABresult
110
101
011
000
477  
478 return JSON::Schema::Modern::Result->new(
479 output_format => $self->output_format,
480 valid => $valid,
481 $valid
482 # strip annotations from result if user didn't explicitly ask for them
483 ? ($config_override->{collect_annotations} // $self->collect_annotations
484 ? (annotations => $state->{annotations}) : ())
485 : (errors => $state->{errors}),
486 $state->{defaults} ? (defaults => $state->{defaults}) : (),
487 data => $state->{data},
488 17239 ▶ );
Branch: 3 branches
BranchTrueFalseTotal
$valid ? : 9519 7720 17239
$$config_override{'collect_annotations'} // $self->collect_annotations ? : 28 9491 9519
$$state{'defaults'} ? : 10 17229 17239
Condition100%3/3
$$config_override{"collect_annotations"} // $self->collect_annotations
l!l&&r!l&&!r
39339177
MC/DC100%2/2
$$config_override{"collect_annotations"} // $self->collect_annotations
$$config_override{"collect_annotations"} $self->collect_annotations
Truth table100%3/3
A // B
A= $$config_override{"collect_annotations"}B= $self->collect_annotations
ABresult
1X1
011
000
489 }
490  
491 11 ▶sub validate_schema ($self, $schema, $config_override = {}) {
Subroutine validate_schema: called CC=10 SCAR=23.3
Pod documented
492 11 ▶ croak 'validate_schema called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 11 11
493  
494 my $metaschema_uri = ref $schema eq 'HASH' && $schema->{'$schema'} ? $schema->{'$schema'}
495 11 ▶ : $self->METASCHEMA_URIS->{$self->specification_version // $self->SPECIFICATION_VERSION_DEFAULT};
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'HASH' && $$schema{'$schema'} ? : 6 5 11
Condition50%3/6
ref $schema eq "HASH" and $$schema{"\$schema"}
!ll&&!rl&&r
056
$self->specification_version // $self->SPECIFICATION_VERSION_DEFAULT
l!l&&r!l&&!r
050
MC/DC50%1/2
ref $schema eq "HASH" and $$schema{"\$schema"}
ref $schema eq "HASH" $$schema{"\$schema"}
MC/DC0%0/2
$self->specification_version // $self->SPECIFICATION_VERSION_DEFAULT
$self->specification_version $self->SPECIFICATION_VERSION_DEFAULT
Truth table66%2/3
A and B
A= ref $schema eq "HASH"B= $$schema{"\$schema"}
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $self->specification_versionB= $self->SPECIFICATION_VERSION_DEFAULT
ABresult
1X1
011
000
496  
497 my $result = $self->evaluate($schema, $metaschema_uri,
498 {
499 %$config_override,
500 11 ▶ $self->strict || $config_override->{strict} ? (_strict_schema_data => 1) : (),
Branch: 1 branch
BranchTrueFalseTotal
$self->strict || $$config_override{'strict'} ? : 5 6 11
Condition100%3/3
$self->strict or $$config_override{"strict"}
l!l&&r!l&&!r
326
MC/DC100%2/2
$self->strict or $$config_override{"strict"}
$self->strict $$config_override{"strict"}
Truth table100%3/3
A or B
A= $self->strictB= $$config_override{"strict"}
ABresult
1X1
011
000
501 validate_formats => 1,
502 });
503  
504 11 ▶ return $result if not $result->valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $result->valid 8 3 11
505  
506 # the traversal pass will validate all constraints that weren't handled by the metaschema
507 3 my $state = $self->traverse($schema);
508 return JSON::Schema::Modern::Result->new(
509 output_format => $self->output_format,
510 valid => 0,
511 errors => $state->{errors},
512 3 ▶ ) if $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$$state{"errors"}} 1 2 3
513  
514 2 return $result; # valid: true
515 }
516  
517 9 ▶sub get ($self, $uri_reference) {
Subroutine get: called CC=12 SCAR=25.1
Pod documented
518 8 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 5 3 8
519 5 my $schema_info = $self->_fetch_from_uri($uri_reference);
520 5 ▶ return if not $schema_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_info 1 4 5
521 4 ▶ my $subschema = ref $schema_info->{schema} ? clone($schema_info->{schema}) : $schema_info->{schema};
Branch: 1 branch
BranchTrueFalseTotal
ref $$schema_info{'schema'} ? : 3 1 4
522 4 return ($subschema, $schema_info->{canonical_uri});
523 }
524 else { # abridged version of _fetch_from_uri
525 3 ▶ $uri_reference = Mojo::URL->new($uri_reference) if not ref $uri_reference;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri_reference 3 0 3
526 3 my $fragment = $uri_reference->fragment;
527 3 my $resource = $self->_get_or_load_resource($uri_reference->clone->fragment(undef));
528 3 ▶ return if not $resource;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource 0 3 3
529  
530 3 my $schema;
531 3 ▶ if (not length($fragment) or $fragment =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $fragment or $fragment =~ m[^/]u) { } 2 1 3
Condition100%3/3
not length $fragment or $fragment =~ m[^/]u
l!l&&r!l&&!r
111
MC/DC100%2/2
not length $fragment or $fragment =~ m[^/]u
not length $fragment $fragment =~ m[^/]u
Truth table100%3/3
not(A) or B
A= not length $fragmentB= $fragment =~ m[^/]u
ABresult
1X1
011
000
532 2 ▶ $schema = $resource->{document}->get($resource->{path}.($fragment//''));
Condition100%2/2
$fragment // ""
l!l
11
MC/DC100%1/1
$fragment // ""
$fragment
Truth table100%2/2
A
A= $fragment
Aresult
11
00
533 }
534 else { # we are following a URI with a plain-name fragment
535 1 ▶ return if not my $subresource = ($resource->{anchors}//{})->{$fragment};
Branch: 1 branch
BranchTrueFalseTotal
unless my $subresource = ($$resource{"anchors"} // {})->{$fragment} 1 0 1
Condition50%1/2
$$resource{"anchors"} // {}
l!l
10
MC/DC0%0/1
$$resource{"anchors"} // {}
$$resource{"anchors"}
Truth table50%1/2
A
A= $$resource{"anchors"}
Aresult
11
00
536 0 $schema = $resource->{document}->get($subresource->{path});
537 }
538 2 ▶ return ref $schema ? clone($schema) : $schema;
Branch: 1 branch
BranchTrueFalseTotal
ref $schema ? : 1 1 2
539 }
540 }
541  
542 1 ▶sub get_document ($self, $uri_reference) {
Subroutine get_document: called CC=2 SCAR=17.9
Pod documented
543 0 my $schema_info = $self->_fetch_from_uri($uri_reference);
544 0 ▶ return if not $schema_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_info 0 0 0
545 0 return $schema_info->{document};
546 }
547  
548 # defined lower down:
549 # sub add_media_type ($self, $media_type, $sub) { ... }
550 # sub get_media_type ($self, $media_type) { ... }
551 # sub add_encoding ($self, $encoding, $sub) { ... }
552 # sub get_encoding ($self, $encoding) { ... }
553 # sub add_vocabulary ($self, $classname) { ... }
554  
555 ######## NO PUBLIC INTERFACES FOLLOW THIS POINT ########
556  
557 # current spec version => { keyword => undef, or arrayref of alternatives }
558 46 my %removed_keywords = (
559 'draft4' => {
560 },
561 'draft6' => {
562 id => [ '$id' ],
563 },
564 'draft7' => {
565 id => [ '$id' ],
566 },
567 'draft2019-09' => {
568 id => [ '$id' ],
569 definitions => [ '$defs' ],
570 dependencies => [ qw(dependentSchemas dependentRequired) ],
571 },
572 'draft2020-12' => {
573 id => [ '$id' ],
574 definitions => [ '$defs' ],
575 dependencies => [ qw(dependentSchemas dependentRequired) ],
576 '$recursiveAnchor' => [ '$dynamicAnchor' ],
577 '$recursiveRef' => [ '$dynamicRef' ],
578 additionalItems => [ 'items' ],
579 },
580 );
581  
582 # {
583 # $spec_version => {
584 # $vocabulary_class => {
585 # traverse => [ [ $keyword => $subref ], [ ... ] ],
586 # evaluate => [ [ $keyword => $subref ], [ ... ] ],
587 # }
588 # }
589 # }
590 # If we could serialize coderefs, this could be an object attribute;
591 # otherwise, we might as well persist this for the lifetime of the process.
592 46 our $vocabulary_cache = {};
593  
594 42269 ▶sub _traverse_subschema ($self, $schema, $state) {
Subroutine _traverse_subschema: called CC=31 SCAR=34.9
595 42268 delete $state->@{'keyword', grep /^_/, keys %$state};
596  
597 return E($state, 'EXCEPTION: maximum traversal depth (%d) exceeded', $self->max_depth)
598 42268 ▶ if $state->{depth}++ > $self->max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"depth"}++ > $self->max_depth 0 42268 42268
599  
600 42268 push $state->{subschemas}->@*, $state->{traversed_keyword_path}.$state->{keyword_path};
601  
602 42268 my $schema_type = get_type($schema);
603 return 1 if $schema_type eq 'boolean'
604 and ($state->{specification_version} ne 'draft4'
605 42268 ▶ or $state->{keyword_path} =~ m{/(?:additional(?:Items|Properties)|uniqueItems)\z});
Branch: 1 branch
BranchTrueFalseTotal
if $schema_type eq "boolean" and $$state{"specification_version"} ne "draft4" || $$state{"keyword_path"} =~ m[/(?:additional(?:Items|Properties)|uniqueItems)\z]u 7732 34536 42268
Condition100%6/6
$schema_type eq "boolean" and $$state{"specification_version"} ne "draft4" || $$state{"keyword_path"} =~ m[/(?:additional(?:Items|Properties)|uniqueItems)\z]u
!ll&&!rl&&r
3452887732
$$state{"specification_version"} ne "draft4" or $$state{"keyword_path"} =~ m[/(?:additional(?:Items|Properties)|uniqueItems)\z]u
l!l&&r!l&&!r
75961368
MC/DC100%3/3
$schema_type eq "boolean" and $$state{"specification_version"} ne "draft4" || $$state{"keyword_path"} =~ m[/(?:additional(?:Items|Properties)|uniqueItems)\z]u
$schema_type eq "boolean" $$state{"specification_version"} ne "draft4" $$state{"keyword_path"} =~ m[/(?:additional(?:Items|Properties)|uniqueItems)\z]u
Truth table100%4/4
A and B or C
A= $schema_type eq "boolean"B= $$state{"specification_version"} ne "draft4"C= $$state{"keyword_path"} =~ m[/(?:additional(?:Items|Properties)|uniqueItems)\z]u
ABCresult
0XX0
1000
11X1
1011
606  
607 34536 ▶ return E($state, 'invalid schema type: %s', $schema_type) if $schema_type ne 'object';
Branch: 1 branch
BranchTrueFalseTotal
if $schema_type ne "object" 17 34519 34536
608  
609 34519 ▶ return 1 if not keys %$schema;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$schema 1239 33280 34519
610  
611 33280 my $valid = 1;
612 33280 my %unknown_keywords = map +($_ => undef), grep !/^x-/, keys %$schema;
613  
614 # we use an index rather than iterating through the lists directly because the lists of
615 # vocabularies and keywords can change after we have started. However, only the Core vocabulary
616 # and $schema keyword can make this change, and they both come first, therefore a simple index
617 # into the list is sufficient.
618 ALL_KEYWORDS:
619 33280 ▶ for (my $vocab_index = 0; $vocab_index < $state->{vocabularies}->@*; $vocab_index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vocab_index < @{$$state{"vocabularies"}}) 203540 33280 236820
620 203540 my $vocabulary = $state->{vocabularies}[$vocab_index];
621 203540 my $keyword_list;
622  
623 203540 ▶ for (my $keyword_index = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword_index < @{$keyword_list //= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }}) 1685884 203540 1889424
Condition66%2/3
$keyword_list //= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
l!l&&r!l&&!r
16858162036080
MC/DC0%0/2
$keyword_list //= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
$keyword_list do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
Truth table66%2/3
A //= B
A= $keyword_listB= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
ABresult
1X1
011
000
624 $keyword_index < ($keyword_list //= do {
625 48 ▶ use autovivification qw(fetch store);
Subroutine BEGIN: called CC=1 SCAR=0.0
626 $vocabulary_cache->{$state->{specification_version}}{$vocabulary}{traverse} //= [
627 map [ $_ => $vocabulary->can('_traverse_keyword_'.($_ =~ s/^\$//r)) ],
628 $vocabulary->keywords($state->{specification_version})
629 203608 ▶ ];
Condition100%2/2
$$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))]
l!l
203051557
MC/DC100%1/1
$$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"} //= [map([$_, $vocabulary->can('_traverse_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))]
$$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"}
Truth table100%2/2
A
A= $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"traverse"}
Aresult
11
00
630 })->@*;
631 $keyword_index++) {
632 1685884 my ($keyword, $sub) = $keyword_list->[$keyword_index]->@*;
633 1685884 ▶ next if not exists $schema->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword} 1628788 57096 1685884
634  
635 # keywords adjacent to $ref are not evaluated before draft2019-09
636 57096 ▶ next if $keyword ne '$ref' and exists $schema->{'$ref'} and $state->{specification_version} =~ /^draft[467]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $keyword ne "\$ref" and exists $$schema{"\$ref"} and $$state{"specification_version"} =~ /^draft[467]\z/u 36 57060 57096
Condition100%6/6
$keyword ne "\$ref" and exists $$schema{"\$ref"} and $$state{"specification_version"} =~ /^draft[467]\z/u
!ll&&!rl&&r
55572148836
$keyword ne "\$ref" and exists $$schema{"\$ref"}
!ll&&!rl&&r
3283522891524
MC/DC100%3/3
$keyword ne "\$ref" and exists $$schema{"\$ref"} and $$state{"specification_version"} =~ /^draft[467]\z/u
$keyword ne "\$ref" exists $$schema{"\$ref"} $$state{"specification_version"} =~ /^draft[467]\z/u
Truth table100%4/4
A and B and C
A= $keyword ne "\$ref"B= exists $$schema{"\$ref"}C= $$state{"specification_version"} =~ /^draft[467]\z/u
ABCresult
0XX0
10X0
1100
1111
637  
638 57060 delete $unknown_keywords{$keyword};
639 57060 $state->{keyword} = $keyword;
640  
641 57060 my $old_spec_version = $state->{specification_version};
642 57060 my $error_count = $state->{errors}->@*;
643  
644 57060 ▶ if (not $sub->($vocabulary, $schema, $state)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$sub($vocabulary, $schema, $state)) 244 56816 57060
645 die 'traverse result is false but there are no errors (keyword: '.$keyword.')'
646 244 ▶ if $error_count == $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count == @{$$state{"errors"}} 0 244 244
647 244 $valid = 0;
648 244 next;
649 }
650 warn 'traverse result is true but there are errors ('.$keyword.': '.$state->{errors}[-1]->error
651 56816 ▶ if $error_count != $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count != @{$$state{"errors"}} 0 56816 56816
652  
653 # a keyword changed the keyword list for this vocabulary; re-fetch the list before continuing
654 56816 ▶ undef $keyword_list if $state->{specification_version} ne $old_spec_version;
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"specification_version"} ne $old_spec_version 68 56748 56816
655  
656 56816 ▶ if (my $callback = $state->{callbacks}{$keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callback = $$state{"callbacks"}{$keyword}) 4 56812 56816
657 4 $error_count = $state->{errors}->@*;
658  
659 4 ▶ if (not $callback->($schema, $state)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$callback($schema, $state)) 0 4 4
660 die 'callback result is false but there are no errors (keyword: '.$keyword.')'
661 0 ▶ if $error_count == $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count == @{$$state{"errors"}} 0 0 0
662 0 $valid = 0;
663 0 next;
664 }
665 die 'callback result is true but there are errors (keyword: '.$keyword.')'
666 4 ▶ if $error_count != $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count != @{$$state{"errors"}} 0 4 4
667 }
668 }
669 }
670  
671 33280 delete $state->{keyword};
672  
673 33280 ▶ if ($self->strict and keys %unknown_keywords) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict and keys %unknown_keywords) 2 33278 33280
Condition100%3/3
$self->strict and keys %unknown_keywords
!ll&&!rl&&r
33197812
MC/DC100%2/2
$self->strict and keys %unknown_keywords
$self->strict keys %unknown_keywords
Truth table100%3/3
A and B
A= $self->strictB= keys %unknown_keywords
ABresult
0X0
100
111
674 2 ▶ $valid = E($state, 'unknown keyword%s seen in schema: %s', keys %unknown_keywords > 1 ? 's' : '',
Branch: 1 branch
BranchTrueFalseTotal
keys %unknown_keywords > 1 ? : 2 0 2
675 join(', ', sort keys %unknown_keywords));
676 }
677  
678 # check for previously-supported but now removed keywords
679 33280 foreach my $keyword (sort keys $removed_keywords{$state->{specification_version}}->%*) {
680 104085 ▶ next if not exists $schema->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword} 104005 80 104085
681 80 my $message ='no-longer-supported "'.$keyword.'" keyword present (at location "'
682 .canonical_uri($state).'")';
683 80 ▶ if (my $alternates = $removed_keywords{$state->{specification_version}}->{$keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alternates = $removed_keywords{$$state{"specification_version"}}{$keyword}) 80 0 80
684 80 my @list = map '"'.$_.'"', @$alternates;
685 80 ▶ @list = ((map $_.',', @list[0..$#list-1]), $list[-1]) if @list > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @list > 2 0 80 80
686 80 ▶ splice(@list, -1, 0, 'or') if @list > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @list > 1 23 57 80
687 80 $message .= ': this should be rewritten as '.join(' ', @list);
688 }
689 80 carp $message;
690 }
691  
692 33280 return $valid;
693 }
694  
695 34924 ▶sub _evaluate_subschema ($self, $data, $schema, $state) {
Subroutine _evaluate_subschema: called CC=57 SCAR=40.5
696 34923 ▶ croak '_evaluate_subschema called in void context' if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 34923 34923
697  
698 # callers created a new $state for us, so we do not propagate upwards changes to depth, traversed
699 # paths; but annotations, errors are arrayrefs so their contents will be shared
700 34923 ▶ $state->{dynamic_scope} = [ ($state->{dynamic_scope}//[])->@* ];
Condition100%2/2
$$state{"dynamic_scope"} // []
l!l
3489627
MC/DC100%1/1
$$state{"dynamic_scope"} // []
$$state{"dynamic_scope"}
Truth table100%2/2
A
A= $$state{"dynamic_scope"}
Aresult
11
00
701 34923 delete $state->@{'keyword', grep /^_/, keys %$state};
702  
703 abort($state, 'EXCEPTION: maximum evaluation depth (%d) exceeded', $self->max_depth)
704 34923 ▶ if $state->{depth}++ > $self->max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"depth"}++ > $self->max_depth 3 34920 34923
705  
706 34920 my $schema_type = get_type($schema);
707 34920 ▶ return $schema || E($state, 'subschema is false') if $schema_type eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $schema_type eq "boolean" 844 34076 34920
Condition66%2/3
$schema || E($state, 'subschema is false')
l!l&&r!l&&!r
4060438
MC/DC50%1/2
$schema || E($state, 'subschema is false')
$schema E($state, 'subschema is false')
Truth table66%2/3
A || B
A= $schemaB= E($state, 'subschema is false')
ABresult
1X1
011
000
708  
709 # this should never happen, due to checks in traverse
710 34076 ▶ abort($state, 'invalid schema type: %s', $schema_type) if $schema_type ne 'object';
Branch: 1 branch
BranchTrueFalseTotal
if $schema_type ne "object" 0 34076 34076
711  
712 34076 ▶ return 1 if not keys %$schema;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$schema 632 33444 34076
713  
714 # find all schema locations in effect at this data path + uri combination
715 # if any of them are absolute prefix of this schema location, we are in a loop.
716 33444 my $canonical_uri = canonical_uri($state);
717 33444 my $schema_location = $state->{traversed_keyword_path}.$state->{keyword_path};
718 {
719 48 ▶ use autovivification qw(fetch store);
Subroutine BEGIN: called CC=1 SCAR=0.0
720 abort($state, 'EXCEPTION: infinite loop detected (same location evaluated twice)')
721 if grep substr($schema_location, 0, length) eq $_,
722 33444 ▶ keys $state->{seen}{$state->{data_path}}{$canonical_uri}->%*;
Branch: 1 branch
BranchTrueFalseTotal
if grep((substr($schema_location, 0, length $_) eq $_), keys %{$$state{"seen"}{$$state{"data_path"}}{$canonical_uri}}) 1 33443 33444
723 33443 $state->{seen}{$state->{data_path}}{$canonical_uri}{$schema_location}++;
724 }
725  
726 33443 my $valid = 1;
727 33443 my %unknown_keywords = map +($_ => undef), grep !/^x-/, keys %$schema;
728  
729 # set aside annotations collected so far; they are not used in the current scope's evaluation
730 33443 my $parent_annotations = $state->{annotations};
731 33443 $state->{annotations} = [];
732  
733 # in order to collect annotations from applicator keywords only when needed, we twiddle the low
734 # bit if we see a local unevaluated* keyword, and clear it again as we move on to a new data path.
735 # We also set it when _strict_schema_data is set, but only for object data instances.
736 $state->{collect_annotations} |=
737 0+((ref $data eq 'ARRAY' && exists $schema->{unevaluatedItems})
738 || ((my $is_object_data = ref $data eq 'HASH')
739 33443 ▶ && (exists $schema->{unevaluatedProperties} || !!$state->{seen_data_properties})));
Condition100%12/12
ref $data eq 'ARRAY' && exists $$schema{'unevaluatedItems'} || (my $is_object_data = ref $data eq 'HASH') && (exists $$schema{'unevaluatedProperties'} || !!$$state{'seen_data_properties'})
l!l&&r!l&&!r
426109231925
ref $data eq "ARRAY" and exists $$schema{"unevaluatedItems"}
!ll&&!rl&&r
282694748426
my $is_object_data = ref $data eq "HASH" and exists $$schema{"unevaluatedProperties"} || !!$$state{"seen_data_properties"}
!ll&&!rl&&r
21492104331092
exists $$schema{"unevaluatedProperties"} or not !$$state{"seen_data_properties"}
l!l&&r!l&&!r
93615610433
MC/DC100%5/5
ref $data eq 'ARRAY' && exists $$schema{'unevaluatedItems'} || (my $is_object_data = ref $data eq 'HASH') && (exists $$schema{'unevaluatedProperties'} || !!$$state{'seen_data_properties'})
ref $data eq "ARRAY" exists $$schema{"unevaluatedItems"} my $is_object_data = ref $data eq "HASH" exists $$schema{"unevaluatedProperties"} not !$$state{"seen_data_properties"}
Truth table66%6/9
A and B || C and D or E
A= ref $data eq "ARRAY"B= exists $$schema{"unevaluatedItems"}C= my $is_object_data = ref $data eq "HASH"D= exists $$schema{"unevaluatedProperties"}E= not !$$state{"seen_data_properties"}
ABCDEresult
11XXX1
0X11X1
0X1011
1011X1
101011
0X0XX0
0X1000
100XX0
101000
740  
741 # set aside defaults collected so far; we need to keep the subschema's defaults separated in
742 # case they must be discarded due to overall invalidity of the subschema
743 33443 my $defaults = $state->{defaults};
744 33443 ▶ $state->{defaults} = {} if $state->{defaults};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"defaults"} 52 33391 33443
745  
746 # we use an index rather than iterating through the lists directly because the lists of
747 # vocabularies and keywords can change after we have started. However, only the Core vocabulary
748 # and $schema keyword can make this change, and they both come first, therefore a simple index
749 # into the list is sufficient.
750  
751 ALL_KEYWORDS:
752 33443 ▶ for (my $vocab_index = 0; $vocab_index < $state->{vocabularies}->@*; $vocab_index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vocab_index < @{$$state{"vocabularies"}}) 183401 27111 210512
753 183401 my $vocabulary = $state->{vocabularies}[$vocab_index];
754 183401 my $keyword_list;
755  
756 183401 ▶ for (my $keyword_index = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword_index < @{$keyword_list //= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }}) 1512801 177069 1689870
Condition66%2/3
$keyword_list //= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
l!l&&r!l&&!r
15064661834040
MC/DC0%0/2
$keyword_list //= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
$keyword_list do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
Truth table66%2/3
A //= B
A= $keyword_listB= do { BEGIN { $^H{'autovivification'} = '194'; } $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))] }
ABresult
1X1
011
000
757 $keyword_index < ($keyword_list //= do {
758 48 ▶ use autovivification qw(fetch store);
Subroutine BEGIN: called CC=1 SCAR=0.0
759 $vocabulary_cache->{$state->{specification_version}}{$vocabulary}{evaluate} //= [
760 map [ $_ => $vocabulary->can('_eval_keyword_'.($_ =~ s/^\$//r)) ],
761 $vocabulary->keywords($state->{specification_version})
762 183404 ▶ ];
Condition100%2/2
$$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))]
l!l
183026378
MC/DC100%1/1
$$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"} //= [map([$_, $vocabulary->can('_eval_keyword_' . $_ =~ s/^\$//ru)], $vocabulary->keywords($$state{"specification_version"}))]
$$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"}
Truth table100%2/2
A
A= $$JSON::Schema::Modern::vocabulary_cache{$$state{"specification_version"}}{$vocabulary}{"evaluate"}
Aresult
11
00
763 })->@*;
764 $keyword_index++) {
765 1512801 my ($keyword, $sub) = $keyword_list->[$keyword_index]->@*;
766 1512801 ▶ next if not exists $schema->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword} 1452393 60408 1512801
767  
768 # keywords adjacent to $ref are not evaluated before draft2019-09
769 60408 ▶ next if $keyword ne '$ref' and exists $schema->{'$ref'} and $state->{specification_version} =~ /^draft[467]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $keyword ne "\$ref" and exists $$schema{"\$ref"} and $$state{"specification_version"} =~ /^draft[467]\z/u 31 60377 60408
Condition100%6/6
$keyword ne "\$ref" and exists $$schema{"\$ref"} and $$state{"specification_version"} =~ /^draft[467]\z/u
!ll&&!rl&&r
58931144631
$keyword ne "\$ref" and exists $$schema{"\$ref"}
!ll&&!rl&&r
3648552831477
MC/DC100%3/3
$keyword ne "\$ref" and exists $$schema{"\$ref"} and $$state{"specification_version"} =~ /^draft[467]\z/u
$keyword ne "\$ref" exists $$schema{"\$ref"} $$state{"specification_version"} =~ /^draft[467]\z/u
Truth table100%4/4
A and B and C
A= $keyword ne "\$ref"B= exists $$schema{"\$ref"}C= $$state{"specification_version"} =~ /^draft[467]\z/u
ABCresult
0XX0
10X0
1100
1111
770  
771 60377 delete $unknown_keywords{$keyword};
772 60377 ▶ next if not $valid and $state->{short_circuit} and $state->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if not $valid and $$state{"short_circuit"} and $$state{"strict"} 1 60376 60377
Condition83%5/6
not $valid and $$state{"short_circuit"} and $$state{"strict"}
!ll&&!rl&&r
6037601
not $valid and $$state{"short_circuit"}
!ll&&!rl&&r
5853418421
MC/DC66%2/3
not $valid and $$state{"short_circuit"} and $$state{"strict"}
not $valid $$state{"short_circuit"} $$state{"strict"}
Truth table75%3/4
not(A) and B and C
A= not $validB= $$state{"short_circuit"}C= $$state{"strict"}
ABCresult
0XX0
10X0
1100
1111
773  
774 60376 $state->{keyword} = $keyword;
775  
776 60376 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 56259 4117 60376
777 56259 my $old_spec_version = $state->{specification_version};
778 56259 my $error_count = $state->{errors}->@*;
779  
780 56259 try {
781 56259 ▶ if (not $sub->($vocabulary, $data, $schema, $state)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$sub($vocabulary, $data, $schema, $state)) 13317 42898 56215
782 warn 'evaluation result is false but there are no errors (keyword: '.$keyword.')'
783 13317 ▶ if $error_count == $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count == @{$$state{"errors"}} 0 13317 13317
784 13317 $valid = 0;
785  
786 13317 ▶ last ALL_KEYWORDS if $state->{short_circuit} and not $state->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"short_circuit"} and not $$state{"strict"} 6287 7030 13317
Condition100%3/3
$$state{"short_circuit"} and not $$state{"strict"}
!ll&&!rl&&r
702646287
MC/DC100%2/2
$$state{"short_circuit"} and not $$state{"strict"}
$$state{"short_circuit"} not $$state{"strict"}
Truth table100%3/3
A and not(B)
A= $$state{"short_circuit"}B= not $$state{"strict"}
ABresult
0X0
100
111
787 7030 next;
788 }
789  
790 warn 'evaluation result is true but there are errors (keyword: '.$keyword.')'
791 42898 ▶ if $error_count != $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count != @{$$state{"errors"}} 0 42898 42898
792 }
793 catch ($e) {
794 44 ▶ die $e if $e->$_isa('JSON::Schema::Modern::Error');
Branch: 1 branch
BranchTrueFalseTotal
if $e->$_isa("JSON::Schema::Modern::Error") 42 2 44
795 2 abort($state, 'EXCEPTION: '.$e);
796 }
797  
798 # a keyword changed the keyword list for this vocabulary; re-fetch the list before continuing
799 42898 ▶ undef $keyword_list if $state->{specification_version} ne $old_spec_version;
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"specification_version"} ne $old_spec_version 3 42895 42898
800 }
801  
802 47015 ▶ if (my $callback = ($state->{callbacks}//{})->{$keyword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callback = ($$state{"callbacks"} // {})->{$keyword}) 24 46991 47015
Condition100%2/2
$$state{"callbacks"} // {}
l!l
4698629
MC/DC100%1/1
$$state{"callbacks"} // {}
$$state{"callbacks"}
Truth table100%2/2
A
A= $$state{"callbacks"}
Aresult
11
00
803 24 my $error_count = $state->{errors}->@*;
804  
805 24 ▶ if (not $callback->($data, $schema, $state)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$callback($data, $schema, $state)) 2 22 24
806 warn 'callback result is false but there are no errors (keyword: '.$keyword.')'
807 2 ▶ if $error_count == $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count == @{$$state{"errors"}} 0 2 2
808 2 $valid = 0;
809  
810 2 ▶ last ALL_KEYWORDS if $state->{short_circuit} and not $state->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $$state{"short_circuit"} and not $$state{"strict"} 1 1 2
Condition66%2/3
$$state{"short_circuit"} and not $$state{"strict"}
!ll&&!rl&&r
101
MC/DC50%1/2
$$state{"short_circuit"} and not $$state{"strict"}
$$state{"short_circuit"} not $$state{"strict"}
Truth table66%2/3
A and not(B)
A= $$state{"short_circuit"}B= not $$state{"strict"}
ABresult
0X0
100
111
811 1 next;
812 }
813 warn 'callback result is true but there are errors (keyword: '.$keyword.')'
814 22 ▶ if $error_count != $state->{errors}->@*;
Branch: 1 branch
BranchTrueFalseTotal
if $error_count != @{$$state{"errors"}} 0 22 22
815 }
816 }
817 }
818  
819 33399 delete $state->{keyword};
820  
821 33399 ▶ if ($state->{strict} and keys %unknown_keywords) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$state{"strict"} and keys %unknown_keywords) 3 33396 33399
Condition100%3/3
$$state{"strict"} and keys %unknown_keywords
!ll&&!rl&&r
331991973
MC/DC100%2/2
$$state{"strict"} and keys %unknown_keywords
$$state{"strict"} keys %unknown_keywords
Truth table100%3/3
A and B
A= $$state{"strict"}B= keys %unknown_keywords
ABresult
0X0
100
111
822 3 ▶ abort($state, 'unknown keyword%s seen in schema: %s', keys %unknown_keywords > 1 ? 's' : '',
Branch: 1 branch
BranchTrueFalseTotal
keys %unknown_keywords > 1 ? : 1 2 3
823 join(', ', sort keys %unknown_keywords));
824 }
825  
826 # Note: we can remove all of this entirely and just rely on strict mode when we (eventually!) remove
827 # the traverse phase and replace with evaluate-against-metaschema.
828 33396 ▶ if ($state->{seen_data_properties} and $is_object_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$state{"seen_data_properties"} and $is_object_data) 156 33240 33396
Condition100%3/3
$$state{"seen_data_properties"} and $is_object_data
!ll&&!rl&&r
3320931156
MC/DC100%2/2
$$state{"seen_data_properties"} and $is_object_data
$$state{"seen_data_properties"} $is_object_data
Truth table100%3/3
A and B
A= $$state{"seen_data_properties"}B= $is_object_data
ABresult
0X0
100
111
829 # record the locations of all local properties
830 $state->{seen_data_properties}{jsonp($state->{data_path}, $_)} |= 0
831 156 foreach grep !/^x-/, keys %$data;
832  
833 156 my @evaluated_properties = map do {
834 577 my $keyword = $_->{keyword};
835 (grep $keyword eq $_, qw(properties additionalProperties patternProperties unevaluatedProperties))
836 577 ▶ ? $_->{annotation}->@* : ();
Branch: 1 branch
BranchTrueFalseTotal
grep(($keyword eq $_), ('properties', 'additionalProperties', 'patternProperties', 'unevaluatedProperties')) ? : 287 290 577
837 }, local_annotations($state);
838  
839 # tick off properties that were recognized by this subschema
840 156 $state->{seen_data_properties}{jsonp($state->{data_path}, $_)} |= 1 foreach @evaluated_properties;
841  
842 # weird! the draft4 metaschema doesn't know about '$ref' at all!
843 $state->{seen_data_properties}{$state->{data_path}.'/$ref'} |= 1
844 156 ▶ if exists $data->{'$ref'} and $state->{specification_version} eq 'draft4';
Branch: 1 branch
BranchTrueFalseTotal
if exists $$data{"\$ref"} and $$state{"specification_version"} eq "draft4" 2 154 156
Condition66%2/3
exists $$data{"\$ref"} and $$state{"specification_version"} eq "draft4"
!ll&&!rl&&r
15402
MC/DC50%1/2
exists $$data{"\$ref"} and $$state{"specification_version"} eq "draft4"
exists $$data{"\$ref"} $$state{"specification_version"} eq "draft4"
Truth table66%2/3
A and B
A= exists $$data{"\$ref"}B= $$state{"specification_version"} eq "draft4"
ABresult
0X0
100
111
845 }
846  
847 33396 ▶ if ($valid and $state->{collect_annotations} and $state->{specification_version} !~ /^draft(?:[467]|2019-09)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid and $$state{"collect_annotations"} and not $$state{"specification_version"} =~ /^draft(?:[467]|2019-09)\z/u) 975 32421 33396
Condition100%6/6
$valid and $$state{"collect_annotations"} and not $$state{"specification_version"} =~ /^draft(?:[467]|2019-09)\z/u
!ll&&!rl&&r
31654767975
$valid and $$state{"collect_annotations"}
!ll&&!rl&&r
13151185031742
MC/DC100%3/3
$valid and $$state{"collect_annotations"} and not $$state{"specification_version"} =~ /^draft(?:[467]|2019-09)\z/u
$valid $$state{"collect_annotations"} not $$state{"specification_version"} =~ /^draft(?:[467]|2019-09)\z/u
Truth table100%4/4
A and B and not(C)
A= $validB= $$state{"collect_annotations"}C= not $$state{"specification_version"} =~ /^draft(?:[467]|2019-09)\z/u
ABCresult
0XX0
10X0
1100
1111
848 annotate_self(+{ %$state, keyword => $_, _unknown => 1 }, $schema)
849 975 foreach sort keys %unknown_keywords;
850 }
851  
852 # only keep new annotations if schema is valid
853 33396 ▶ push $parent_annotations->@*, $state->{annotations}->@* if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 20245 13151 33396
854  
855 # only keep new defaults if schema is valid
856 $defaults->@{keys $state->{defaults}->%*} = values $state->{defaults}->%*
857 33396 ▶ if $valid and $state->{defaults};
Branch: 1 branch
BranchTrueFalseTotal
if $valid and $$state{"defaults"} 41 33355 33396
Condition100%3/3
$valid and $$state{"defaults"}
!ll&&!rl&&r
131512020441
MC/DC100%2/2
$valid and $$state{"defaults"}
$valid $$state{"defaults"}
Truth table100%3/3
A and B
A= $validB= $$state{"defaults"}
ABresult
0X0
100
111
858  
859 33396 return $valid;
860 }
861  
862 46 has _resource_index => (
863 is => 'bare',
864 isa => Map[my $resource_key_type = Str->where('!/#/') => my $resource_type = Dict[
865 canonical_uri => (InstanceOf['Mojo::URL'])->where(q{not defined $_->fragment}),
866 path => json_pointer_type, # JSON pointer relative to the document root
867 specification_version => my $spec_version_type = Enum(SPECIFICATION_VERSIONS_SUPPORTED),
868 document => InstanceOf['JSON::Schema::Modern::Document'],
869 # the vocabularies used when evaluating instance data against schema
870 vocabularies => ArrayRef[my $vocabulary_class_type = ClassName->where(q{$_->DOES('JSON::Schema::Modern::Vocabulary')})],
871 anchors => Optional[HashRef[Dict[
872 canonical_uri => canonical_uri_type, # equivalent uri with json pointer fragment
873 path => json_pointer_type, # JSON pointer relative to the document root
874 dynamic => Optional[Bool],
875 ]]],
876 Slurpy[HashRef[Undef]], # no other fields allowed
877 ]],
878 );
879  
880 44110 ▶sub _get_resource {
Subroutine _get_resource: called CC=3 SCAR=11.1
881 44109 ▶ die 'bad resource: ', $_[1] if $_[1] =~ /#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /#/u 0 44109 44109
882 44109 ▶ ($_[0]->{_resource_index}//{})->{$_[1]}
Condition100%2/2
$_[0]{"_resource_index"} // {}
l!l
43922187
MC/DC100%1/1
$_[0]{"_resource_index"} // {}
$_[0]{"_resource_index"}
Truth table100%2/2
A
A= $_[0]{"_resource_index"}
Aresult
11
00
883 }
884  
885 # does not check for duplicate entries, or for malformed uris
886 104 ▶sub _add_resources_unsafe {
Subroutine _add_resources_unsafe: called CC=2 SCAR=6.9
887 48 ▶ use autovivification 'store';
Subroutine BEGIN: called CC=1 SCAR=0.0
888 $_[0]->{_resource_index}{$resource_key_type->($_->[0])} = $resource_type->($_->[1])
889 103 foreach pairs @_[1..$#_];
890 }
891 25 ▶sub _resource_index { ($_[0]->{_resource_index}//{})->%* }
Subroutine _resource_index: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"_resource_index"} // {}
l!l
250
MC/DC0%0/1
$_[0]{"_resource_index"} // {}
$_[0]{"_resource_index"}
Truth table50%1/2
A
A= $_[0]{"_resource_index"}
Aresult
11
00
892 17388 ▶sub _canonical_resources { values(($_[0]->{_resource_index}//{})->%*) }
Subroutine _canonical_resources: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{"_resource_index"} // {}
l!l
17200188
MC/DC100%1/1
$_[0]{"_resource_index"} // {}
$_[0]{"_resource_index"}
Truth table100%2/2
A
A= $_[0]{"_resource_index"}
Aresult
11
00
893 2124 ▶sub _resource_pairs { pairs(($_[0]->{_resource_index}//{})->%*) }
Subroutine _resource_pairs: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"_resource_index"} // {}
l!l
21240
MC/DC0%0/1
$_[0]{"_resource_index"} // {}
$_[0]{"_resource_index"}
Truth table50%1/2
A
A= $_[0]{"_resource_index"}
Aresult
11
00
894  
895 18995 ▶sub _add_resource ($self, @kvs) {
Subroutine _add_resource: called CC=10 SCAR=23.1
896 0 foreach my $pair (sort { $a->[0] cmp $b->[0] } pairs @kvs) {
897 18994 my ($canonical_uri, $resource) = @$pair;
898  
899 18994 ▶ if (my $existing = $self->_get_resource($canonical_uri)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $existing = $self->_get_resource($canonical_uri)) { } 17083 1911 18994
elsif (JSON::Schema::Modern::Utilities::get_schema_filename($canonical_uri)) { } 2 1909 1911
900 # we allow overwriting canonical_uri = '' to allow for ad hoc evaluation of schemas that
901 # lack all identifiers altogether, but preserve other resources from the original document
902 17083 ▶ if ($canonical_uri ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($canonical_uri ne "") 859 16224 17083
903 my @diffs = (
904 ($existing->{path} eq $resource->{path} ? () : 'path'),
905 ($existing->{canonical_uri} eq $resource->{canonical_uri} ? () : 'canonical_uri'),
906 ($existing->{specification_version} eq $resource->{specification_version} ? () : 'specification_version'),
907 859 ▶ (refaddr($existing->{document}) == refaddr($resource->{document}) ? () : 'refaddr'));
Branch: 4 branches
BranchTrueFalseTotal
$$existing{'path'} eq $$resource{'path'} ? : 856 3 859
$$existing{'canonical_uri'} eq $$resource{'canonical_uri'} ? : 856 3 859
$$existing{'specification_version'} eq $$resource{'specification_version'} ? : 859 0 859
builtin::refaddr($$existing{'document'}) == builtin::refaddr($$resource{'document'}) ? : 849 10 859
908 859 ▶ next if not @diffs;
Branch: 1 branch
BranchTrueFalseTotal
unless @diffs 849 10 859
909 10 croak 'uri "'.$canonical_uri.'" conflicts with an existing schema resource: documents differ by ',
910 join(', ', @diffs);
911 }
912 }
913 elsif (JSON::Schema::Modern::Utilities::get_schema_filename($canonical_uri)) {
914 2 croak 'uri "'.$canonical_uri.'" conflicts with an existing cached schema resource';
915 }
916  
917 48 ▶ use autovivification 'store';
Subroutine BEGIN: called CC=1 SCAR=0.0
918 18133 $self->{_resource_index}{$resource_key_type->($canonical_uri)} = $resource_type->($resource);
919 }
920 }
921  
922 # $vocabulary uri (not its $id!) => [ specification_version, class ]
923 has _vocabulary_classes => (
924 is => 'bare',
925 isa => HashRef[
926 my $vocabulary_type = Tuple[
927 $spec_version_type,
928 $vocabulary_class_type,
929 ]
930 ],
931 reader => '__vocabulary_classes',
932 lazy => 1,
933 23 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
934 +{
935 22 ▶ map do { my $class = $_; pairmap { $a => [ $b, $class ] } $class->vocabulary },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
936 map load_module('JSON::Schema::Modern::Vocabulary::'.$_),
937 qw(Core Applicator Validation FormatAssertion FormatAnnotation Content MetaData Unevaluated)
938 }
939 },
940 46 );
941  
942 107 ▶sub _get_vocabulary_class { $_[0]->__vocabulary_classes->{$_[1]} }
Subroutine _get_vocabulary_class: called CC=1 SCAR=0.0
943  
944 11 ▶sub add_vocabulary ($self, $classname) {
Subroutine add_vocabulary: called CC=4 SCAR=13.9
Pod documented
945 10 ▶ return if grep $_->[1] eq $classname, values $self->__vocabulary_classes->%*;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($our $_[1] eq $classname), values %{$self->__vocabulary_classes}) 0 10 10
946  
947 10 $vocabulary_class_type->(load_module($classname));
948  
949 # uri => version, uri => version
950 7 foreach my $pair (pairs $classname->vocabulary) {
951 7 my ($uri_string, $spec_version) = @$pair;
952 7 Str->where(q{my $uri = Mojo::URL->new($_); $uri->is_abs && !defined $uri->fragment})->($uri_string);
953 6 $spec_version_type->($spec_version);
954  
955 4 ▶ croak 'keywords starting with "$" are reserved for core and cannot be used'
Branch: 1 branch
BranchTrueFalseTotal
if grep /^\$/u, $classname->keywords 1 3 4
956 if grep /^\$/, $classname->keywords;
957  
958 3 $self->{_vocabulary_classes}{$uri_string} = $vocabulary_type->([ $spec_version, $classname ]);
959 }
960 }
961  
962 # $schema uri => [ specification_version, [ vocab classes, in evaluation order ] ].
963 has _metaschema_vocabulary_classes => (
964 is => 'bare',
965 isa => HashRef[
966 my $mvc_type = Tuple[
967 $spec_version_type,
968 ArrayRef[$vocabulary_class_type],
969 ]
970 ],
971 reader => '__metaschema_vocabulary_classes',
972 lazy => 1,
973 229 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
974 38 ▶ my @modules = map load_module('JSON::Schema::Modern::Vocabulary::'.$_),
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
975 qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated);
976 +{
977 'https://json-schema.org/draft/2020-12/schema' => [ 'draft2020-12', [ @modules ] ],
978 228 do { pop @modules; () }, # remove Unevaluated
979 'https://json-schema.org/draft/2019-09/schema' => [ 'draft2019-09', [ @modules ] ],
980 'http://json-schema.org/draft-07/schema' => [ 'draft7', [ @modules ] ],
981 228 do { splice @modules, 4, 1; () }, # remove Content
982 'http://json-schema.org/draft-06/schema' => [ 'draft6', \@modules ],
983 'http://json-schema.org/draft-04/schema' => [ 'draft4', \@modules ],
984 },
985 },
986 46 );
987  
988 30417 ▶sub _get_metaschema_vocabulary_classes { $_[0]->__metaschema_vocabulary_classes->{$_[1] =~ s/#\z//r} }
Subroutine _get_metaschema_vocabulary_classes: called CC=1 SCAR=0.0
989 5862 ▶sub _set_metaschema_vocabulary_classes { $_[0]->__metaschema_vocabulary_classes->{$_[1] =~ s/#\z//r} = $mvc_type->($_[2]) }
Subroutine _set_metaschema_vocabulary_classes: called CC=1 SCAR=0.0
990 4 ▶sub __all_metaschema_vocabulary_classes { values $_[0]->__metaschema_vocabulary_classes->%* }
Subroutine __all_metaschema_vocabulary_classes: called CC=1 SCAR=0.0
991  
992 # translate vocabulary URIs into classes, caching the results (if any)
993 54 ▶sub _fetch_vocabulary_data ($self, $state, $schema_info) {
Subroutine _fetch_vocabulary_data: called CC=18 SCAR=28.9
994 53 ▶ if (not exists $schema_info->{schema}{'$vocabulary'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$schema_info{"schema"}{"\$vocabulary"}) 2 51 53
995 # "If "$vocabulary" is absent, an implementation MAY determine behavior based on the meta-schema
996 # if it is recognized from the URI value of the referring schema's "$schema" keyword."
997 2 my $metaschema_uri = $self->METASCHEMA_URIS->{$schema_info->{specification_version}};
998 2 return $self->_get_metaschema_vocabulary_classes($metaschema_uri)->@*;
999 }
1000  
1001 51 my $valid = 1;
1002 # Core §8.1.2-6: "The "$vocabulary" keyword SHOULD be used in the root schema of any schema
1003 # document intended for use as a meta-schema. It MUST NOT appear in subschemas."
1004 51 ▶ $valid = E($state, '$vocabulary can only appear at the document root') if length $schema_info->{document_path};
Branch: 1 branch
BranchTrueFalseTotal
if length $$schema_info{"document_path"} 1 50 51
1005 51 ▶ $valid = E($state, 'metaschemas must have an $id') if not exists $schema_info->{schema}{'$id'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema_info{"schema"}{"\$id"} 1 50 51
1006  
1007 51 ▶ return (undef, []) if not $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 2 49 51
1008  
1009 49 my @vocabulary_classes;
1010  
1011 49 foreach my $uri (sort keys $schema_info->{schema}{'$vocabulary'}->%*) {
1012 105 my $class_info = $self->_get_vocabulary_class($uri);
1013 $valid = E({ %$state, _keyword_path_suffix => $uri }, '"%s" is not a known vocabulary', $uri), next
1014 105 ▶ if $schema_info->{schema}{'$vocabulary'}{$uri} and not $class_info;
Branch: 1 branch
BranchTrueFalseTotal
if $$schema_info{"schema"}{"\$vocabulary"}{$uri} and not $class_info 8 97 105
Condition100%3/3
$$schema_info{"schema"}{"\$vocabulary"}{$uri} and not $class_info
!ll&&!rl&&r
14838
MC/DC100%2/2
$$schema_info{"schema"}{"\$vocabulary"}{$uri} and not $class_info
$$schema_info{"schema"}{"\$vocabulary"}{$uri} not $class_info
Truth table100%3/3
A and not(B)
A= $$schema_info{"schema"}{"\$vocabulary"}{$uri}B= not $class_info
ABresult
0X0
100
111
1015  
1016 97 ▶ next if not $class_info; # vocabulary is not known, but marked as false in the metaschema
Branch: 1 branch
BranchTrueFalseTotal
unless $class_info 8 89 97
1017  
1018 89 my ($spec_version, $class) = @$class_info;
1019 $valid = E({ %$state, _keyword_path_suffix => $uri }, '"%s" uses %s, but the metaschema itself uses %s',
1020 $uri, $spec_version, $schema_info->{specification_version}), next
1021 89 ▶ if $spec_version ne $schema_info->{specification_version};
Branch: 1 branch
BranchTrueFalseTotal
if $spec_version ne $$schema_info{"specification_version"} 6 83 89
1022  
1023 83 push @vocabulary_classes, $class;
1024 }
1025  
1026 @vocabulary_classes = sort {
1027 49 ▶ $a->evaluation_order <=> $b->evaluation_order
Branch: 1 branch
BranchTrueFalseTotal
$a->evaluation_order == 999 ? : 0 1 1
Condition66%2/3
$a->evaluation_order <=> $b->evaluation_order || ($a->evaluation_order == 999 ? 0 : ($valid = E($state, '%s and %s have a conflicting evaluation_order', sort($a, $b))))
l!l&&r!l&&!r
4801
MC/DC50%1/2
$a->evaluation_order <=> $b->evaluation_order || ($a->evaluation_order == 999 ? 0 : ($valid = E($state, '%s and %s have a conflicting evaluation_order', sort($a, $b))))
$a->evaluation_order <=> $b->evaluation_order ($a->evaluation_order == 999 ? 0 : ($valid = E($state, '%s and %s have a conflicting evaluation_order', sort($a, $b))))
Truth table66%2/3
A || B
A= $a->evaluation_order <=> $b->evaluation_orderB= ($a->evaluation_order == 999 ? 0 : ($valid = E($state, '%s and %s have a conflicting evaluation_order', sort($a, $b))))
ABresult
1X1
011
000
1028 || ($a->evaluation_order == 999 ? 0
1029 : ($valid = E($state, '%s and %s have a conflicting evaluation_order', sort $a, $b)))
1030 } @vocabulary_classes;
1031  
1032 49 ▶ $valid = E($state, 'the first vocabulary (by evaluation_order) must be Core')
Branch: 1 branch
BranchTrueFalseTotal
if ($vocabulary_classes[0] // "") ne "JSON::Schema::Modern::Vocabulary::Core" 9 40 49
Condition100%2/2
$vocabulary_classes[0] // ""
l!l
418
MC/DC100%1/1
$vocabulary_classes[0] // ""
$vocabulary_classes[0]
Truth table100%2/2
A
A= $vocabulary_classes[0]
Aresult
11
00
1033 if ($vocabulary_classes[0]//'') ne 'JSON::Schema::Modern::Vocabulary::Core';
1034  
1035 49 my %seen_keyword;
1036 49 foreach my $class (@vocabulary_classes) {
1037 83 foreach my $keyword ($class->keywords($schema_info->{specification_version})) {
1038 $valid = E($state, '%s keyword "%s" conflicts with keyword of the same name from %s',
1039 $class, $keyword, $seen_keyword{$keyword})
1040 794 ▶ if $seen_keyword{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_keyword{$keyword} 1 793 794
1041 794 $seen_keyword{$keyword} = $class;
1042 }
1043 }
1044  
1045 49 ▶ return ($schema_info->{specification_version}, $valid ? \@vocabulary_classes : []);
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 32 17 49
1046 }
1047  
1048 # used for determining a default '$schema' keyword where there is none
1049 # these are also normalized as this is how we cache them
1050 48 ▶use constant METASCHEMA_URIS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1051 'draft2020-12' => 'https://json-schema.org/draft/2020-12/schema',
1052 'draft2019-09' => 'https://json-schema.org/draft/2019-09/schema',
1053 'draft7' => 'http://json-schema.org/draft-07/schema',
1054 'draft6' => 'http://json-schema.org/draft-06/schema',
1055 'draft4' => 'http://json-schema.org/draft-04/schema',
1056 48 };
1057  
1058 # for internal use only. files are under share/
1059 48 ▶use constant _CACHED_METASCHEMAS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1060 'https://json-schema.org/draft/2020-12/meta/applicator' => 'draft2020-12/meta/applicator.json',
1061 'https://json-schema.org/draft/2020-12/meta/content' => 'draft2020-12/meta/content.json',
1062 'https://json-schema.org/draft/2020-12/meta/core' => 'draft2020-12/meta/core.json',
1063 'https://json-schema.org/draft/2020-12/meta/format-annotation' => 'draft2020-12/meta/format-annotation.json',
1064 'https://json-schema.org/draft/2020-12/meta/format-assertion' => 'draft2020-12/meta/format-assertion.json',
1065 'https://json-schema.org/draft/2020-12/meta/meta-data' => 'draft2020-12/meta/meta-data.json',
1066 'https://json-schema.org/draft/2020-12/meta/unevaluated' => 'draft2020-12/meta/unevaluated.json',
1067 'https://json-schema.org/draft/2020-12/meta/validation' => 'draft2020-12/meta/validation.json',
1068 'https://json-schema.org/draft/2020-12/output/schema' => 'draft2020-12/output/schema.json',
1069 'https://json-schema.org/draft/2020-12/schema' => 'draft2020-12/schema.json',
1070  
1071 'https://json-schema.org/draft/2019-09/meta/applicator' => 'draft2019-09/meta/applicator.json',
1072 'https://json-schema.org/draft/2019-09/meta/content' => 'draft2019-09/meta/content.json',
1073 'https://json-schema.org/draft/2019-09/meta/core' => 'draft2019-09/meta/core.json',
1074 'https://json-schema.org/draft/2019-09/meta/format' => 'draft2019-09/meta/format.json',
1075 'https://json-schema.org/draft/2019-09/meta/meta-data' => 'draft2019-09/meta/meta-data.json',
1076 'https://json-schema.org/draft/2019-09/meta/validation' => 'draft2019-09/meta/validation.json',
1077 'https://json-schema.org/draft/2019-09/output/schema' => 'draft2019-09/output/schema.json',
1078 'https://json-schema.org/draft/2019-09/schema' => 'draft2019-09/schema.json',
1079  
1080 # trailing # is omitted because we always cache documents by its canonical (fragmentless) URI
1081 'http://json-schema.org/draft-07/schema' => 'draft7/schema.json',
1082 'http://json-schema.org/draft-06/schema' => 'draft6/schema.json',
1083 'http://json-schema.org/draft-04/schema' => 'draft4/schema.json',
1084 48 };
1085  
1086 # simple runtime-wide cache of metaschema document objects that are sourced from disk
1087 46 my $metaschema_cache = {};
1088  
1089 {
1090 46 my $share_dir = dist_dir('JSON-Schema-Modern');
1091 JSON::Schema::Modern::Utilities::register_schema($_, $share_dir.'/'._CACHED_METASCHEMAS->{$_})
1092 46 foreach keys _CACHED_METASCHEMAS->%*;
1093 }
1094  
1095 # returns the same as _get_resource
1096 24475 ▶sub _get_or_load_resource ($self, $uri) {
Subroutine _get_or_load_resource: called CC=3 SCAR=11.0
1097 24474 my $resource = $self->_get_resource($uri);
1098 24474 ▶ return $resource if $resource;
Branch: 1 branch
BranchTrueFalseTotal
if $resource 24360 114 24474
1099  
1100 114 ▶ if (my $document = load_cached_document($self, $uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $document = load_cached_document($self, $uri)) 103 11 114
1101 103 return $self->_get_resource($uri);
1102 }
1103  
1104 # TODO:
1105 # - load from network or disk
1106  
1107 11 return;
1108 };
1109  
1110 # returns information necessary to use a schema found at a particular URI or uri-reference:
1111 # - schema: a schema (which may not be at a document root)
1112 # - canonical_uri: the canonical uri for that schema,
1113 # - document: the JSON::Schema::Modern::Document object that holds that schema
1114 # - document_path: the path relative to the document root for this schema
1115 # - specification_version: the specification version that applies to this schema
1116 # - vocabularies: the vocabularies to use when considering schema keywords
1117 # creates a Document and adds it to the resource index, if not already present.
1118 24237 ▶sub _fetch_from_uri ($self, $uri_reference) {
Subroutine _fetch_from_uri: called CC=14 SCAR=26.4
1119 24236 ▶ $uri_reference = Mojo::URL->new($uri_reference) if not ref $uri_reference;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri_reference 130 24106 24236
1120  
1121 # this is *a* resource that would contain our desired location, but may not be the closest one
1122 24236 my $resource = $self->_get_or_load_resource($uri_reference->clone->fragment(undef));
1123 24236 ▶ return if not $resource;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource 11 24225 24236
1124  
1125 24225 my $fragment = $uri_reference->fragment;
1126 24225 ▶ if (not length($fragment) or $fragment =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $fragment or $fragment =~ m[^/]u) { } 23644 581 24225
Condition100%3/3
not length $fragment or $fragment =~ m[^/]u
l!l&&r!l&&!r
215182126581
MC/DC100%2/2
not length $fragment or $fragment =~ m[^/]u
not length $fragment $fragment =~ m[^/]u
Truth table100%3/3
not(A) or B
A= not length $fragmentB= $fragment =~ m[^/]u
ABresult
1X1
011
000
1127 23644 ▶ my $subschema = $resource->{document}->get(my $document_path = $resource->{path}.($fragment//''));
Condition100%2/2
$fragment // ""
l!l
499018654
MC/DC100%1/1
$fragment // ""
$fragment
Truth table100%2/2
A
A= $fragment
Aresult
11
00
1128 23644 ▶ return if not defined $subschema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subschema 2 23642 23644
1129  
1130 23642 my $closest_resource;
1131 23642 ▶ if (not length $fragment) { # we already have the canonical resource root
Branch: 1 branch
BranchTrueFalseTotal
if (not length $fragment) { } 21518 2124 23642
1132 21518 $closest_resource = [ undef, $resource ];
1133 }
1134 else {
1135 # determine the canonical uri by finding the closest schema resource(s)
1136 2124 my $doc_addr = refaddr($resource->{document});
1137 my @closest_resources =
1138 505 ▶ sort { length($b->[1]{path}) <=> length($a->[1]{path}) } # sort by length, descending
Condition83%5/6
!length($our $_[1]{'path'}) || length $document_path && $document_path =~ m[^\Q$our $_[1]{'path'}\E(?:/|\z)]u
l!l&&r!l&&!r
2157452306
length $document_path and $document_path =~ m[^\Q$our $_[1]{"path"}\E(?:/|\z)]u
!ll&&!rl&&r
0306452
MC/DC66%2/3
!length($our $_[1]{'path'}) || length $document_path && $document_path =~ m[^\Q$our $_[1]{'path'}\E(?:/|\z)]u
!length($our $_[1]{'path'}) length $document_path $document_path =~ m[^\Q$our $_[1]{"path"}\E(?:/|\z)]u
Truth table75%3/4
not(A) || B and C
A= !length($our $_[1]{'path'})B= length $document_pathC= $document_path =~ m[^\Q$our $_[1]{"path"}\E(?:/|\z)]u
ABCresult
1XX1
0111
00X0
0100
1139 grep !length($_->[1]{path}) # document root
1140 || length($document_path)
1141 && $document_path =~ m{^\Q$_->[1]{path}\E(?:/|\z)}, # path is above desired location
1142 2124 grep refaddr($_->[1]{document}) == $doc_addr, # in same document
1143 $self->_resource_pairs;
1144  
1145 # now whittle down to all the resources with the same document path as the first candidate
1146 2124 ▶ if (@closest_resources > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@closest_resources > 1) 465 1659 2124
1147 # find the resource key that most closely matches the original query uri, by matching prefixes
1148 465 my $match = $uri_reference.'';
1149 @closest_resources =
1150 26 sort { _prefix_match_length($b->[0], $match) <=> _prefix_match_length($a->[0], $match) }
1151 grep $_->[1]{path} eq $closest_resources[0]->[1]{path},
1152 465 @closest_resources;
1153 }
1154  
1155 2124 $closest_resource = $closest_resources[0];
1156 }
1157  
1158 my $canonical_uri = $closest_resource->[1]{canonical_uri}->clone
1159 23642 ->fragment(substr($document_path, length($closest_resource->[1]{path})));
1160 23642 ▶ $canonical_uri->fragment(undef) if not length($canonical_uri->fragment);
Branch: 1 branch
BranchTrueFalseTotal
unless length $canonical_uri->fragment 21790 1852 23642
1161  
1162 return {
1163 schema => $subschema,
1164 canonical_uri => $canonical_uri,
1165 document_path => $document_path,
1166 23642 $closest_resource->[1]->%{qw(document specification_version vocabularies)}, # reference, not copy
1167 };
1168 }
1169 else { # we are following a URI with a plain-name fragment
1170 581 ▶ return if not my $subresource = ($resource->{anchors}//{})->{$fragment};
Branch: 1 branch
BranchTrueFalseTotal
unless my $subresource = ($$resource{"anchors"} // {})->{$fragment} 2 579 581
Condition100%2/2
$$resource{"anchors"} // {}
l!l
5801
MC/DC100%1/1
$$resource{"anchors"} // {}
$$resource{"anchors"}
Truth table100%2/2
A
A= $$resource{"anchors"}
Aresult
11
00
1171 return {
1172 schema => $resource->{document}->get($subresource->{path}),
1173 canonical_uri => $subresource->{canonical_uri}, # this is *not* the anchor-containing URI
1174 document_path => $subresource->{path},
1175 579 $resource->%{qw(document specification_version vocabularies)}, # reference, not copy
1176 };
1177 }
1178 }
1179  
1180 # given two strings, determines the number of characters in common, starting from the first
1181 # character
1182 53 ▶sub _prefix_match_length ($x, $y) {
Subroutine _prefix_match_length: called CC=3 SCAR=11.1
1183 52 my $len = min(length($x), length($y));
1184 52 foreach my $pos (0..$len) {
1185 1456 ▶ return $pos if substr($x, $pos, 1) ne substr($y, $pos, 1);
Branch: 1 branch
BranchTrueFalseTotal
if substr($x, $pos, 1) ne substr($y, $pos, 1) 52 1404 1456
1186 }
1187 0 return $len;
1188 }
1189  
1190 48 ▶use constant _JSON_BACKEND => JSON::Schema::Modern::Utilities::_JSON_BACKEND;
Subroutine BEGIN: called CC=1 SCAR=0.0
1191  
1192 # used for internal encoding as well (when caching serialized schemas)
1193 has _json_decoder => (
1194 is => 'ro',
1195 isa => HasMethods[qw(encode decode)],
1196 lazy => 1,
1197 164 ▶ default => sub { _JSON_BACKEND->new->allow_nonref(1)->canonical(1)->utf8(1)->allow_bignum(1)->convert_blessed(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1198 46 );
1199  
1200 # since media types are case-insensitive, all legacy type names must be casefolded on insertion.
1201 has _media_type => (
1202 is => 'bare',
1203 isa => ArrayRef[my $media_type_type = Str->where(q{$_ eq CORE::fc($_)})],
1204 reader => '__media_type',
1205 lazy => 1,
1206 245 ▶ default => sub ($self) { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
1207 46 );
1208  
1209 46 my ($warn_add_media_type, $warn_get_media_type); # we will warn just once
1210  
1211 # deprecated interface
1212 4 ▶sub add_media_type ($self, $media_type, $decoder) {
Subroutine add_media_type: called CC=2 SCAR=6.9
Pod documented
1213 4 $media_type_type->($media_type);
1214  
1215 4 ▶ carp '$jsm->add_media_type is deprecated; use the function in JSON::Schema::Modern::Utilities instead' if not $warn_add_media_type++;
Branch: 1 branch
BranchTrueFalseTotal
unless $warn_add_media_type++ 1 3 4
1216  
1217 # backcompat preservation: add to the global registry, and remove it again when this object goes
1218 # out of scope
1219 4 JSON::Schema::Modern::Utilities::add_media_type($media_type, $decoder, undef, refaddr $self);
1220 4 push $self->__media_type->@*, $media_type;
1221 4 return;
1222 }
1223  
1224 245 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=2 SCAR=6.9
1225 245 foreach my $media_type (uniqstr $self->__media_type->@*) {
1226 4 JSON::Schema::Modern::Utilities::delete_media_type($media_type, refaddr $self);
1227 }
1228 }
1229  
1230 # deprecated interface; will use global definitions
1231 24 ▶sub get_media_type ($self, $type) {
Subroutine get_media_type: called CC=2 SCAR=6.9
Pod documented
1232 24 ▶ carp '$jsm->get_media_type is deprecated; use the function in JSON::Schema::Modern::Utilities instead' if not $warn_get_media_type++;
Branch: 1 branch
BranchTrueFalseTotal
unless $warn_get_media_type++ 2 22 24
1233  
1234 24 JSON::Schema::Modern::Utilities::_get_media_type_decoder($type);
1235 }
1236  
1237 has _encoding => (
1238 is => 'bare',
1239 isa => HashRef[CodeRef],
1240 reader => '__encoding',
1241 lazy => 1,
1242 7 ▶ default => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.2
1243 +{
1244 0 ▶ identity => sub ($content_ref, @) { $content_ref },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1245 20 ▶ base64 => sub ($content_ref, @) {
Subroutine __ANON__: called CC=3 SCAR=11.0
1246 20 ▶ die "invalid characters\n"
Branch: 1 branch
BranchTrueFalseTotal
if $$content_ref =~ m[[^[:alnum:]+/=]]a or $$content_ref =~ /=(?=[^=])/u 6 14 20
Condition66%2/3
$$content_ref =~ m[[^[:alnum:]+/=]]a or $$content_ref =~ /=(?=[^=])/u
l!l&&r!l&&!r
6014
MC/DC50%1/2
$$content_ref =~ m[[^[:alnum:]+/=]]a or $$content_ref =~ /=(?=[^=])/u
$$content_ref =~ m[[^[:alnum:]+/=]]a $$content_ref =~ /=(?=[^=])/u
Truth table66%2/3
A or B
A= $$content_ref =~ m[[^[:alnum:]+/=]]aB= $$content_ref =~ /=(?=[^=])/u
ABresult
1X1
011
000
1247 if $content_ref->$* =~ m{[^[:alnum:]+/=]}a or $content_ref->$* =~ m{=(?=[^=])};
1248 14 require MIME::Base64; \ MIME::Base64::decode_base64($content_ref->$*);
1249 },
1250 1 ▶ base64url => sub ($content_ref, @) {
Subroutine __ANON__: called CC=3 SCAR=12.5
1251 1 ▶ die "invalid characters\n"
Branch: 1 branch
BranchTrueFalseTotal
if $$content_ref =~ /[^[:alnum:]=_-]/a or $$content_ref =~ /=(?=[^=])/u 0 1 1
Condition33%1/3
$$content_ref =~ /[^[:alnum:]=_-]/a or $$content_ref =~ /=(?=[^=])/u
l!l&&r!l&&!r
001
MC/DC0%0/2
$$content_ref =~ /[^[:alnum:]=_-]/a or $$content_ref =~ /=(?=[^=])/u
$$content_ref =~ /[^[:alnum:]=_-]/a $$content_ref =~ /=(?=[^=])/u
Truth table33%1/3
A or B
A= $$content_ref =~ /[^[:alnum:]=_-]/aB= $$content_ref =~ /=(?=[^=])/u
ABresult
1X1
011
000
1252 if $content_ref->$* =~ m{[^[:alnum:]=_-]}a or $content_ref->$* =~ m{=(?=[^=])};
1253 1 require MIME::Base64; \ MIME::Base64::decode_base64url($content_ref->$*);
1254 },
1255 7 };
1256 },
1257 46 );
1258  
1259 23 ▶sub get_encoding { $_[0]->__encoding->{$_[1]} }
Subroutine get_encoding: called CC=1 SCAR=0.0
Pod documented
1260 0 ▶sub add_encoding { $_[0]->__encoding->{$_[1]} = CodeRef->($_[2]) }
Subroutine add_encoding: not called CC=1 SCAR=6.9
Pod documented
1261  
1262 # callback hook for Sereal::Encoder
1263 3 ▶sub FREEZE ($self, $serializer) {
Subroutine FREEZE: called CC=1 SCAR=0.0
Pod undocumented
1264 3 my $data = +{ %$self };
1265 # Cpanel::JSON::XS doesn't serialize: https://github.com/Sereal/Sereal/issues/266
1266 # coderefs can't serialize cleanly and must be re-added by the user.
1267 3 delete $data->@{qw(_json_decoder _format_validations _media_type _encoding)};
1268 3 return $data;
1269 }
1270  
1271 # callback hook for Sereal::Decoder
1272 4 ▶sub THAW ($class, $serializer, $data) {
Subroutine THAW: called CC=4 SCAR=13.9
Pod undocumented
1273 4 ▶ $data->{max_depth} = delete $data->{max_traversal_depth} if exists $data->{max_traversal_depth};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$data{"max_traversal_depth"} 0 4 4
1274 4 my $self = bless($data, $class);
1275  
1276 # load all vocabulary classes, both those used by loaded schemas, as well as all the core modules
1277 load_module($_)
1278 4 foreach uniqstr(
1279 (map $_->{vocabularies}->@*, $self->_canonical_resources),
1280 (map $_->[1], values $self->__vocabulary_classes->%*));
1281  
1282 # populate the global cache from our existing resources (otherwise already-existing resources
1283 # may be loaded from disk again)
1284 4 foreach my $resource ($self->_canonical_resources) {
1285 12 JSON::Schema::Modern::Utilities::__populate_cached_document($self, $resource->{canonical_uri}, $resource->{document});
1286  
1287 # also update our existing resources from the global cache, to avoid future refaddr errors
1288 12 load_cached_document($self, $resource->{canonical_uri});
1289 }
1290  
1291 4 return $self;
1292 }
1293  
1294 46 1;
1295  
1296 __END__
« Modern/Vocabulary/Applicator.pm ↑ Summary Modern/Vocabulary/Unevaluated.pm »