Skip to content

Commit c168dc5

Browse files
fix paths in errors
see the examples in https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.10.4.2 but also see json-schema-org/json-schema-spec#937 - instance_location and keyword_location will always be json pointers - absolute_keyword_location will always be an absolute URI or URI reference, when defined Luckily the published schema at https://json-schema.org/draft/2019-09/output/schema will still validate our output, because a bare json pointer looks like a uri reference.
1 parent 2ad57fa commit c168dc5

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/JSON/Schema/Draft201909.pm

+8-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ sub evaluate {
7272
short_circuit => $self->short_circuit,
7373
data_path => '',
7474
traversed_schema_path => '', # the accumulated path up to the last $ref traversal
75-
absolute_schema_path => '', # the absolute path of the last traversed $ref
75+
absolute_schema_uri => undef, # the absolute path of the last traversed $ref; always a Mojo::URL
7676
schema_path => '', # the rest of the path, since the last traversed $ref
7777
errors => [],
7878
};
@@ -166,11 +166,11 @@ sub _eval_keyword_ref {
166166

167167
# for now, the base_uri of the schema is always '' (yes I know this is not an absolute uri)
168168
# TODO: we need to track the base_uri of the schema resource that we are referencing.
169-
# absolute_schema_path may not look anything like the contents of the $ref keyword.
169+
# absolute_schema_uri may not look anything like the contents of the $ref keyword.
170170
return $self->_eval($data, $subschema,
171171
+{ %$state,
172172
traversed_schema_path => $state->{traversed_schema_path}.$state->{schema_path}.'/$ref',
173-
absolute_schema_path => $url->to_string,
173+
absolute_schema_uri => $url,
174174
schema_path => '',
175175
});
176176
}
@@ -847,8 +847,11 @@ sub E {
847847
push @{$state->{errors}}, JSON::Schema::Draft201909::Error->new(
848848
instance_location => $state->{data_path},
849849
keyword_location => $state->{traversed_schema_path}.$state->{schema_path}.$suffix,
850-
!$state->{absolute_schema_path} ? ()
851-
: ( absolute_keyword_location => $state->{absolute_schema_path}.$state->{schema_path}.$suffix ),
850+
!$state->{absolute_schema_uri} ? () : ( absolute_keyword_location => do {
851+
my $abs = $state->{absolute_schema_uri}->clone;
852+
$abs->fragment($abs->fragment.$state->{schema_path}.$suffix);
853+
$abs;
854+
} ),
852855
error => @args ? sprintf($error_string, @args) : $error_string,
853856
);
854857

lib/JSON/Schema/Draft201909/Error.pm

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ use namespace::clean;
1515
has [qw(
1616
instance_location
1717
keyword_location
18-
absolute_keyword_location
1918
error
20-
)] => ( is => 'ro', isa => Str );
19+
)] => (
20+
is => 'ro',
21+
isa => Str,
22+
required => 1,
23+
);
24+
25+
has absolute_keyword_location => (
26+
is => 'ro',
27+
isa => Str, # always a uri (absolute uri or uri reference)
28+
coerce => sub { "$_[0]" },
29+
);
2130

2231
sub TO_JSON {
2332
my $self = shift;

0 commit comments

Comments
 (0)