-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implicit rules for
minItems
(#231)
Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
6 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class MinItemsGivenMinContains final : public sourcemeta::alterschema::Rule { | ||
public: | ||
MinItemsGivenMinContains() | ||
: Rule{"min_items_given_min_contains", | ||
"Every array has a minimum size of zero items but may be affected " | ||
"by `minContains`"} {}; | ||
|
||
[[nodiscard]] auto | ||
condition(const sourcemeta::jsontoolkit::JSON &schema, const std::string &, | ||
const std::set<std::string> &vocabularies, | ||
const sourcemeta::jsontoolkit::Pointer &) const -> bool override { | ||
return contains_any( | ||
vocabularies, | ||
{"https://json-schema.org/draft/2020-12/vocab/validation", | ||
"https://json-schema.org/draft/2019-09/vocab/validation"}) && | ||
schema.is_object() && schema.defines("type") && | ||
schema.at("type").is_string() && | ||
schema.at("type").to_string() == "array" && | ||
!schema.defines("minItems"); | ||
} | ||
|
||
auto transform(Transformer &transformer) const -> void override { | ||
if (transformer.schema().defines("contains") && | ||
transformer.schema().defines("minContains") && | ||
transformer.schema().at("minContains").is_integer()) { | ||
transformer.assign( | ||
"minItems", sourcemeta::jsontoolkit::JSON{ | ||
transformer.schema().at("minContains").to_integer()}); | ||
} else { | ||
transformer.assign("minItems", sourcemeta::jsontoolkit::JSON{0}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class MinItemsImplicit final : public Rule { | ||
public: | ||
MinItemsImplicit() | ||
: Rule{"min_items_implicit", | ||
"Every array has a minimum size of zero items"} {}; | ||
|
||
[[nodiscard]] auto | ||
condition(const sourcemeta::jsontoolkit::JSON &schema, const std::string &, | ||
const std::set<std::string> &vocabularies, | ||
const sourcemeta::jsontoolkit::Pointer &) const -> bool override { | ||
return contains_any(vocabularies, | ||
{"http://json-schema.org/draft-07/schema#", | ||
"http://json-schema.org/draft-06/schema#", | ||
"http://json-schema.org/draft-04/schema#", | ||
"http://json-schema.org/draft-03/schema#", | ||
"http://json-schema.org/draft-02/hyper-schema#", | ||
"http://json-schema.org/draft-01/hyper-schema#"}) && | ||
schema.is_object() && schema.defines("type") && | ||
schema.at("type").is_string() && | ||
schema.at("type").to_string() == "array" && | ||
!schema.defines("minItems"); | ||
} | ||
|
||
auto transform(Transformer &transformer) const -> void override { | ||
transformer.assign("minItems", sourcemeta::jsontoolkit::JSON{0}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters