Skip to content

Commit

Permalink
Merge pull request #949 from EpicWink/fix-unevaluated
Browse files Browse the repository at this point in the history
Only validate unevaluated properties/items on applicable types
  • Loading branch information
Julian committed May 20, 2022
2 parents 63d0af6 + 731be4f commit d715573
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 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
15 changes: 0 additions & 15 deletions jsonschema/tests/test_jsonschema_test_suite.py
Expand Up @@ -346,11 +346,6 @@ def leap_second(test):
message="unevaluatedItems is different in 2019-09 (needs work).",
subject="unevaluatedItems",
)(test)
or skip(
message=bug(949),
subject="unevaluatedProperties",
case_description="non-object instances are valid",
)(test)
or skip(
message="dynamicRef support isn't working yet.",
subject="recursiveRef",
Expand Down Expand Up @@ -416,16 +411,6 @@ def leap_second(test):
message="Vocabulary support is not yet present.",
subject="vocabulary",
)(test)
or skip(
message=bug(949),
subject="unevaluatedItems",
case_description="non-array instances are valid",
)(test)
or skip(
message=bug(949),
subject="unevaluatedProperties",
case_description="non-object instances are valid",
)(test)
or skip(
message=bug(),
subject="ref",
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, "'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, "'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 d715573

Please sign in to comment.