Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only validate unevaluated properties/items on applicable types #949

Merged
merged 3 commits into from May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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