Skip to content

Commit

Permalink
WIP do older too
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Sep 10, 2024
1 parent 1f23b44 commit 7fc58eb
Show file tree
Hide file tree
Showing 44 changed files with 1,699 additions and 20 deletions.
42 changes: 42 additions & 0 deletions src/linter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/drop_non_array_keywords_applicator_2020_12.h
redundant/drop_non_array_keywords_content_2019_09.h
redundant/drop_non_array_keywords_content_2020_12.h
redundant/drop_non_array_keywords_draft0.h
redundant/drop_non_array_keywords_draft1.h
redundant/drop_non_array_keywords_draft2.h
redundant/drop_non_array_keywords_draft3.h
redundant/drop_non_array_keywords_draft4.h
redundant/drop_non_array_keywords_draft6.h
redundant/drop_non_array_keywords_draft7.h
redundant/drop_non_array_keywords_format_2019_09.h
redundant/drop_non_array_keywords_format_2020_12.h
redundant/drop_non_array_keywords_unevaluated_2020_12.h
Expand All @@ -37,6 +44,13 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/drop_non_boolean_keywords_applicator_2020_12.h
redundant/drop_non_boolean_keywords_content_2019_09.h
redundant/drop_non_boolean_keywords_content_2020_12.h
redundant/drop_non_boolean_keywords_draft0.h
redundant/drop_non_boolean_keywords_draft1.h
redundant/drop_non_boolean_keywords_draft2.h
redundant/drop_non_boolean_keywords_draft3.h
redundant/drop_non_boolean_keywords_draft4.h
redundant/drop_non_boolean_keywords_draft6.h
redundant/drop_non_boolean_keywords_draft7.h
redundant/drop_non_boolean_keywords_format_2019_09.h
redundant/drop_non_boolean_keywords_format_2020_12.h
redundant/drop_non_boolean_keywords_unevaluated_2020_12.h
Expand All @@ -46,6 +60,13 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/drop_non_null_keywords_applicator_2020_12.h
redundant/drop_non_null_keywords_content_2019_09.h
redundant/drop_non_null_keywords_content_2020_12.h
redundant/drop_non_null_keywords_draft0.h
redundant/drop_non_null_keywords_draft1.h
redundant/drop_non_null_keywords_draft2.h
redundant/drop_non_null_keywords_draft3.h
redundant/drop_non_null_keywords_draft4.h
redundant/drop_non_null_keywords_draft6.h
redundant/drop_non_null_keywords_draft7.h
redundant/drop_non_null_keywords_format_2019_09.h
redundant/drop_non_null_keywords_format_2020_12.h
redundant/drop_non_null_keywords_unevaluated_2020_12.h
Expand All @@ -55,6 +76,13 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/drop_non_numeric_keywords_applicator_2020_12.h
redundant/drop_non_numeric_keywords_content_2019_09.h
redundant/drop_non_numeric_keywords_content_2020_12.h
redundant/drop_non_numeric_keywords_draft0.h
redundant/drop_non_numeric_keywords_draft1.h
redundant/drop_non_numeric_keywords_draft2.h
redundant/drop_non_numeric_keywords_draft3.h
redundant/drop_non_numeric_keywords_draft4.h
redundant/drop_non_numeric_keywords_draft6.h
redundant/drop_non_numeric_keywords_draft7.h
redundant/drop_non_numeric_keywords_format_2019_09.h
redundant/drop_non_numeric_keywords_format_2020_12.h
redundant/drop_non_numeric_keywords_unevaluated_2020_12.h
Expand All @@ -64,13 +92,27 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/drop_non_object_keywords_applicator_2020_12.h
redundant/drop_non_object_keywords_content_2019_09.h
redundant/drop_non_object_keywords_content_2020_12.h
redundant/drop_non_object_keywords_draft0.h
redundant/drop_non_object_keywords_draft1.h
redundant/drop_non_object_keywords_draft2.h
redundant/drop_non_object_keywords_draft3.h
redundant/drop_non_object_keywords_draft4.h
redundant/drop_non_object_keywords_draft6.h
redundant/drop_non_object_keywords_draft7.h
redundant/drop_non_object_keywords_format_2019_09.h
redundant/drop_non_object_keywords_format_2020_12.h
redundant/drop_non_object_keywords_unevaluated_2020_12.h
redundant/drop_non_object_keywords_validation_2019_09.h
redundant/drop_non_object_keywords_validation_2020_12.h
redundant/drop_non_string_keywords_applicator_2019_09.h
redundant/drop_non_string_keywords_applicator_2020_12.h
redundant/drop_non_string_keywords_draft0.h
redundant/drop_non_string_keywords_draft1.h
redundant/drop_non_string_keywords_draft2.h
redundant/drop_non_string_keywords_draft3.h
redundant/drop_non_string_keywords_draft4.h
redundant/drop_non_string_keywords_draft6.h
redundant/drop_non_string_keywords_draft7.h
redundant/drop_non_string_keywords_unevaluated_2020_12.h
redundant/drop_non_string_keywords_validation_2019_09.h
redundant/drop_non_string_keywords_validation_2020_12.h
Expand Down
104 changes: 84 additions & 20 deletions src/linter/linter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ template <typename T> auto every_item_is_boolean(const T &container) -> bool {
[](const auto &element) { return element.is_boolean(); });
}

// We don't have to check for "type: null" as that type
// is collapsed to an enum by other canonicalizer rules.
auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema,
const std::set<std::string> &vocabularies) -> bool {
return 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/hyper-schema#",
"http://json-schema.org/draft-01/hyper-schema#"}) &&
schema.is_object() &&
((schema.defines("type") && schema.at("type").is_string() &&
schema.at("type").to_string() == "null") ||
(schema.defines("enum") && schema.at("enum").is_array() &&
every_item_is_null(schema.at("enum").as_array())));
}

// Modernize
#include "modernize/enum_to_const.h"
// AntiPattern
Expand All @@ -68,6 +48,13 @@ auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema,
#include "redundant/drop_non_array_keywords_applicator_2020_12.h"
#include "redundant/drop_non_array_keywords_content_2019_09.h"
#include "redundant/drop_non_array_keywords_content_2020_12.h"
#include "redundant/drop_non_array_keywords_draft0.h"
#include "redundant/drop_non_array_keywords_draft1.h"
#include "redundant/drop_non_array_keywords_draft2.h"
#include "redundant/drop_non_array_keywords_draft3.h"
#include "redundant/drop_non_array_keywords_draft4.h"
#include "redundant/drop_non_array_keywords_draft6.h"
#include "redundant/drop_non_array_keywords_draft7.h"
#include "redundant/drop_non_array_keywords_format_2019_09.h"
#include "redundant/drop_non_array_keywords_format_2020_12.h"
#include "redundant/drop_non_array_keywords_unevaluated_2020_12.h"
Expand All @@ -77,6 +64,13 @@ auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema,
#include "redundant/drop_non_boolean_keywords_applicator_2020_12.h"
#include "redundant/drop_non_boolean_keywords_content_2019_09.h"
#include "redundant/drop_non_boolean_keywords_content_2020_12.h"
#include "redundant/drop_non_boolean_keywords_draft0.h"
#include "redundant/drop_non_boolean_keywords_draft1.h"
#include "redundant/drop_non_boolean_keywords_draft2.h"
#include "redundant/drop_non_boolean_keywords_draft3.h"
#include "redundant/drop_non_boolean_keywords_draft4.h"
#include "redundant/drop_non_boolean_keywords_draft6.h"
#include "redundant/drop_non_boolean_keywords_draft7.h"
#include "redundant/drop_non_boolean_keywords_format_2019_09.h"
#include "redundant/drop_non_boolean_keywords_format_2020_12.h"
#include "redundant/drop_non_boolean_keywords_unevaluated_2020_12.h"
Expand All @@ -86,6 +80,13 @@ auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema,
#include "redundant/drop_non_null_keywords_applicator_2020_12.h"
#include "redundant/drop_non_null_keywords_content_2019_09.h"
#include "redundant/drop_non_null_keywords_content_2020_12.h"
#include "redundant/drop_non_null_keywords_draft0.h"
#include "redundant/drop_non_null_keywords_draft1.h"
#include "redundant/drop_non_null_keywords_draft2.h"
#include "redundant/drop_non_null_keywords_draft3.h"
#include "redundant/drop_non_null_keywords_draft4.h"
#include "redundant/drop_non_null_keywords_draft6.h"
#include "redundant/drop_non_null_keywords_draft7.h"
#include "redundant/drop_non_null_keywords_format_2019_09.h"
#include "redundant/drop_non_null_keywords_format_2020_12.h"
#include "redundant/drop_non_null_keywords_unevaluated_2020_12.h"
Expand All @@ -95,6 +96,13 @@ auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema,
#include "redundant/drop_non_numeric_keywords_applicator_2020_12.h"
#include "redundant/drop_non_numeric_keywords_content_2019_09.h"
#include "redundant/drop_non_numeric_keywords_content_2020_12.h"
#include "redundant/drop_non_numeric_keywords_draft0.h"
#include "redundant/drop_non_numeric_keywords_draft1.h"
#include "redundant/drop_non_numeric_keywords_draft2.h"
#include "redundant/drop_non_numeric_keywords_draft3.h"
#include "redundant/drop_non_numeric_keywords_draft4.h"
#include "redundant/drop_non_numeric_keywords_draft6.h"
#include "redundant/drop_non_numeric_keywords_draft7.h"
#include "redundant/drop_non_numeric_keywords_format_2019_09.h"
#include "redundant/drop_non_numeric_keywords_format_2020_12.h"
#include "redundant/drop_non_numeric_keywords_unevaluated_2020_12.h"
Expand All @@ -104,13 +112,27 @@ auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema,
#include "redundant/drop_non_object_keywords_applicator_2020_12.h"
#include "redundant/drop_non_object_keywords_content_2019_09.h"
#include "redundant/drop_non_object_keywords_content_2020_12.h"
#include "redundant/drop_non_object_keywords_draft0.h"
#include "redundant/drop_non_object_keywords_draft1.h"
#include "redundant/drop_non_object_keywords_draft2.h"
#include "redundant/drop_non_object_keywords_draft3.h"
#include "redundant/drop_non_object_keywords_draft4.h"
#include "redundant/drop_non_object_keywords_draft6.h"
#include "redundant/drop_non_object_keywords_draft7.h"
#include "redundant/drop_non_object_keywords_format_2019_09.h"
#include "redundant/drop_non_object_keywords_format_2020_12.h"
#include "redundant/drop_non_object_keywords_unevaluated_2020_12.h"
#include "redundant/drop_non_object_keywords_validation_2019_09.h"
#include "redundant/drop_non_object_keywords_validation_2020_12.h"
#include "redundant/drop_non_string_keywords_applicator_2019_09.h"
#include "redundant/drop_non_string_keywords_applicator_2020_12.h"
#include "redundant/drop_non_string_keywords_draft0.h"
#include "redundant/drop_non_string_keywords_draft1.h"
#include "redundant/drop_non_string_keywords_draft2.h"
#include "redundant/drop_non_string_keywords_draft3.h"
#include "redundant/drop_non_string_keywords_draft4.h"
#include "redundant/drop_non_string_keywords_draft6.h"
#include "redundant/drop_non_string_keywords_draft7.h"
#include "redundant/drop_non_string_keywords_unevaluated_2020_12.h"
#include "redundant/drop_non_string_keywords_validation_2019_09.h"
#include "redundant/drop_non_string_keywords_validation_2020_12.h"
Expand Down Expand Up @@ -163,6 +185,13 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<DropNonArrayKeywordsApplicator_2020_12>();
bundle.add<DropNonArrayKeywordsContent_2019_09>();
bundle.add<DropNonArrayKeywordsContent_2020_12>();
bundle.add<DropNonArrayKeywords_Draft0>();
bundle.add<DropNonArrayKeywords_Draft1>();
bundle.add<DropNonArrayKeywords_Draft2>();
bundle.add<DropNonArrayKeywords_Draft3>();
bundle.add<DropNonArrayKeywords_Draft4>();
bundle.add<DropNonArrayKeywords_Draft6>();
bundle.add<DropNonArrayKeywords_Draft7>();
bundle.add<DropNonArrayKeywordsFormat_2019_09>();
bundle.add<DropNonArrayKeywordsFormat_2020_12>();
bundle.add<DropNonArrayKeywordsUnevaluated_2020_12>();
Expand All @@ -172,6 +201,13 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<DropNonBooleanKeywordsApplicator_2020_12>();
bundle.add<DropNonBooleanKeywordsContent_2019_09>();
bundle.add<DropNonBooleanKeywordsContent_2020_12>();
bundle.add<DropNonBooleanKeywords_Draft0>();
bundle.add<DropNonBooleanKeywords_Draft1>();
bundle.add<DropNonBooleanKeywords_Draft2>();
bundle.add<DropNonBooleanKeywords_Draft3>();
bundle.add<DropNonBooleanKeywords_Draft4>();
bundle.add<DropNonBooleanKeywords_Draft6>();
bundle.add<DropNonBooleanKeywords_Draft7>();
bundle.add<DropNonBooleanKeywordsFormat_2019_09>();
bundle.add<DropNonBooleanKeywordsFormat_2020_12>();
bundle.add<DropNonBooleanKeywordsUnevaluated_2020_12>();
Expand All @@ -181,6 +217,13 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<DropNonNullKeywordsApplicator_2020_12>();
bundle.add<DropNonNullKeywordsContent_2019_09>();
bundle.add<DropNonNullKeywordsContent_2020_12>();
bundle.add<DropNonNullKeywords_Draft0>();
bundle.add<DropNonNullKeywords_Draft1>();
bundle.add<DropNonNullKeywords_Draft2>();
bundle.add<DropNonNullKeywords_Draft3>();
bundle.add<DropNonNullKeywords_Draft4>();
bundle.add<DropNonNullKeywords_Draft6>();
bundle.add<DropNonNullKeywords_Draft7>();
bundle.add<DropNonNullKeywordsFormat_2019_09>();
bundle.add<DropNonNullKeywordsFormat_2020_12>();
bundle.add<DropNonNullKeywordsUnevaluated_2020_12>();
Expand All @@ -190,6 +233,13 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<DropNonNumericKeywordsApplicator_2020_12>();
bundle.add<DropNonNumericKeywordsContent_2019_09>();
bundle.add<DropNonNumericKeywordsContent_2020_12>();
bundle.add<DropNonNumericKeywords_Draft0>();
bundle.add<DropNonNumericKeywords_Draft1>();
bundle.add<DropNonNumericKeywords_Draft2>();
bundle.add<DropNonNumericKeywords_Draft3>();
bundle.add<DropNonNumericKeywords_Draft4>();
bundle.add<DropNonNumericKeywords_Draft6>();
bundle.add<DropNonNumericKeywords_Draft7>();
bundle.add<DropNonNumericKeywordsFormat_2019_09>();
bundle.add<DropNonNumericKeywordsFormat_2020_12>();
bundle.add<DropNonNumericKeywordsUnevaluated_2020_12>();
Expand All @@ -199,13 +249,27 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<DropNonObjectKeywordsApplicator_2020_12>();
bundle.add<DropNonObjectKeywordsContent_2019_09>();
bundle.add<DropNonObjectKeywordsContent_2020_12>();
bundle.add<DropNonObjectKeywords_Draft0>();
bundle.add<DropNonObjectKeywords_Draft1>();
bundle.add<DropNonObjectKeywords_Draft2>();
bundle.add<DropNonObjectKeywords_Draft3>();
bundle.add<DropNonObjectKeywords_Draft4>();
bundle.add<DropNonObjectKeywords_Draft6>();
bundle.add<DropNonObjectKeywords_Draft7>();
bundle.add<DropNonObjectKeywordsFormat_2019_09>();
bundle.add<DropNonObjectKeywordsFormat_2020_12>();
bundle.add<DropNonObjectKeywordsUnevaluated_2020_12>();
bundle.add<DropNonObjectKeywordsValidation_2019_09>();
bundle.add<DropNonObjectKeywordsValidation_2020_12>();
bundle.add<DropNonStringKeywordsApplicator_2019_09>();
bundle.add<DropNonStringKeywordsApplicator_2020_12>();
bundle.add<DropNonStringKeywords_Draft0>();
bundle.add<DropNonStringKeywords_Draft1>();
bundle.add<DropNonStringKeywords_Draft2>();
bundle.add<DropNonStringKeywords_Draft3>();
bundle.add<DropNonStringKeywords_Draft4>();
bundle.add<DropNonStringKeywords_Draft6>();
bundle.add<DropNonStringKeywords_Draft7>();
bundle.add<DropNonStringKeywordsUnevaluated_2020_12>();
bundle.add<DropNonStringKeywordsValidation_2019_09>();
bundle.add<DropNonStringKeywordsValidation_2020_12>();
Expand Down
29 changes: 29 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_draft0.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class DropNonArrayKeywords_Draft0 final : public Rule {
public:
DropNonArrayKeywords_Draft0()
: Rule{"drop_non_array_keywords_draft0",
"Keywords that don't apply to arrays will never match if the "
"instance is guaranteed to be an array"} {};

[[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("http://json-schema.org/draft-00/schema#") &&
schema.is_object() && 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{
"maxDecimal", "maximum", "maximumCanEqual", "minimum",
"minimumCanEqual", "maxLength", "minLength", "pattern",
"requires", "optional", "properties", "additionalProperties",
"contentEncoding", "format"};
};
29 changes: 29 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_draft1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class DropNonArrayKeywords_Draft1 final : public Rule {
public:
DropNonArrayKeywords_Draft1()
: Rule{"drop_non_array_keywords_draft1",
"Keywords that don't apply to arrays will never match if the "
"instance is guaranteed to be an array"} {};

[[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("http://json-schema.org/draft-01/schema#") &&
schema.is_object() && 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{
"maxDecimal", "maximum", "maximumCanEqual", "minimum",
"minimumCanEqual", "maxLength", "minLength", "pattern",
"requires", "optional", "properties", "additionalProperties",
"contentEncoding", "format"};
};
30 changes: 30 additions & 0 deletions src/linter/redundant/drop_non_array_keywords_draft2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class DropNonArrayKeywords_Draft2 final : public Rule {
public:
DropNonArrayKeywords_Draft2()
: Rule{"drop_non_array_keywords_draft2",
"Keywords that don't apply to arrays will never match if the "
"instance is guaranteed to be an array"} {};

[[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("http://json-schema.org/draft-02/schema#") &&
schema.is_object() && 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{
"properties", "optional", "additionalProperties",
"requires", "minimum", "maximum",
"minimumCanEqual", "maximumCanEqual", "pattern",
"maxLength", "minLength", "format",
"contentEncoding", "divisibleBy"};
};
Loading

0 comments on commit 7fc58eb

Please sign in to comment.