Skip to content

Commit

Permalink
Improve error when making a $ref to a non-schema
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Sep 23, 2024
1 parent 23ee0c8 commit e01a3a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/jsonschema/compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,14 @@ auto compile(const SchemaCompilerContext &context,
}

const auto &entry{context.frame.at({ReferenceType::Static, destination})};

const auto &new_schema{get(context.root, entry.pointer)};

if (!is_schema(new_schema)) {
throw SchemaReferenceError(
destination, destination_pointer,
"The target of the reference is not a valid schema");
}

return compile_subschema(
context,
{entry.relative_pointer, new_schema,
Expand Down
32 changes: 32 additions & 0 deletions test/jsonschema/jsonschema_compile_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4219,3 +4219,35 @@ TEST(JSONSchema_compile_draft4, format_uri_3) {
instance, 0,
"The string value \"!!!x::://\" was expected to represent a valid URI");
}

TEST(JSONSchema_compile_draft4, ref_to_non_schema) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"array": {
"items": [
{ "type": "string" },
{ "type": "array" }
]
}
},
"additionalProperties": {
"$ref": "#/definitions/array/items"
}
})JSON")};

try {
sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler);
FAIL() << "The compile function was expected to throw";
} catch (const sourcemeta::jsontoolkit::SchemaReferenceError &error) {
EXPECT_EQ(error.id(), "#/definitions/array/items");
EXPECT_EQ(sourcemeta::jsontoolkit::to_string(error.location()), "/$ref");
SUCCEED();
} catch (...) {
FAIL();
}
}

0 comments on commit e01a3a4

Please sign in to comment.