diff --git a/src/extension/alterschema/CMakeLists.txt b/src/extension/alterschema/CMakeLists.txt index 8a02c1870..142639954 100644 --- a/src/extension/alterschema/CMakeLists.txt +++ b/src/extension/alterschema/CMakeLists.txt @@ -66,7 +66,8 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema linter/property_names_type_default.h linter/property_names_default.h linter/draft_ref_siblings.h - linter/definitions_to_defs.h) + linter/definitions_to_defs.h + linter/non_applicable_enum_validation_keywords.h) if(SOURCEMETA_CORE_INSTALL) sourcemeta_library_install(NAMESPACE sourcemeta PROJECT core NAME alterschema) diff --git a/src/extension/alterschema/alterschema.cc b/src/extension/alterschema/alterschema.cc index 0dcab396c..5d398834a 100644 --- a/src/extension/alterschema/alterschema.cc +++ b/src/extension/alterschema/alterschema.cc @@ -69,6 +69,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/pattern_properties_default.h" #include "linter/properties_default.h" @@ -115,6 +116,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) 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..bd62482c7 --- /dev/null +++ b/src/extension/alterschema/linter/non_applicable_enum_validation_keywords.h @@ -0,0 +1,75 @@ +class EnumValidationKeywordsDefault final : public SchemaTransformRule { +public: + EnumValidationKeywordsDefault() + : SchemaTransformRule{ + "enum_validation_keywords_default", + "Setting validation keywords that do not apply to any item in " + "`enum` is considered an " + "anti-pattern, as the enumeration choices already imply their " + "respective types"} {}; + + [[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.type == sourcemeta::core::SchemaKeywordType::Other || + metadata.type == sourcemeta::core::SchemaKeywordType::Reference) { + continue; + } + + if (metadata.instances.empty()) { + continue; + } + + if (std::ranges::none_of(metadata.instances.cbegin(), + metadata.instances.cend(), + [&enum_types](const auto keyword_type) { + return enum_types.contains(keyword_type); + })) { + this->blacklist.emplace_back(entry.first); + } + } + + return !this->blacklist.empty(); + } + + 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 9da73da18..74923e928 100644 --- a/test/alterschema/alterschema_lint_2019_09_test.cc +++ b/test/alterschema/alterschema_lint_2019_09_test.cc @@ -159,6 +159,175 @@ 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, 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 02601a6c3..dab5cd2f6 100644 --- a/test/alterschema/alterschema_lint_2020_12_test.cc +++ b/test/alterschema/alterschema_lint_2020_12_test.cc @@ -2814,3 +2814,253 @@ TEST(AlterSchema_lint_2020_12, non_applicable_type_specific_keywords_3) { 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", + "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..f0c18e4f6 100644 --- a/test/alterschema/alterschema_lint_draft1_test.cc +++ b/test/alterschema/alterschema_lint_draft1_test.cc @@ -73,6 +73,196 @@ 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, 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..f4e07399e 100644 --- a/test/alterschema/alterschema_lint_draft2_test.cc +++ b/test/alterschema/alterschema_lint_draft2_test.cc @@ -73,6 +73,196 @@ 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, 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..cdd96bb79 100644 --- a/test/alterschema/alterschema_lint_draft3_test.cc +++ b/test/alterschema/alterschema_lint_draft3_test.cc @@ -73,6 +73,196 @@ 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, 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 ecf73f451..3be6261c4 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#", diff --git a/test/alterschema/alterschema_lint_draft6_test.cc b/test/alterschema/alterschema_lint_draft6_test.cc index 4f0f5bce7..8f23f82a5 100644 --- a/test/alterschema/alterschema_lint_draft6_test.cc +++ b/test/alterschema/alterschema_lint_draft6_test.cc @@ -159,6 +159,175 @@ 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, 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 82411ca25..b5fa13541 100644 --- a/test/alterschema/alterschema_lint_draft7_test.cc +++ b/test/alterschema/alterschema_lint_draft7_test.cc @@ -159,6 +159,175 @@ 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, single_type_array_1) { sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({ "$schema": "http://json-schema.org/draft-07/schema#",