Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile anyOf for Draft 4 #639

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/jsonschema/default_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ auto sourcemeta::jsontoolkit::default_schema_compiler(
// enum
// format
// - Semi Complex
// anyOf
// oneOf
// dependencies

Expand All @@ -58,6 +57,8 @@ auto sourcemeta::jsontoolkit::default_schema_compiler(
compiler_draft4_validation_required);
COMPILE("http://json-schema.org/draft-04/schema#", "allOf",
compiler_draft4_validation_allof);
COMPILE("http://json-schema.org/draft-04/schema#", "anyOf",
compiler_draft4_validation_anyof);
COMPILE("http://json-schema.org/draft-04/schema#", "properties",
compiler_draft4_validation_properties);
COMPILE("http://json-schema.org/draft-04/schema#", "patternProperties",
Expand Down
19 changes: 19 additions & 0 deletions src/jsonschema/default_compiler_draft4.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ auto compiler_draft4_validation_allof(const SchemaCompilerContext &context)
return result;
}

auto compiler_draft4_validation_anyof(const SchemaCompilerContext &context)
-> SchemaCompilerTemplate {
assert(context.value.is_array());
assert(!context.value.empty());

const auto subcontext{applicate(context)};
SchemaCompilerTemplate disjunctors;
for (std::uint64_t index = 0; index < context.value.size(); index++) {
disjunctors.push_back(make<SchemaCompilerLogicalAnd>(
subcontext, SchemaCompilerValueNone{},
compile(subcontext, {static_cast<Pointer::Token::Index>(index)}),
SchemaCompilerTemplate{}));
}

return {make<SchemaCompilerLogicalOr>(context, SchemaCompilerValueNone{},
std::move(disjunctors),
SchemaCompilerTemplate{})};
}

auto compiler_draft4_validation_properties(const SchemaCompilerContext &context)
-> SchemaCompilerTemplate {
assert(context.value.is_object());
Expand Down
62 changes: 62 additions & 0 deletions test/jsonschema/jsonschema_compile_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1468,3 +1468,65 @@ TEST(JSONSchema_compile_draft4, additionalItems_5) {
EVALUATE_TRACE_FAILURE(6, LoopItems, "/additionalItems", "#/additionalItems",
"");
}

TEST(JSONSchema_compile_draft4, anyOf_1) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"anyOf": [
{ "type": "string" },
{ "type": "integer" },
{ "type": "number" }
]
})JSON")};

const auto compiled_schema{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};

const sourcemeta::jsontoolkit::JSON instance{1};
EVALUATE_WITH_TRACE_FAST_SUCCESS(compiled_schema, instance, 5);

EVALUATE_TRACE_FAILURE(0, AssertionType, "/anyOf/0/type", "#/anyOf/0/type",
"");
EVALUATE_TRACE_FAILURE(1, LogicalAnd, "/anyOf", "#/anyOf", "");
EVALUATE_TRACE_SUCCESS(2, AssertionType, "/anyOf/1/type", "#/anyOf/1/type",
"");
EVALUATE_TRACE_SUCCESS(3, LogicalAnd, "/anyOf", "#/anyOf", "");
EVALUATE_TRACE_SUCCESS(4, LogicalOr, "/anyOf", "#/anyOf", "");
}

TEST(JSONSchema_compile_draft4, anyOf_2) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"anyOf": [
{ "type": "string" },
{ "type": "integer" },
{ "type": "number" }
]
})JSON")};

const auto compiled_schema{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};

const sourcemeta::jsontoolkit::JSON instance{true};
EVALUATE_WITH_TRACE_FAST_FAILURE(compiled_schema, instance, 9);

EVALUATE_TRACE_FAILURE(0, AssertionType, "/anyOf/0/type", "#/anyOf/0/type",
"");
EVALUATE_TRACE_FAILURE(1, LogicalAnd, "/anyOf", "#/anyOf", "");
EVALUATE_TRACE_FAILURE(2, AssertionType, "/anyOf/1/type", "#/anyOf/1/type",
"");
EVALUATE_TRACE_FAILURE(3, LogicalAnd, "/anyOf", "#/anyOf", "");
EVALUATE_TRACE_FAILURE(4, AssertionType, "/anyOf/2/type", "#/anyOf/2/type",
"");
EVALUATE_TRACE_FAILURE(5, AssertionType, "/anyOf/2/type", "#/anyOf/2/type",
"");
EVALUATE_TRACE_FAILURE(6, LogicalOr, "/anyOf/2/type", "#/anyOf/2/type", "");
EVALUATE_TRACE_FAILURE(7, LogicalAnd, "/anyOf", "#/anyOf", "");
EVALUATE_TRACE_FAILURE(8, LogicalOr, "/anyOf", "#/anyOf", "");
}
Loading