Skip to content

Commit

Permalink
sr/json: add support for 201909 and 202012 drafts
Browse files Browse the repository at this point in the history
This adds support for validating schemas of the 2019-09 and 2020-12
drafts. The assertion keywords introduced in these drafts are not yet
validated for compatibility across successive schema versions. The
implementation of these keywords is going to be implemented later.

https://json-schema.org/draft/2019-09/release-notes
https://json-schema.org/draft/2020-12/release-notes
  • Loading branch information
pgellert committed Jul 24, 2024
1 parent adbe29d commit ff9305b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/v/pandaproxy/schema_registry/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ enum class json_schema_dialect {
draft4,
draft6,
draft7,
draft201909,
draft202012,
};

constexpr std::string_view
Expand All @@ -122,6 +124,10 @@ to_uri(json_schema_dialect draft, bool strip = false) {
return "http://json-schema.org/draft-06/schema#";
case draft7:
return "http://json-schema.org/draft-07/schema#";
case draft201909:
return "https://json-schema.org/draft/2019-09/schema#";
case draft202012:
return "https://json-schema.org/draft/2020-12/schema#";
}
}();

Expand All @@ -139,6 +145,8 @@ constexpr std::optional<json_schema_dialect> from_uri(std::string_view uri) {
.match_all(to_uri(draft4), to_uri(draft4, true), draft4)
.match_all(to_uri(draft6), to_uri(draft6, true), draft6)
.match_all(to_uri(draft7), to_uri(draft7, true), draft7)
.match_all(to_uri(draft201909), to_uri(draft201909, true), draft201909)
.match_all(to_uri(draft202012), to_uri(draft202012, true), draft202012)
.default_match(std::nullopt);
}

Expand Down Expand Up @@ -167,6 +175,12 @@ jsoncons::jsonschema::json_schema<jsoncons::json> const& get_metaschema() {
case json_schema_dialect::draft7:
return jsoncons::jsonschema::draft7::schema_draft7<
jsoncons::json>::get_schema();
case json_schema_dialect::draft201909:
return jsoncons::jsonschema::draft201909::schema_draft201909<
jsoncons::json>::get_schema();
case json_schema_dialect::draft202012:
return jsoncons::jsonschema::draft202012::schema_draft202012<
jsoncons::json>::get_schema();
}
}();

Expand All @@ -190,6 +204,10 @@ result<void> validate_json_schema(
return get_metaschema<draft6>();
case draft7:
return get_metaschema<draft7>();
case draft201909:
return get_metaschema<draft201909>();
case draft202012:
return get_metaschema<draft202012>();
}
}();

Expand Down Expand Up @@ -217,7 +235,7 @@ result<void> try_validate_json_schema(const jsoncons::json& schema) {

// no explicit $schema: try to validate from newest to oldest draft
auto first_error = std::optional<error_info>{};
for (auto d : {draft7, draft6, draft4}) {
for (auto d : {draft202012, draft201909, draft7, draft6, draft4}) {
auto res = validate_json_schema(d, schema);
if (res.has_value()) {
return outcome::success();
Expand Down Expand Up @@ -1255,7 +1273,19 @@ bool is_superset(json::Value const& older, json::Value const& newer) {
"if",
"then",
"else",
// later drafts:
// draft 2019-09 unhandled keywords:
"$anchor",
"$recursiveRef",
"$recursiveAnchor",
"unevaluatedItems",
"unevaluatedProperties",
"dependentRequired",
"maxContains",
"minContains",
"deprecated",
// draft 2020-12 unhandled keywords:
"$dynamicRef",
"$dynamicAnchor",
"prefixItems",
}) {
if (
Expand Down
2 changes: 2 additions & 0 deletions src/v/pandaproxy/schema_registry/test/test_json_schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ static constexpr auto valid_test_cases = std::to_array<std::string_view>({
R"json({"$schema": "http://json-schema.org/draft-07/schema"})json",
R"json({"$schema": "http://json-schema.org/draft-06/schema"})json",
R"json({"$schema": "http://json-schema.org/draft-04/schema"})json",
R"json({"$schema": "https://json-schema.org/draft/2019-09/schema"})json",
R"json({"$schema": "https://json-schema.org/draft/2020-12/schema"})json",
});
SEASTAR_THREAD_TEST_CASE(test_make_valid_json_schema) {
for (const auto& data : valid_test_cases) {
Expand Down

0 comments on commit ff9305b

Please sign in to comment.