diff --git a/src/extension/alterschema/CMakeLists.txt b/src/extension/alterschema/CMakeLists.txt index e5bf1f228..528bcbe2c 100644 --- a/src/extension/alterschema/CMakeLists.txt +++ b/src/extension/alterschema/CMakeLists.txt @@ -67,6 +67,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema linter/property_names_default.h linter/draft_ref_siblings.h linter/definitions_to_defs.h + linter/non_applicable_enum_validation_keywords.h # Strict strict/required_properties_in_properties.h) diff --git a/src/extension/alterschema/alterschema.cc b/src/extension/alterschema/alterschema.cc index 8383c6477..5896ee98b 100644 --- a/src/extension/alterschema/alterschema.cc +++ b/src/extension/alterschema/alterschema.cc @@ -67,6 +67,7 @@ contains_any(const Vocabularies &container, #include "linter/minimum_real_for_integer.h" #include "linter/modern_official_dialect_with_empty_fragment.h" #include "linter/multiple_of_default.h" +#include "linter/non_applicable_enum_validation_keywords.h" #include "linter/non_applicable_type_specific_keywords.h" #include "linter/not_false.h" #include "linter/pattern_properties_default.h" @@ -116,6 +117,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/non_applicable_enum_validation_keywords.h b/src/extension/alterschema/linter/non_applicable_enum_validation_keywords.h new file mode 100644 index 000000000..2ef36b023 --- /dev/null +++ b/src/extension/alterschema/linter/non_applicable_enum_validation_keywords.h @@ -0,0 +1,77 @@ +class NonApplicableEnumValidationKeywords final : public SchemaTransformRule { +public: + NonApplicableEnumValidationKeywords() + : SchemaTransformRule{ + "enum_validation_keywords_default", + "Setting validation keywords that do not apply to any item in " + "`enum` is considered an anti-pattern"} {}; + + [[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 &walker, + const sourcemeta::core::SchemaResolver &) const + -> sourcemeta::core::SchemaTransformRule::Result override { + if (!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/schema#", + "http://json-schema.org/draft-02/hyper-schema#", + "http://json-schema.org/draft-01/schema#", + "http://json-schema.org/draft-01/hyper-schema#"}) || + !schema.is_object() || !schema.defines("enum") || + !schema.at("enum").is_array()) { + return false; + } + + std::set enum_types; + for (const auto &value : schema.at("enum").as_array()) { + enum_types.emplace(value.type()); + } + + if (enum_types.empty()) { + return false; + } + + this->blacklist.clear(); + for (const auto &entry : schema.as_object()) { + const auto metadata = walker(entry.first, vocabularies); + + if (metadata.instances.empty()) { + continue; + } + + if (std::ranges::none_of(metadata.instances, + [&enum_types](const auto keyword_type) { + return enum_types.contains(keyword_type); + })) { + this->blacklist.emplace_back(entry.first); + } + } + + if (this->blacklist.empty()) { + return false; + } + + std::ostringstream message; + for (const auto &entry : this->blacklist) { + message << "- " << entry << "\n"; + } + + return message.str(); + } + + auto transform(sourcemeta::core::JSON &schema) const -> void override { + schema.erase_keys(this->blacklist.cbegin(), this->blacklist.cend()); + } + +private: + mutable std::vector blacklist; +}; diff --git a/test/alterschema/alterschema_lint_2019_09_test.cc b/test/alterschema/alterschema_lint_2019_09_test.cc index 507ebb44e..06b52aadb 100644 --- a/test/alterschema/alterschema_lint_2019_09_test.cc +++ b/test/alterschema/alterschema_lint_2019_09_test.cc @@ -159,6 +159,207 @@ TEST(AlterSchema_lint_2019_09, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2019_09, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "$id": "https://example.com/schema", + "$anchor": "color-enum", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "$comment": "This schema uses enum with various annotation keywords", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2, + "minProperties": 1, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "$id": "https://example.com/schema", + "$anchor": "color-enum", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "$comment": "This schema uses enum with various annotation keywords", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_2019_09, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "https://json-schema.org/draft/2019-09/schema", diff --git a/test/alterschema/alterschema_lint_2020_12_test.cc b/test/alterschema/alterschema_lint_2020_12_test.cc index 13e5d02d4..0e0589af6 100644 --- a/test/alterschema/alterschema_lint_2020_12_test.cc +++ b/test/alterschema/alterschema_lint_2020_12_test.cc @@ -3009,3 +3009,287 @@ TEST(AlterSchema_lint_2020_12, required_properties_in_properties_5) { EXPECT_EQ(document, expected); } + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, 2, 3 ], + "minLength": 3, + "pattern": "^[a-z]+$" + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "one", "two" ], + "minimum": 0, + "maximum": 100, + "multipleOf": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "one", "two" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ true, false ], + "minLength": 1, + "minimum": 0, + "minItems": 1, + "minProperties": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ true, false ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://example.com/schema", + "$anchor": "color-enum", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "$comment": "This schema uses enum with various annotation keywords", + "examples": [ "red", "blue" ], + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2, + "minProperties": 1, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://example.com/schema", + "$anchor": "color-enum", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "$comment": "This schema uses enum with various annotation keywords", + "examples": [ "red", "blue" ], + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, non_applicable_enum_validation_keywords_10) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, 2, 3 ], + "minLength": 2, + "maxLength": 10, + "pattern": "^[a-z]+$" + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, + non_applicable_enum_validation_keywords_with_ref) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$ref": "#/$defs/myType", + "enum": [ "foo", "bar" ], + "minimum": 10, + "maxItems": 5, + "$defs": { + "myType": { + "type": "string" + } + } + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$ref": "#/$defs/myType", + "enum": [ "foo", "bar" ], + "$defs": { + "myType": { + "type": "string" + } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_2020_12, + non_applicable_enum_validation_keywords_universal_keywords) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "foo", "bar" ], + "title": "My enum", + "description": "A test enum", + "default": "foo", + "examples": [ "foo" ], + "minimum": 10, + "maxItems": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "enum": [ "foo", "bar" ], + "title": "My enum", + "description": "A test enum", + "default": "foo", + "examples": [ "foo" ] + })JSON"); + + EXPECT_EQ(document, expected); +} diff --git a/test/alterschema/alterschema_lint_draft1_test.cc b/test/alterschema/alterschema_lint_draft1_test.cc index ca14fe71e..3edf0a975 100644 --- a/test/alterschema/alterschema_lint_draft1_test.cc +++ b/test/alterschema/alterschema_lint_draft1_test.cc @@ -73,6 +73,222 @@ TEST(AlterSchema_lint_draft1, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "x-foo-bar": 1, + "maxLength": 100, + "maxItems": 10, + "x-baz-qux": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "x-foo-bar": 1, + "maxLength": 100, + "maxItems": 10, + "x-baz-qux": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, + non_applicable_enum_validation_keywords_unknown_keyword) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ "foo", "bar" ], + "x-unknown-keyword": "value", + "x-custom-prop": 42 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "enum": [ "foo", "bar" ], + "x-unknown-keyword": "value", + "x-custom-prop": 42 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft1, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-01/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft1, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-01/schema#", diff --git a/test/alterschema/alterschema_lint_draft2_test.cc b/test/alterschema/alterschema_lint_draft2_test.cc index 9f404087f..86ba8ce4e 100644 --- a/test/alterschema/alterschema_lint_draft2_test.cc +++ b/test/alterschema/alterschema_lint_draft2_test.cc @@ -73,6 +73,222 @@ TEST(AlterSchema_lint_draft2, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "x-foo-bar": 1, + "maxLength": 100, + "maxItems": 10, + "x-baz-qux": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "x-foo-bar": 1, + "maxLength": 100, + "maxItems": 10, + "x-baz-qux": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, + non_applicable_enum_validation_keywords_unknown_keyword) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ "foo", "bar" ], + "x-unknown-keyword": "value", + "x-custom-prop": 42 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "enum": [ "foo", "bar" ], + "x-unknown-keyword": "value", + "x-custom-prop": 42 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft2, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-02/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft2, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-02/schema#", diff --git a/test/alterschema/alterschema_lint_draft3_test.cc b/test/alterschema/alterschema_lint_draft3_test.cc index 6e97bbb11..68aafa667 100644 --- a/test/alterschema/alterschema_lint_draft3_test.cc +++ b/test/alterschema/alterschema_lint_draft3_test.cc @@ -73,6 +73,222 @@ TEST(AlterSchema_lint_draft3, enum_with_type_4) { 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#", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "x-foo-bar": 1, + "maxLength": 100, + "maxItems": 10, + "x-baz-qux": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "x-foo-bar": 1, + "maxLength": 100, + "maxItems": 10, + "x-baz-qux": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, + non_applicable_enum_validation_keywords_unknown_keyword) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ "foo", "bar" ], + "x-unknown-keyword": "value", + "x-custom-prop": 42 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "enum": [ "foo", "bar" ], + "x-unknown-keyword": "value", + "x-custom-prop": 42 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft3, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-03/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft3, single_type_array_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 ac5a58b4e..906352c63 100644 --- a/test/alterschema/alterschema_lint_draft4_test.cc +++ b/test/alterschema/alterschema_lint_draft4_test.cc @@ -73,6 +73,175 @@ TEST(AlterSchema_lint_draft4, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft4, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-04/schema#", @@ -89,6 +258,34 @@ TEST(AlterSchema_lint_draft4, single_type_array_1) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft4, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2, + "minProperties": 1, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft4, additional_properties_default_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-04/schema#", diff --git a/test/alterschema/alterschema_lint_draft6_test.cc b/test/alterschema/alterschema_lint_draft6_test.cc index dbd589b6e..5391a56a8 100644 --- a/test/alterschema/alterschema_lint_draft6_test.cc +++ b/test/alterschema/alterschema_lint_draft6_test.cc @@ -159,6 +159,203 @@ TEST(AlterSchema_lint_draft6, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft6, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2, + "minProperties": 1, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft6, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-06/schema#", diff --git a/test/alterschema/alterschema_lint_draft7_test.cc b/test/alterschema/alterschema_lint_draft7_test.cc index 5a8f8b20f..d5f7ae623 100644 --- a/test/alterschema/alterschema_lint_draft7_test.cc +++ b/test/alterschema/alterschema_lint_draft7_test.cc @@ -159,6 +159,205 @@ TEST(AlterSchema_lint_draft7, enum_with_type_4) { EXPECT_EQ(document, expected); } +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_1) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ "foo", "bar" ], + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ "foo", "bar" ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_2) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 1, 2, 3 ], + "minLength": 0, + "maxLength": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 1, 2, 3 ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_3) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ], + "minLength": 3, + "minimum": 0 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ { "a": 1 }, { "b": 2 } ] + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_4) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 1, "foo" ], + "minLength": 2 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_5) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0, + "minItems": 1 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 1, "foo" ], + "minLength": 2, + "minimum": 0 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_6) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + }, + "minLength": 5, + "minimum": 10 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ { "name": "alice" }, { "age": 25 } ], + "properties": { + "name": { "type": "string" }, + "age": { "type": "number" } + } + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_7) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3, + "minItems": 2 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ "small", "medium", "large" ], + "title": "Size Options", + "minLength": 3 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_8) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "enum": [ 42, "hello", true, null, { "key": "value" }, [1, 2, 3] ], + "minimum": 10, + "minLength": 2, + "minItems": 1, + "minProperties": 1, + "maxLength": 100, + "maxItems": 10, + "maxProperties": 5 + })JSON"); + + EXPECT_EQ(document, expected); +} + +TEST(AlterSchema_lint_draft7, non_applicable_enum_validation_keywords_9) { + sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "$comment": "This schema uses enum with various annotation keywords", + "enum": [ "red", "green", "blue" ], + "minimum": 5, + "minLength": 10, + "minItems": 2, + "minProperties": 1, + "maxProperties": 5 + })JSON"); + + LINT_AND_FIX_FOR_READABILITY(document); + + const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://example.com/schema", + "title": "My Enum Schema", + "description": "A schema with enum and annotations", + "$comment": "This schema uses enum with various annotation keywords", + "enum": [ "red", "green", "blue" ], + "minLength": 10 + })JSON"); + + EXPECT_EQ(document, expected); +} + TEST(AlterSchema_lint_draft7, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-07/schema#",