Skip to content

Commit 58b015e

Browse files
committed
Only validate unevaluated props/items on applicable types
1 parent fb85c66 commit 58b015e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

jsonschema/_validators.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ def if_(validator, if_schema, instance, schema):
418418

419419

420420
def unevaluatedItems(validator, unevaluatedItems, instance, schema):
421+
if not validator.is_type(instance, "array"):
422+
return
421423
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(
422424
validator, instance, schema,
423425
)
@@ -431,6 +433,8 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema):
431433

432434

433435
def unevaluatedProperties(validator, unevaluatedProperties, instance, schema):
436+
if not validator.is_type(instance, "object"):
437+
return
434438
evaluated_property_keys = find_evaluated_property_keys_by_schema(
435439
validator, instance, schema,
436440
)

jsonschema/tests/test_validators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ def test_unevaluated_items(self):
597597
"Unevaluated items are not allowed ('bar', 'foo' were unexpected)",
598598
)
599599

600+
def test_unevaluated_items_on_invalid_type(self):
601+
schema = {"type": "array", "unevaluatedItems": False}
602+
message = self.message_for(instance="foo", schema=schema)
603+
self.assertEqual(message, f"'foo' is not of type 'array'")
604+
600605
def test_unevaluated_properties(self):
601606
schema = {"type": "object", "unevaluatedProperties": False}
602607
message = self.message_for(
@@ -612,6 +617,11 @@ def test_unevaluated_properties(self):
612617
"('bar', 'foo' were unexpected)",
613618
)
614619

620+
def test_unevaluated_properties_on_invalid_type(self):
621+
schema = {"type": "object", "unevaluatedProperties": False}
622+
message = self.message_for(instance="foo", schema=schema)
623+
self.assertEqual(message, f"'foo' is not of type 'object'")
624+
615625

616626
class TestValidationErrorDetails(TestCase):
617627
# TODO: These really need unit tests for each individual validator, rather

0 commit comments

Comments
 (0)