Skip to content

Commit

Permalink
Lint properties set to the empty object
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 8e4f0af commit 67d0190
Show file tree
Hide file tree
Showing 11 changed files with 169 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/properties_default.h
redundant/then_without_if.h
redundant/unevaluated_items_default.h
redundant/unevaluated_properties_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/properties_default.h"
#include "redundant/then_without_if.h"
#include "redundant/unevaluated_items_default.h"
#include "redundant/unevaluated_properties_default.h"
Expand Down Expand Up @@ -84,6 +85,7 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<ItemsSchemaDefault>();
bundle.add<MaxContainsWithoutContains>();
bundle.add<MinContainsWithoutContains>();
bundle.add<PropertiesDefault>();
bundle.add<ThenWithoutIf>();
bundle.add<UnevaluatedItemsDefault>();
bundle.add<UnevaluatedPropertiesDefault>();
Expand Down
30 changes: 30 additions & 0 deletions src/linter/redundant/properties_default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class PropertiesDefault final : public Rule {
public:
PropertiesDefault()
: Rule{"properties_default",
"Setting the `properties` 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#",
"http://json-schema.org/draft-02/hyper-schema#",
"http://json-schema.org/draft-01/hyper-schema#"}) &&
schema.is_object() && schema.defines("properties") &&
schema.at("properties").is_object() &&
schema.at("properties").empty();
}

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

EXPECT_EQ(document, expected);
}

TEST(Lint_2019_09, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"properties": {}
})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 @@ -806,3 +806,20 @@ TEST(Lint_2020_12, dependent_required_tautology_2) {

EXPECT_EQ(document, expected);
}

TEST(Lint_2020_12, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {}
})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/draft1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,20 @@ TEST(Lint_draft1, minimum_real_for_integer_1) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft1, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-01/schema#",
"properties": {}
})JSON");

LINT_AND_FIX(document);

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

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

EXPECT_EQ(document, expected);
}

TEST(Lint_draft2, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-02/schema#",
"properties": {}
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-02/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 @@ -268,3 +268,20 @@ TEST(Lint_draft3, dependent_required_tautology_2) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft3, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#",
"properties": {}
})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 @@ -322,3 +322,20 @@ TEST(Lint_draft4, dependent_required_tautology_2) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft4, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {}
})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 @@ -473,3 +473,20 @@ TEST(Lint_draft6, dependent_required_tautology_2) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft6, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {}
})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 @@ -564,3 +564,20 @@ TEST(Lint_draft7, dependent_required_tautology_2) {

EXPECT_EQ(document, expected);
}

TEST(Lint_draft7, properties_default) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {}
})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 67d0190

Please sign in to comment.