Skip to content

Commit

Permalink
Port redundant/pattern_properties_default from JSON BinPack
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 9, 2024
1 parent 4af8b51 commit b29e412
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/linter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/items_schema_default.h
redundant/max_contains_without_contains.h
redundant/min_contains_without_contains.h
redundant/pattern_properties_default.h
redundant/properties_default.h
redundant/then_without_if.h
redundant/unevaluated_items_default.h
Expand Down
2 changes: 2 additions & 0 deletions src/linter/linter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ auto contains_any(const T &container, const T &values) -> bool {
#include "redundant/items_schema_default.h"
#include "redundant/max_contains_without_contains.h"
#include "redundant/min_contains_without_contains.h"
#include "redundant/pattern_properties_default.h"
#include "redundant/properties_default.h"
#include "redundant/then_without_if.h"
#include "redundant/unevaluated_items_default.h"
Expand Down Expand Up @@ -85,6 +86,7 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<ItemsSchemaDefault>();
bundle.add<MaxContainsWithoutContains>();
bundle.add<MinContainsWithoutContains>();
bundle.add<PatternPropertiesDefault>();
bundle.add<PropertiesDefault>();
bundle.add<ThenWithoutIf>();
bundle.add<UnevaluatedItemsDefault>();
Expand Down
28 changes: 28 additions & 0 deletions src/linter/redundant/pattern_properties_default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class PatternPropertiesDefault final : public Rule {
public:
PatternPropertiesDefault()
: Rule{"pattern_properties_default",
"Setting the `patternProperties` keyword to the empty object "
"does not add any further constraint"} {};

[[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 contains_any(
vocabularies,
{"https://json-schema.org/draft/2020-12/vocab/applicator",
"https://json-schema.org/draft/2019-09/vocab/applicator",
"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#"}) &&
schema.is_object() && schema.defines("patternProperties") &&
schema.at("patternProperties").is_object() &&
schema.at("patternProperties").empty();
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase("patternProperties");
}
};
17 changes: 17 additions & 0 deletions test/linter/2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,20 @@ TEST(Lint_2019_09, properties_default) {

EXPECT_EQ(document, expected);
}

TEST(Lint_2019_09, pattern_properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"patternProperties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema"
})JSON");

EXPECT_EQ(document, expected);
}
17 changes: 17 additions & 0 deletions test/linter/2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,20 @@ TEST(Lint_2020_12, properties_default) {

EXPECT_EQ(document, expected);
}

TEST(Lint_2020_12, pattern_properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"patternProperties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema"
})JSON");

EXPECT_EQ(document, expected);
}
17 changes: 17 additions & 0 deletions test/linter/draft3_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,20 @@ TEST(Lint_draft3, properties_default) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft3, pattern_properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#",
"patternProperties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#"
})JSON");

EXPECT_EQ(document, expected);
}
17 changes: 17 additions & 0 deletions test/linter/draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,20 @@ TEST(Lint_draft4, properties_default) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft4, pattern_properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"patternProperties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#"
})JSON");

EXPECT_EQ(document, expected);
}
17 changes: 17 additions & 0 deletions test/linter/draft6_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,20 @@ TEST(Lint_draft6, properties_default) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft6, pattern_properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"patternProperties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#"
})JSON");

EXPECT_EQ(document, expected);
}
17 changes: 17 additions & 0 deletions test/linter/draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,20 @@ TEST(Lint_draft7, properties_default) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft7, pattern_properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"patternProperties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#"
})JSON");

EXPECT_EQ(document, expected);
}

0 comments on commit b29e412

Please sign in to comment.