Skip to content

Commit

Permalink
[WIP] Add redundancy rules for keyword that won't apply to given types
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Sep 9, 2024
1 parent bb285c6 commit 9169441
Show file tree
Hide file tree
Showing 50 changed files with 1,333 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_applicator_2019_09.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DropNonArrayKeywordsApplicator_2019_09 final : public Rule {
public:
DropNonArrayKeywordsApplicator_2019_09()
: Rule{"drop_non_array_keywords_applicator_2019_09"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/applicator") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"properties", "patternProperties",
"additionalProperties",
"dependentSchemas", "propertyNames"};
};
27 changes: 27 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_applicator_2020_12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DropNonArrayKeywordsApplicator_2020_12 final : public Rule {
public:
DropNonArrayKeywordsApplicator_2020_12()
: Rule{"drop_non_array_keywords_applicator_2020_12"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/applicator") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"properties", "patternProperties",
"additionalProperties",
"dependentSchemas", "propertyNames"};
};
27 changes: 27 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_content_2019_09.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DropNonArrayKeywordsContent_2019_09 final : public Rule {
public:
DropNonArrayKeywordsContent_2019_09()
: Rule{"drop_non_array_keywords_content_2019_09"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.is_object() && schema.defines("type") &&
schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/content") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"contentEncoding", "contentMediaType",
"contentSchema"};
};
27 changes: 27 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_content_2020_12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DropNonArrayKeywordsContent_2020_12 final : public Rule {
public:
DropNonArrayKeywordsContent_2020_12()
: Rule{"drop_non_array_keywords_content_2020_12"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.is_object() && schema.defines("type") &&
schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/content") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"contentEncoding", "contentMediaType",
"contentSchema"};
};
27 changes: 27 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_format_2019_09.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DropNonArrayKeywordsFormat_2019_09 final : public Rule {
public:
DropNonArrayKeywordsFormat_2019_09()
: Rule{"drop_non_array_keywords_format_2019_09"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
(vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/"
"format-annotation") ||
vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/"
"format-assertion")) &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"format"};
};
27 changes: 27 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_format_2020_12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DropNonArrayKeywordsFormat_2020_12 final : public Rule {
public:
DropNonArrayKeywordsFormat_2020_12()
: Rule{"drop_non_array_keywords_format_2020_12"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
(vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/"
"format-annotation") ||
vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/"
"format-assertion")) &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"format"};
};
25 changes: 25 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_unevaluated_2020_12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class DropNonArrayKeywordsUnevaluated_2020_12 final : public Rule {
public:
DropNonArrayKeywordsUnevaluated_2020_12()
: Rule{"drop_non_array_keywords_unevaluated_2020_12"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/unevaluated") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"unevaluatedProperties"};
};
26 changes: 26 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_validation_2019_09.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class DropNonArrayKeywordsValidation_2019_09 final : public Rule {
public:
DropNonArrayKeywordsValidation_2019_09()
: Rule{"drop_non_array_keywords_validation_2019_09"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{
"minLength", "maxLength", "pattern", "maximum",
"exclusiveMinimum", "multipleOf", "exclusiveMaximum", "minimum",
"dependentRequired", "minProperties", "maxProperties", "required"};
};
26 changes: 26 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_validation_2020_12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class DropNonArrayKeywordsValidation_2020_12 final : public Rule {
public:
DropNonArrayKeywordsValidation_2020_12()
: Rule{"drop_non_array_keywords_validation_2020_12"} {};

[[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 vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") &&
schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "array" &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{
"minLength", "maxLength", "pattern", "maximum",
"exclusiveMinimum", "multipleOf", "exclusiveMaximum", "minimum",
"dependentRequired", "minProperties", "maxProperties", "required"};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class DropNonBooleanKeywordsApplicator_2019_09 final : public Rule {
public:
DropNonBooleanKeywordsApplicator_2019_09()
: Rule{"drop_non_boolean_keywords_applicator_2019_09"} {};

[[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 is_boolean_schema(schema, vocabularies) &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/applicator") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"properties",
"patternProperties",
"additionalProperties",
"dependentSchemas",
"propertyNames",
"prefixItems",
"contains",
"items"};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class DropNonBooleanKeywordsApplicator_2020_12 final : public Rule {
public:
DropNonBooleanKeywordsApplicator_2020_12()
: Rule{"drop_non_boolean_keywords_applicator_2020_12"} {};

[[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 is_boolean_schema(schema, vocabularies) &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/applicator") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"properties",
"patternProperties",
"additionalProperties",
"dependentSchemas",
"propertyNames",
"prefixItems",
"contains",
"items"};
};
23 changes: 23 additions & 0 deletions src/linter/redundant/drop_non_boolean_keywords_content_2019_09.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class DropNonBooleanKeywordsContent_2019_09 final : public Rule {
public:
DropNonBooleanKeywordsContent_2019_09()
: Rule{"drop_non_boolean_keywords_content_2019_09"} {};

[[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 is_boolean_schema(schema, vocabularies) &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/content") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"contentEncoding", "contentMediaType",
"contentSchema"};
};
23 changes: 23 additions & 0 deletions src/linter/redundant/drop_non_boolean_keywords_content_2020_12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class DropNonBooleanKeywordsContent_2020_12 final : public Rule {
public:
DropNonBooleanKeywordsContent_2020_12()
: Rule{"drop_non_boolean_keywords_content_2020_12"} {};

[[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 is_boolean_schema(schema, vocabularies) &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/content") &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"contentEncoding", "contentMediaType",
"contentSchema"};
};
24 changes: 24 additions & 0 deletions src/linter/redundant/drop_non_boolean_keywords_format_2019_09.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class DropNonBooleanKeywordsFormat_2019_09 final : public Rule {
public:
DropNonBooleanKeywordsFormat_2019_09()
: Rule{"drop_non_boolean_keywords_format_2019_09"} {};

[[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 is_boolean_schema(schema, vocabularies) &&
(vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/"
"format-annotation") ||
vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/"
"format-assertion")) &&
schema.defines_any(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase_keys(this->BLACKLIST.cbegin(), this->BLACKLIST.cend());
}

private:
const std::set<std::string> BLACKLIST{"format"};
};
Loading

0 comments on commit 9169441

Please sign in to comment.