Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT blaze NAME alterschema
common/exclusive_minimum_number_and_minimum.h
common/if_without_then_else.h
common/ignored_metaschema.h
common/incoherent_exclusive_limits.h
common/max_contains_without_contains.h
common/maximum_real_for_integer.h
common/min_contains_without_contains.h
Expand Down
3 changes: 3 additions & 0 deletions src/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ auto WALK_UP_IN_PLACE_APPLICATORS(const JSON &root, const SchemaFrame &frame,
#include "common/flatten_nested_extends.h"
#include "common/if_without_then_else.h"
#include "common/ignored_metaschema.h"
#include "common/incoherent_exclusive_limits.h"
#include "common/max_contains_without_contains.h"
#include "common/maximum_real_for_integer.h"
#include "common/min_contains_without_contains.h"
Expand Down Expand Up @@ -339,6 +340,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
if (mode == AlterSchemaMode::Canonicalizer) {
bundle.add<ExclusiveMinimumBooleanIntegerFold>();
bundle.add<ExclusiveMaximumBooleanIntegerFold>();
bundle.add<IncoherentExclusiveLimits>();
bundle.add<UnsatisfiableExclusiveEqualBounds>();
bundle.add<MinimumCanEqualIntegerFold>();
bundle.add<MaximumCanEqualIntegerFold>();
Expand Down Expand Up @@ -464,6 +466,7 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode) -> void {
bundle.add<UnevaluatedItemsDefault>();
bundle.add<UnevaluatedPropertiesDefault>();
bundle.add<UnsatisfiableMaxContains>();
bundle.add<IncoherentExclusiveLimits>();
bundle.add<IncoherentMinMaxContains>();
bundle.add<UnsatisfiableMinProperties>();
bundle.add<EnumToConst>();
Expand Down
60 changes: 60 additions & 0 deletions src/alterschema/common/incoherent_exclusive_limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class IncoherentExclusiveLimits final : public SchemaTransformRule {
public:
using mutates = std::true_type;
using reframe_after_transform = std::true_type;
IncoherentExclusiveLimits()
: SchemaTransformRule{
"incoherent_exclusive_limits",
"`exclusiveMinimum` greater than or equal to "
"`exclusiveMaximum` makes the schema unsatisfiable"} {};

[[nodiscard]] auto
condition(const sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON &, const Vocabularies &vocabularies,
const SchemaFrame &, const SchemaFrame::Location &,
const SchemaWalker &, const SchemaResolver &) const
-> SchemaTransformRule::Result override {
ONLY_CONTINUE_IF(schema.is_object());
ONLY_CONTINUE_IF(vocabularies.contains_any(
{Vocabularies::Known::JSON_Schema_2020_12_Validation,
Vocabularies::Known::JSON_Schema_2019_09_Validation,
Vocabularies::Known::JSON_Schema_Draft_7,
Vocabularies::Known::JSON_Schema_Draft_6}));

const auto *exclusive_minimum{schema.try_at("exclusiveMinimum")};
ONLY_CONTINUE_IF(exclusive_minimum && exclusive_minimum->is_number());
const auto *exclusive_maximum{schema.try_at("exclusiveMaximum")};
ONLY_CONTINUE_IF(exclusive_maximum && exclusive_maximum->is_number() &&
*exclusive_minimum >= *exclusive_maximum);
Comment thread
AcEKaycgR marked this conversation as resolved.
return APPLIES_TO_KEYWORDS("exclusiveMinimum", "exclusiveMaximum");
}

auto transform(JSON &schema, const Result &) const -> void override {
auto not_false =
JSON::make_object(); // {} = always true, not {} = never valid

if (!schema.defines("not") && !schema.defines("allOf")) {
schema.assign("not", std::move(not_false));
} else {
auto new_entry = JSON::make_object();
new_entry.assign("not", std::move(not_false));

if (schema.defines("allOf") && schema.at("allOf").is_array()) {
schema.at("allOf").push_back(std::move(new_entry));
} else {
auto all_of = JSON::make_array();
if (schema.defines("not")) {
auto existing_not = JSON::make_object();
existing_not.assign("not", schema.at("not"));
all_of.push_back(std::move(existing_not));
schema.erase("not");
}
all_of.push_back(std::move(new_entry));
schema.assign("allOf", std::move(all_of));
}
}

schema.erase("exclusiveMinimum");
schema.erase("exclusiveMaximum");
}
};
124 changes: 124 additions & 0 deletions test/alterschema/alterschema_canonicalize_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,130 @@ TEST_F(Canonicalizer201909Test, exclusive_minimum_integer_to_minimum_3) {
CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer201909Test, incoherent_exclusive_limits_1) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"exclusiveMinimum": 3,
"exclusiveMaximum": 3
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"not": true
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer201909Test, incoherent_exclusive_limits_2) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"exclusiveMinimum": 5,
"exclusiveMaximum": 2
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"not": true
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer201909Test, incoherent_exclusive_limits_3) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"exclusiveMinimum": 5,
"exclusiveMaximum": 2,
"not": { "type": "string" }
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"allOf": [
{
"not": { "type": "string", "minLength": 0 }
},
{
"not": true
}
]
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer201909Test, incoherent_exclusive_limits_4) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$ref": "#/$defs/test/additionalProperties",
"$defs": {
"test": {
"additionalProperties": {
"type": "string"
},
"exclusiveMaximum": 5,
"exclusiveMinimum": 8
}
}
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$defs": {
"test": {
"allOf": [
{
"not": true
},
{
"anyOf": [
{
"enum": [ null ]
},
{
"enum": [ false, true ]
},
{
"type": "object",
"additionalProperties": {
"type": "string",
"minLength": 0
},
"patternProperties": {},
"propertyNames": true,
"minProperties": 0,
"properties": {}
},
{
"type": "array",
"uniqueItems": false,
"minItems": 0,
"contains": true,
"minContains": 0,
"items": true
},
{
"type": "string",
"minLength": 0
},
{
"type": "number"
}
]
}
]
}
},
"allOf": [
{
"$ref": "#/$defs/test/allOf/1/anyOf/2/additionalProperties"
}
]
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}
TEST_F(Canonicalizer201909Test, exclusive_minimum_integer_to_minimum_5) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
Expand Down
124 changes: 124 additions & 0 deletions test/alterschema/alterschema_canonicalize_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,130 @@ TEST_F(Canonicalizer202012Test, exclusive_minimum_integer_to_minimum_3) {
CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer202012Test, incoherent_exclusive_limits_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"exclusiveMinimum": 3,
"exclusiveMaximum": 3
})JSON");

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"not": true
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer202012Test, incoherent_exclusive_limits_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"exclusiveMinimum": 5,
"exclusiveMaximum": 2
})JSON");

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"not": true
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer202012Test, incoherent_exclusive_limits_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"exclusiveMinimum": 5,
"exclusiveMaximum": 2,
"not": { "type": "string" }
})JSON");

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"allOf": [
{
"not": { "type": "string", "minLength": 0 }
},
{
"not": true
}
]
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(Canonicalizer202012Test, incoherent_exclusive_limits_4) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/test/additionalProperties",
"$defs": {
"test": {
"additionalProperties": {
"type": "string"
},
"exclusiveMaximum": 5,
"exclusiveMinimum": 8
}
}
})JSON");

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"test": {
"allOf": [
{
"not": true
},
{
"anyOf": [
{
"enum": [ null ]
},
{
"enum": [ false, true ]
},
{
"type": "object",
"additionalProperties": {
"type": "string",
"minLength": 0
},
"patternProperties": {},
"propertyNames": true,
"minProperties": 0,
"properties": {}
},
{
"type": "array",
"uniqueItems": false,
"minItems": 0,
"contains": true,
"minContains": 0,
"items": true
},
{
"type": "string",
"minLength": 0
},
{
"type": "number"
}
]
}
]
}
},
"allOf": [
{
"$ref": "#/$defs/test/allOf/1/anyOf/2/additionalProperties"
}
]
})JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}
TEST_F(Canonicalizer202012Test, exclusive_minimum_integer_to_minimum_5) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down
Loading
Loading