Skip to content

Linter: create rule to remove other validation keywords when enum is present based on type #1899

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
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/property_names_default.h
linter/draft_ref_siblings.h
linter/definitions_to_defs.h
linter/non_applicable_enum_validation_keywords.h

# Strict
strict/required_properties_in_properties.h)
Expand Down
2 changes: 2 additions & 0 deletions src/extension/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ contains_any(const Vocabularies &container,
#include "linter/minimum_real_for_integer.h"
#include "linter/modern_official_dialect_with_empty_fragment.h"
#include "linter/multiple_of_default.h"
#include "linter/non_applicable_enum_validation_keywords.h"
#include "linter/non_applicable_type_specific_keywords.h"
#include "linter/not_false.h"
#include "linter/pattern_properties_default.h"
Expand Down Expand Up @@ -116,6 +117,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
bundle.add<MinimumRealForInteger>();
bundle.add<SingleTypeArray>();
bundle.add<EnumWithType>();
bundle.add<NonApplicableEnumValidationKeywords>();
bundle.add<DuplicateEnumValues>();
bundle.add<DuplicateRequiredValues>();
bundle.add<ConstWithType>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class NonApplicableEnumValidationKeywords final : public SchemaTransformRule {
public:
NonApplicableEnumValidationKeywords()
: SchemaTransformRule{
"enum_validation_keywords_default",
"Setting validation keywords that do not apply to any item in "
"`enum` is considered an anti-pattern"} {};

[[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 &walker,
const sourcemeta::core::SchemaResolver &) const
-> sourcemeta::core::SchemaTransformRule::Result override {
if (!contains_any(vocabularies,
{"https://json-schema.org/draft/2020-12/vocab/validation",
"https://json-schema.org/draft/2019-09/vocab/validation",
"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/schema#",
"http://json-schema.org/draft-02/hyper-schema#",
"http://json-schema.org/draft-01/schema#",
"http://json-schema.org/draft-01/hyper-schema#"}) ||
!schema.is_object() || !schema.defines("enum") ||
!schema.at("enum").is_array()) {
return false;
}

std::set<sourcemeta::core::JSON::Type> enum_types;
for (const auto &value : schema.at("enum").as_array()) {
enum_types.emplace(value.type());
}

if (enum_types.empty()) {
return false;
}

this->blacklist.clear();
for (const auto &entry : schema.as_object()) {
const auto metadata = walker(entry.first, vocabularies);

if (metadata.instances.empty()) {
continue;
}

if (std::ranges::none_of(metadata.instances,
[&enum_types](const auto keyword_type) {
return enum_types.contains(keyword_type);
})) {
this->blacklist.emplace_back(entry.first);
}
}

if (this->blacklist.empty()) {
return false;
}

std::ostringstream message;
for (const auto &entry : this->blacklist) {
message << "- " << entry << "\n";
}

return message.str();
}

auto transform(sourcemeta::core::JSON &schema) const -> void override {
schema.erase_keys(this->blacklist.cbegin(), this->blacklist.cend());
}

private:
mutable std::vector<sourcemeta::core::JSON::String> blacklist;
};
201 changes: 201 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,207 @@ TEST(AlterSchema_lint_2019_09, enum_with_type_4) {
EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ "foo", "bar" ],
"minimum": 0
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ "foo", "bar" ]
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 1, 2, 3 ],
"minLength": 0,
"maxLength": 5
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 1, 2, 3 ]
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ { "a": 1 }, { "b": 2 } ],
"minLength": 3,
"minimum": 0
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ { "a": 1 }, { "b": 2 } ]
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_4) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 1, "foo" ],
"minLength": 2
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 1, "foo" ],
"minLength": 2
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_5) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 1, "foo" ],
"minLength": 2,
"minimum": 0,
"minItems": 1
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 1, "foo" ],
"minLength": 2,
"minimum": 0
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_6) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ { "name": "alice" }, { "age": 25 } ],
"properties": {
"name": { "type": "string" },
"age": { "type": "number" }
},
"minLength": 5,
"minimum": 10
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ { "name": "alice" }, { "age": 25 } ],
"properties": {
"name": { "type": "string" },
"age": { "type": "number" }
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_7) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ "small", "medium", "large" ],
"title": "Size Options",
"minLength": 3,
"minItems": 2
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ "small", "medium", "large" ],
"title": "Size Options",
"minLength": 3
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_8) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ],
"minimum": 10,
"minLength": 2,
"minItems": 1,
"minProperties": 1,
"maxLength": 100,
"maxItems": 10,
"maxProperties": 5
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ],
"minimum": 10,
"minLength": 2,
"minItems": 1,
"minProperties": 1,
"maxLength": 100,
"maxItems": 10,
"maxProperties": 5
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_9) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/schema",
"$anchor": "color-enum",
"title": "My Enum Schema",
"description": "A schema with enum and annotations",
"$comment": "This schema uses enum with various annotation keywords",
"enum": [ "red", "green", "blue" ],
"minimum": 5,
"minLength": 10,
"minItems": 2,
"minProperties": 1,
"maxProperties": 5
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/schema",
"$anchor": "color-enum",
"title": "My Enum Schema",
"description": "A schema with enum and annotations",
"$comment": "This schema uses enum with various annotation keywords",
"enum": [ "red", "green", "blue" ],
"minLength": 10
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, single_type_array_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
Expand Down
Loading
Loading