Skip to content

Commit daa40b3

Browse files
committed
Don't leak the additionalItems keyword into JSON Schema draft 2020-12
In 2020-12 it is called 'items', which of course is already supported.
1 parent 8cff13d commit daa40b3

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ v4.19.2
22
=======
33

44
* Fix the error message for additional items when used with heterogeneous arrays.
5+
* Don't leak the ``additionalItems`` keyword into JSON Schema draft 2020-12, where it was replaced by ``items``.
56

67
v4.19.1
78
=======

jsonschema/_keywords.py

-18
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,6 @@ def items(validator, items, instance, schema):
8383
)
8484

8585

86-
def additionalItems(validator, aI, instance, schema):
87-
if (
88-
not validator.is_type(instance, "array")
89-
or validator.is_type(schema.get("items", {}), "object")
90-
):
91-
return
92-
93-
len_items = len(schema.get("items", []))
94-
if validator.is_type(aI, "object"):
95-
for index, item in enumerate(instance[len_items:], start=len_items):
96-
yield from validator.descend(item, aI, path=index)
97-
elif not aI and len(instance) > len(schema.get("items", [])):
98-
error = "Additional items are not allowed (%s %s unexpected)"
99-
yield ValidationError(
100-
error % extras_msg(instance[len(schema.get("items", [])):]),
101-
)
102-
103-
10486
def const(validator, const, instance, schema):
10587
if not equal(instance, const):
10688
yield ValidationError(f"{const!r} was expected")

jsonschema/_legacy_keywords.py

+18
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ def items_draft3_draft4(validator, items, instance, schema):
101101
)
102102

103103

104+
def additionalItems(validator, aI, instance, schema):
105+
if (
106+
not validator.is_type(instance, "array")
107+
or validator.is_type(schema.get("items", {}), "object")
108+
):
109+
return
110+
111+
len_items = len(schema.get("items", []))
112+
if validator.is_type(aI, "object"):
113+
for index, item in enumerate(instance[len_items:], start=len_items):
114+
yield from validator.descend(item, aI, path=index)
115+
elif not aI and len(instance) > len(schema.get("items", [])):
116+
error = "Additional items are not allowed (%s %s unexpected)"
117+
yield ValidationError(
118+
error % _utils.extras_msg(instance[len(schema.get("items", [])):]),
119+
)
120+
121+
104122
def items_draft6_draft7_draft201909(validator, items, instance, schema):
105123
if not validator.is_type(instance, "array"):
106124
return

jsonschema/validators.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def extend(
587587
),
588588
validators={
589589
"$ref": _keywords.ref,
590-
"additionalItems": _keywords.additionalItems,
590+
"additionalItems": _legacy_keywords.additionalItems,
591591
"additionalProperties": _keywords.additionalProperties,
592592
"dependencies": _legacy_keywords.dependencies_draft3,
593593
"disallow": _legacy_keywords.disallow_draft3,
@@ -621,7 +621,7 @@ def extend(
621621
),
622622
validators={
623623
"$ref": _keywords.ref,
624-
"additionalItems": _keywords.additionalItems,
624+
"additionalItems": _legacy_keywords.additionalItems,
625625
"additionalProperties": _keywords.additionalProperties,
626626
"allOf": _keywords.allOf,
627627
"anyOf": _keywords.anyOf,
@@ -660,7 +660,7 @@ def extend(
660660
),
661661
validators={
662662
"$ref": _keywords.ref,
663-
"additionalItems": _keywords.additionalItems,
663+
"additionalItems": _legacy_keywords.additionalItems,
664664
"additionalProperties": _keywords.additionalProperties,
665665
"allOf": _keywords.allOf,
666666
"anyOf": _keywords.anyOf,
@@ -704,7 +704,7 @@ def extend(
704704
),
705705
validators={
706706
"$ref": _keywords.ref,
707-
"additionalItems": _keywords.additionalItems,
707+
"additionalItems": _legacy_keywords.additionalItems,
708708
"additionalProperties": _keywords.additionalProperties,
709709
"allOf": _keywords.allOf,
710710
"anyOf": _keywords.anyOf,
@@ -750,7 +750,7 @@ def extend(
750750
validators={
751751
"$recursiveRef": _legacy_keywords.recursiveRef,
752752
"$ref": _keywords.ref,
753-
"additionalItems": _keywords.additionalItems,
753+
"additionalItems": _legacy_keywords.additionalItems,
754754
"additionalProperties": _keywords.additionalProperties,
755755
"allOf": _keywords.allOf,
756756
"anyOf": _keywords.anyOf,
@@ -797,7 +797,6 @@ def extend(
797797
validators={
798798
"$dynamicRef": _keywords.dynamicRef,
799799
"$ref": _keywords.ref,
800-
"additionalItems": _keywords.additionalItems,
801800
"additionalProperties": _keywords.additionalProperties,
802801
"allOf": _keywords.allOf,
803802
"anyOf": _keywords.anyOf,

0 commit comments

Comments
 (0)