-
-
Couldn't load subscription status.
- Fork 7
Linter: create rule to to add syntactical sugar when then is set to false #1918
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
base: main
Are you sure you want to change the base?
Changes from all commits
43b69a8
5af1cce
04ff1fd
480ab0a
704b199
f4eb3e0
39c1dba
ea2c18a
56eb2e1
e6e3ae0
2caa08f
fe385be
3bb0d5d
1cb1036
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||
| class ThenFalse final : public SchemaTransformRule { | ||||||
| public: | ||||||
| ThenFalse() | ||||||
| : SchemaTransformRule{ | ||||||
| "then_false", | ||||||
| "`Setting the `then` keyword to the false schema is a convoluted " | ||||||
| "way of negating the `if` subschema, which you can more cleanly " | ||||||
| "represent using the `not` keyword"} {}; | ||||||
|
|
||||||
| [[nodiscard]] auto | ||||||
| condition(const JSON &schema, const JSON &, const Vocabularies &vocabularies, | ||||||
| const SchemaFrame &, const SchemaFrame::Location &, | ||||||
| const SchemaWalker &, const SchemaResolver &) const | ||||||
| -> SchemaTransformRule::Result override { | ||||||
| ONLY_CONTINUE_IF( | ||||||
| contains_any(vocabularies, | ||||||
| {"https://json-schema.org/draft/2020-12/vocab/applicator", | ||||||
| "https://json-schema.org/draft/2019-09/vocab/applicator", | ||||||
| "http://json-schema.org/draft-07/schema#"}) && | ||||||
| schema.is_object() && schema.defines("if") && schema.defines("then") && | ||||||
| is_schema(schema.at("then")) && schema.at("then").is_boolean() && | ||||||
| !schema.at("then").to_boolean() && is_schema(schema.at("if")) && | ||||||
| !(schema.at("if").is_boolean() && schema.at("if").to_boolean()) && | ||||||
| (!schema.defines("else") || | ||||||
| (schema.at("else").is_boolean() && schema.at("else").to_boolean())) && | ||||||
| (!schema.at("if").is_object() || | ||||||
| std::all_of(schema.at("if").as_object().begin(), | ||||||
| schema.at("if").as_object().end(), | ||||||
| [&schema](const auto &entry) { | ||||||
| return !schema.defines(entry.first); | ||||||
| }))); | ||||||
|
|
||||||
| if (schema.defines("else")) { | ||||||
| return APPLIES_TO_KEYWORDS("if", "then", "else"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's keep |
||||||
| } else { | ||||||
| return APPLIES_TO_KEYWORDS("if", "then"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| auto transform(JSON &schema, const Result &) const -> void override { | ||||||
| auto if_schema = schema.at("if"); | ||||||
| schema.erase("if"); | ||||||
| schema.erase("then"); | ||||||
| schema.erase("else"); | ||||||
| schema.assign("not", std::move(if_schema)); | ||||||
Karan-Palan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
| }; | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
is_schema(schema.at("then"))is unnecessary if you are already checking its a boolean right afterwards