-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat(linter): add forbid_empty_enum rule #2287
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
Open
Vaibhav701161
wants to merge
6
commits into
sourcemeta:main
Choose a base branch
from
Vaibhav701161:feat/linter-forbid-empty-enum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
040c9ed
feat(linter): make forbid_empty_enum auto-fixable by rewriting to false
Vaibhav701161 cbadb5c
test(linter): update forbid_empty_enum tests for 2019-09 and 2020-12
Vaibhav701161 b5d92d6
test(linter): update forbid_empty_enum tests for draft4, draft6, and …
Vaibhav701161 ec6066e
test(linter): add edge cases and draft3 coverage for forbid_empty_enum
Vaibhav701161 2c90804
feat(linter): preserve schema structure in forbid_empty_enum autofix
Vaibhav701161 b86dd96
test(linter): extend forbid_empty_enum coverage across dialects and r…
Vaibhav701161 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
| class ForbidEmptyEnum final : public SchemaTransformRule { | ||
| public: | ||
| using mutates = std::true_type; | ||
| using reframe_after_transform = std::true_type; | ||
| ForbidEmptyEnum() | ||
| : SchemaTransformRule{"forbid_empty_enum", | ||
| "An empty `enum` validates nothing and is " | ||
| "equivalent to `not: {}`"} {}; | ||
|
|
||
| [[nodiscard]] auto | ||
| condition(const sourcemeta::core::JSON &schema, | ||
| const sourcemeta::core::JSON &, | ||
| const sourcemeta::core::Vocabularies &vocabularies, | ||
| const sourcemeta::core::SchemaFrame &, | ||
| const sourcemeta::core::SchemaFrame::Location &, | ||
| const sourcemeta::core::SchemaWalker &, | ||
| const sourcemeta::core::SchemaResolver &) const | ||
| -> sourcemeta::core::SchemaTransformRule::Result override { | ||
| ONLY_CONTINUE_IF(vocabularies.contains_any( | ||
| {Vocabularies::Known::JSON_Schema_2020_12_Validation, | ||
| Vocabularies::Known::JSON_Schema_2019_09_Validation, | ||
| Vocabularies::Known::JSON_Schema_Draft_7, | ||
| Vocabularies::Known::JSON_Schema_Draft_6, | ||
| Vocabularies::Known::JSON_Schema_Draft_4}) && | ||
| schema.is_object() && schema.defines("enum") && | ||
| !schema.defines("not") && schema.at("enum").is_array() && | ||
| schema.at("enum").empty()); | ||
| return APPLIES_TO_KEYWORDS("enum"); | ||
| } | ||
|
|
||
| auto transform(JSON &schema, const Result &) const -> void override { | ||
| schema.erase("enum"); | ||
| schema.assign("not", JSON::make_object()); | ||
| } | ||
| }; | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4494,3 +4494,249 @@ TEST(AlterSchema_lint_2019_09, empty_object_as_true_1) { | |
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_1) { | ||
|
Member
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. Now that the rule is always fixable, let's always exercise it with |
||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [1], | ||
| "enum": [] | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [1], | ||
| "not": true | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_2) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [1], | ||
| "enum": [1, 2] | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [1], | ||
| "enum": [1, 2] | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_3) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [1], | ||
| "type": "string" | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [1], | ||
| "type": "string" | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_4) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "properties": { | ||
| "foo": { | ||
| "enum": [] | ||
| } | ||
| } | ||
| })JSON"); | ||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "properties": { | ||
| "foo": { | ||
| "not": true | ||
| } | ||
| } | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_5) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "$defs": { | ||
| "A": { | ||
| "enum": [] | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/A" | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "$defs": { | ||
| "A": { | ||
| "not": true | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/A" | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_6) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "$defs": { | ||
| "A": { | ||
| "enum": [], | ||
| "$defs": { | ||
| "inner": { "type": "string" } | ||
| } | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/A/$defs/inner" | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "$defs": { | ||
| "A": { | ||
| "$defs": { | ||
| "inner": { "type": "string" } | ||
| }, | ||
| "not": true | ||
| } | ||
| }, | ||
| "$ref": "#/$defs/A/$defs/inner" | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_7) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "$defs": { | ||
| "A": { | ||
| "$id": "https://example.com/schemas/A", | ||
| "enum": [] | ||
| } | ||
| }, | ||
| "$ref": "https://example.com/schemas/A" | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "$defs": { | ||
| "A": { | ||
| "$id": "https://example.com/schemas/A", | ||
| "not": true | ||
| } | ||
| }, | ||
| "$ref": "https://example.com/schemas/A" | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
|
|
||
| TEST(AlterSchema_lint_2019_09, forbid_empty_enum_8) { | ||
| sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "properties": { | ||
| "foo": { | ||
| "x-note": "placeholder", | ||
| "enum": [] | ||
| } | ||
| } | ||
| })JSON"); | ||
|
|
||
| LINT_AND_FIX(document, result, traces); | ||
|
|
||
| EXPECT_TRUE(result.first); | ||
|
|
||
| const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ | ||
| "$schema": "https://json-schema.org/draft/2019-09/schema", | ||
| "title": "Example", | ||
| "description": "Example schema", | ||
| "examples": [{}], | ||
| "properties": { | ||
| "foo": { | ||
| "x-note": "placeholder", | ||
| "not": true | ||
| } | ||
| } | ||
| })JSON"); | ||
|
|
||
| EXPECT_EQ(document, expected); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Can you add a
LINT_AND_FIXtest where you have a$refpointing directly at the subschema with the emptyenumand also one pointing to something INSIDE the subschema with emptyenum(maybe by having a$defssibling to the emptyenum)?I suspect the second will break at the moment.
Also what if you have an empty
enumon a subschema that has its own nested$id? By turning the entire thing tofalse, we would end up getting rid of the identifier, which can break a reference to the subschema by URI.Can you try to encode as many of these edge cases as you can possibly think of? These are the tricky cases that show up in practice and end up in incorrect linter behaviour or crashes
Given all the above, it might be easier to add
not: {}instead of turning the entire thing tofalse. And you can always update the condition to apply for Draft 4 and later (which will address https://github.com/sourcemeta/core/pull/2287/changes#r2931230208) and only apply ofnotis not there already (a fine compromise for now?)