diff --git a/src/extension/alterschema/CMakeLists.txt b/src/extension/alterschema/CMakeLists.txt index ee43cbbab..1e7632925 100644 --- a/src/extension/alterschema/CMakeLists.txt +++ b/src/extension/alterschema/CMakeLists.txt @@ -80,6 +80,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema linter/duplicate_examples.h linter/enum_to_const.h linter/equal_numeric_bounds_to_const.h + linter/forbid_empty_enum.h linter/items_array_default.h linter/items_schema_default.h linter/multiple_of_default.h diff --git a/src/extension/alterschema/alterschema.cc b/src/extension/alterschema/alterschema.cc index 131b20c7b..ac8095862 100644 --- a/src/extension/alterschema/alterschema.cc +++ b/src/extension/alterschema/alterschema.cc @@ -108,6 +108,7 @@ inline auto APPLIES_TO_POINTERS(std::vector &&keywords) #include "linter/duplicate_examples.h" #include "linter/enum_to_const.h" #include "linter/equal_numeric_bounds_to_const.h" +#include "linter/forbid_empty_enum.h" #include "linter/items_array_default.h" #include "linter/items_schema_default.h" #include "linter/multiple_of_default.h" @@ -222,6 +223,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void { bundle.add(); bundle.add(); bundle.add(); + bundle.add(); bundle.add(); bundle.add(); bundle.add(); diff --git a/src/extension/alterschema/linter/forbid_empty_enum.h b/src/extension/alterschema/linter/forbid_empty_enum.h new file mode 100644 index 000000000..98713e252 --- /dev/null +++ b/src/extension/alterschema/linter/forbid_empty_enum.h @@ -0,0 +1,35 @@ +class ForbidEmptyEnum final : public SchemaTransformRule { +public: + using mutates = std::true_type; + using reframe_after_transform = std::true_type; + ForbidEmptyEnum() + : SchemaTransformRule{"forbid_empty_enum", + "An empty `enum` validates nothing and is " + "equivalent to `not: {}`"} {}; + + [[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 &, + const sourcemeta::core::SchemaResolver &) const + -> sourcemeta::core::SchemaTransformRule::Result override { + ONLY_CONTINUE_IF(vocabularies.contains_any( + {Vocabularies::Known::JSON_Schema_2020_12_Validation, + Vocabularies::Known::JSON_Schema_2019_09_Validation, + Vocabularies::Known::JSON_Schema_Draft_7, + Vocabularies::Known::JSON_Schema_Draft_6, + Vocabularies::Known::JSON_Schema_Draft_4}) && + schema.is_object() && schema.defines("enum") && + !schema.defines("not") && schema.at("enum").is_array() && + schema.at("enum").empty()); + return APPLIES_TO_KEYWORDS("enum"); + } + + auto transform(JSON &schema, const Result &) const -> void override { + schema.erase("enum"); + schema.assign("not", JSON::make_object()); + } +}; diff --git a/test/alterschema/alterschema_lint_2019_09_test.cc b/test/alterschema/alterschema_lint_2019_09_test.cc index b68b20641..bf6f714f4 100644 --- a/test/alterschema/alterschema_lint_2019_09_test.cc +++ b/test/alterschema/alterschema_lint_2019_09_test.cc @@ -4494,3 +4494,249 @@ TEST(AlterSchema_lint_2019_09, empty_object_as_true_1) { EXPECT_EQ(document, expected); } + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "not": true + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": true + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "enum": [] + } + }, + "$ref": "#/$defs/A" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "not": true + } + }, + "$ref": "#/$defs/A" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "enum": [], + "$defs": { + "inner": { "type": "string" } + } + } + }, + "$ref": "#/$defs/A/$defs/inner" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "$defs": { + "inner": { "type": "string" } + }, + "not": true + } + }, + "$ref": "#/$defs/A/$defs/inner" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "$id": "https://example.com/schemas/A", + "enum": [] + } + }, + "$ref": "https://example.com/schemas/A" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "$id": "https://example.com/schemas/A", + "not": true + } + }, + "$ref": "https://example.com/schemas/A" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, forbid_empty_enum_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "x-note": "placeholder", + "enum": [] + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "x-note": "placeholder", + "not": true + } + } + })JSON"); + + EXPECT_EQ(document, expected); +} diff --git a/test/alterschema/alterschema_lint_2020_12_test.cc b/test/alterschema/alterschema_lint_2020_12_test.cc index ba496eff7..c441b4972 100644 --- a/test/alterschema/alterschema_lint_2020_12_test.cc +++ b/test/alterschema/alterschema_lint_2020_12_test.cc @@ -9664,3 +9664,451 @@ TEST(AlterSchema_lint_2020_12, object_oneof_required_not_required_6) { "can be elevated", true); } + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "not": true + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": true + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "enum": [] + }, + "bar": { + "enum": [] + }, + "baz": { + "enum": [1, 2] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": true + }, + "bar": { + "not": true + }, + "baz": { + "enum": [1, 2] + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [[]], + "prefixItems": [ + { + "enum": [] + }, + { + "type": "string" + } + ] + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [[]], + "prefixItems": [ + { + "not": true + }, + { + "type": "string" + } + ] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [[]], + "properties": { + "arr": { + "type": "array", + "items": { + "x-note": "placeholder", + "enum": [] + } + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [[]], + "properties": { + "arr": { + "type": "array", + "items": { + "x-note": "placeholder", + "not": true + } + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": {} + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": {} + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": { + "enum": [] + } + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": { + "not": true + } + } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_10) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "enum": [] + } + }, + "$ref": "#/$defs/A" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "not": true + } + }, + "$ref": "#/$defs/A" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_11) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "enum": [], + "$defs": { + "inner": { "type": "string" } + } + } + }, + "$ref": "#/$defs/A/$defs/inner" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "$defs": { + "inner": { "type": "string" } + }, + "not": true + } + }, + "$ref": "#/$defs/A/$defs/inner" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_12) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "$id": "https://example.com/schemas/A", + "enum": [] + } + }, + "$ref": "https://example.com/schemas/A" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "$defs": { + "A": { + "$id": "https://example.com/schemas/A", + "not": true + } + }, + "$ref": "https://example.com/schemas/A" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_13) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "x-note": "placeholder", + "enum": [] + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "x-note": "placeholder", + "not": true + } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, forbid_empty_enum_14) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "not": { "type": "string" }, + "enum": [] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "not": { "type": "string" }, + "enum": [] + })JSON"); + + EXPECT_EQ(document, expected); +} diff --git a/test/alterschema/alterschema_lint_draft3_test.cc b/test/alterschema/alterschema_lint_draft3_test.cc index 69aab7efc..b608808d7 100644 --- a/test/alterschema/alterschema_lint_draft3_test.cc +++ b/test/alterschema/alterschema_lint_draft3_test.cc @@ -81,6 +81,34 @@ TEST(AlterSchema_lint_draft3, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft3, forbid_empty_enum_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "title": "Example", + "description": "Example schema", + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "title": "Example", + "description": "Example schema", + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-03/schema#", diff --git a/test/alterschema/alterschema_lint_draft4_test.cc b/test/alterschema/alterschema_lint_draft4_test.cc index 6c62d61a6..038954fb5 100644 --- a/test/alterschema/alterschema_lint_draft4_test.cc +++ b/test/alterschema/alterschema_lint_draft4_test.cc @@ -1812,7 +1812,7 @@ TEST(AlterSchema_lint_draft4, not_false_4) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-04/schema#", "type": "string", - "not": true + "not": {} })JSON"); LINT_AND_FIX(document, result, traces); @@ -1822,7 +1822,7 @@ TEST(AlterSchema_lint_draft4, not_false_4) { const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-04/schema#", "type": "string", - "not": true + "not": {} })JSON"); EXPECT_EQ(document, expected); @@ -2604,3 +2604,97 @@ TEST(AlterSchema_lint_draft4, EXPECT_EQ(document, expected); } + +TEST(AlterSchema_lint_draft4, forbid_empty_enum_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "enum": [] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "not": {} + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, forbid_empty_enum_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "enum": [1, 2] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "enum": [1, 2] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, forbid_empty_enum_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "type": "string" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "type": "string" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, forbid_empty_enum_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Example", + "description": "Example schema", + "properties": { + "foo": { + "not": {} + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} diff --git a/test/alterschema/alterschema_lint_draft6_test.cc b/test/alterschema/alterschema_lint_draft6_test.cc index 6193d80bb..b66a21057 100644 --- a/test/alterschema/alterschema_lint_draft6_test.cc +++ b/test/alterschema/alterschema_lint_draft6_test.cc @@ -2929,3 +2929,105 @@ TEST(AlterSchema_lint_draft6, empty_object_as_true_1) { EXPECT_EQ(document, expected); } + +TEST(AlterSchema_lint_draft6, forbid_empty_enum_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "not": true + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, forbid_empty_enum_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, forbid_empty_enum_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, forbid_empty_enum_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": true + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +} diff --git a/test/alterschema/alterschema_lint_draft7_test.cc b/test/alterschema/alterschema_lint_draft7_test.cc index 14b0734c2..fe231ed98 100644 --- a/test/alterschema/alterschema_lint_draft7_test.cc +++ b/test/alterschema/alterschema_lint_draft7_test.cc @@ -3507,3 +3507,105 @@ TEST(AlterSchema_lint_draft7, "illustrate the expected data", false); } + +TEST(AlterSchema_lint_draft7, forbid_empty_enum_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "not": true + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, forbid_empty_enum_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "enum": [1, 2] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, forbid_empty_enum_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [1], + "type": "string" + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, forbid_empty_enum_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "enum": [] + } + } + })JSON"); + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Example", + "description": "Example schema", + "examples": [{}], + "properties": { + "foo": { + "not": true + } + } + })JSON"); + + LINT_AND_FIX(document, result, traces); + + EXPECT_TRUE(result.first); + EXPECT_EQ(document, expected); +}