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
34 changes: 34 additions & 0 deletions src/alterschema/common/incoherent_exclusive_limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class IncoherentExclusiveLimits final : public SchemaTransformRule {
public:
using mutates = std::true_type;
using reframe_after_transform = std::false_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 {
schema.into(JSON{false});
Copy link
Copy Markdown
Member

@jviotti jviotti May 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, but you will have to guard against $ref's. Consider a schema like this, which is weird, but definitely valid:

{
  "$ref": "#/$defs/test/additionalProperties"
  "$defs": {
    "test": {
      "additionalProperties": {
        "type": "string"
      },
      "exclusiveMaximum": 5,
      "exclusiveMinimum": 8
    }
  }
}

By unconditionally converting $defs/test to false you break the reference. So try to think of potential adversarial tests for how you can break the rule. Also maybe if the schema has a $dynamicAnchor, etc.

If I recall correctly, in other cases, for this reason, what we do is NOT convert the whole thing to false but just add not: {}, or if not is already there, add it into an existing or new allOf

}
};
24 changes: 24 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,30 @@ 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("false");

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("false");

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
24 changes: 24 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,30 @@ 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("false");

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("false");

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
28 changes: 28 additions & 0 deletions test/alterschema/alterschema_canonicalize_draft6_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ TEST_F(CanonicalizerDraft6Test, integer_both_exclusive_bounds_fold) {
CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(CanonicalizerDraft6Test, incoherent_exclusive_limits_1) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"exclusiveMinimum": 3,
"exclusiveMaximum": 3
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON(
false
)JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(CanonicalizerDraft6Test, incoherent_exclusive_limits_2) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"exclusiveMinimum": 5,
"exclusiveMaximum": 2
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON(
false
)JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(CanonicalizerDraft6Test, number_bare) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
Expand Down
28 changes: 28 additions & 0 deletions test/alterschema/alterschema_canonicalize_draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ TEST_F(CanonicalizerDraft7Test, integer_both_exclusive_bounds_fold) {
CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(CanonicalizerDraft7Test, incoherent_exclusive_limits_1) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"exclusiveMinimum": 3,
"exclusiveMaximum": 3
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON(
false
)JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(CanonicalizerDraft7Test, incoherent_exclusive_limits_2) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"exclusiveMinimum": 5,
"exclusiveMaximum": 2
})JSON");

const auto expected = sourcemeta::core::parse_json(R"JSON(
false
)JSON");

CANONICALIZE_AND_VALIDATE(document, expected, *compiled_meta_);
}

TEST_F(CanonicalizerDraft7Test, number_bare) {
auto document = sourcemeta::core::parse_json(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
86 changes: 86 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,92 @@ TEST(AlterSchema_lint_2019_09, incoherent_min_max_contains_9) {
true);
}

TEST(AlterSchema_lint_2019_09, incoherent_exclusive_limits_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 1,
"exclusiveMaximum": 5
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2019_09, incoherent_exclusive_limits_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 1
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2019_09, incoherent_exclusive_limits_3) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 3,
Comment thread
AcEKaycgR marked this conversation as resolved.
"exclusiveMaximum": 3
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "incoherent_exclusive_limits",
"`exclusiveMinimum` greater than or equal to "
"`exclusiveMaximum` makes the schema unsatisfiable",
true);
}

TEST(AlterSchema_lint_2019_09, incoherent_exclusive_limits_4) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 5,
"exclusiveMaximum": 2
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "incoherent_exclusive_limits",
"`exclusiveMinimum` greater than or equal to "
"`exclusiveMaximum` makes the schema unsatisfiable",
true);
}

TEST(AlterSchema_lint_2019_09, incoherent_exclusive_limits_5) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMaximum": 5
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2019_09, equal_numeric_bounds_to_const_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
Expand Down
86 changes: 86 additions & 0 deletions test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,92 @@ TEST(AlterSchema_lint_2020_12, incoherent_min_max_contains_9) {
true);
}

TEST(AlterSchema_lint_2020_12, incoherent_exclusive_limits_1) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 1,
"exclusiveMaximum": 5
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2020_12, incoherent_exclusive_limits_2) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 1
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2020_12, incoherent_exclusive_limits_3) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 3,
"exclusiveMaximum": 3
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "incoherent_exclusive_limits",
"`exclusiveMinimum` greater than or equal to "
"`exclusiveMaximum` makes the schema unsatisfiable",
true);
}

TEST(AlterSchema_lint_2020_12, incoherent_exclusive_limits_4) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMinimum": 5,
"exclusiveMaximum": 2
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_FALSE(result.first);
EXPECT_EQ(traces.size(), 1);
EXPECT_LINT_TRACE(traces, 0, "", "incoherent_exclusive_limits",
"`exclusiveMinimum` greater than or equal to "
"`exclusiveMaximum` makes the schema unsatisfiable",
true);
}

TEST(AlterSchema_lint_2020_12, incoherent_exclusive_limits_5) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Test",
"description": "A test schema",
"examples": [ "foo" ],
"exclusiveMaximum": 5
})JSON");

LINT_WITHOUT_FIX(document, result, traces);

EXPECT_TRUE(result.first);
EXPECT_EQ(traces.size(), 0);
}

TEST(AlterSchema_lint_2020_12, equal_numeric_bounds_to_const_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down
Loading
Loading