diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 9a07f5ec3..874e8796f 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -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, ) @@ -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, ) diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index 60f52a97a..2d807b77b 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -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", @@ -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", diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 23655a163..3aabd3370 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -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( @@ -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