Skip to content

Commit

Permalink
Only validate unevaluated props/items on applicable types
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicWink committed May 18, 2022
1 parent fb85c66 commit 58b015e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jsonschema/_validators.py
Expand Up @@ -418,6 +418,8 @@ def if_(validator, if_schema, instance, schema):


def unevaluatedItems(validator, unevaluatedItems, instance, schema):
if not validator.is_type(instance, "array"):
return
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(
validator, instance, schema,
)
Expand All @@ -431,6 +433,8 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema):


def unevaluatedProperties(validator, unevaluatedProperties, instance, schema):
if not validator.is_type(instance, "object"):
return
evaluated_property_keys = find_evaluated_property_keys_by_schema(
validator, instance, schema,
)
Expand Down
10 changes: 10 additions & 0 deletions jsonschema/tests/test_validators.py
Expand Up @@ -597,6 +597,11 @@ def test_unevaluated_items(self):
"Unevaluated items are not allowed ('bar', 'foo' were unexpected)",
)

def test_unevaluated_items_on_invalid_type(self):
schema = {"type": "array", "unevaluatedItems": False}
message = self.message_for(instance="foo", schema=schema)
self.assertEqual(message, f"'foo' is not of type 'array'")

def test_unevaluated_properties(self):
schema = {"type": "object", "unevaluatedProperties": False}
message = self.message_for(
Expand All @@ -612,6 +617,11 @@ def test_unevaluated_properties(self):
"('bar', 'foo' were unexpected)",
)

def test_unevaluated_properties_on_invalid_type(self):
schema = {"type": "object", "unevaluatedProperties": False}
message = self.message_for(instance="foo", schema=schema)
self.assertEqual(message, f"'foo' is not of type 'object'")


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

0 comments on commit 58b015e

Please sign in to comment.