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 11 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
3 changes: 2 additions & 1 deletion src/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/property_names_type_default.h
linter/property_names_default.h
linter/draft_ref_siblings.h
linter/definitions_to_defs.h)
linter/definitions_to_defs.h
linter/non_applicable_enum_validation_keywords.h)

if(SOURCEMETA_CORE_INSTALL)
sourcemeta_library_install(NAMESPACE sourcemeta PROJECT core NAME alterschema)
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 @@ -69,6 +69,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/pattern_properties_default.h"
#include "linter/properties_default.h"
Expand Down Expand Up @@ -115,6 +116,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
bundle.add<MinimumRealForInteger>();
bundle.add<SingleTypeArray>();
bundle.add<EnumWithType>();
bundle.add<EnumValidationKeywordsDefault>();
bundle.add<DuplicateEnumValues>();
bundle.add<DuplicateRequiredValues>();
bundle.add<ConstWithType>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class EnumValidationKeywordsDefault final : public SchemaTransformRule {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the class doesn't match the file name?

public:
EnumValidationKeywordsDefault()
: SchemaTransformRule{
"enum_validation_keywords_default",
"Setting validation keywords that do not apply to any item in "
"`enum` is considered an "
"anti-pattern, as the enumeration choices already imply their "
"respective types"} {};

[[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.type == sourcemeta::core::SchemaKeywordType::Other ||
metadata.type == sourcemeta::core::SchemaKeywordType::Reference) {
continue;
}

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

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

return !this->blacklist.empty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of a minor thing, but can you print the offending keywords like we do for x-?

}

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;
};
169 changes: 169 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,175 @@ 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, 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