Skip to content

Commit 7830605

Browse files
committed
Emit a better error message for unevaluatedProperties with a subschema.
Closes: #996
1 parent 75903d8 commit 7830605

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

jsonschema/_validators.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,23 +435,32 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema):
435435
def unevaluatedProperties(validator, unevaluatedProperties, instance, schema):
436436
if not validator.is_type(instance, "object"):
437437
return
438-
evaluated_property_keys = find_evaluated_property_keys_by_schema(
438+
evaluated_keys = find_evaluated_property_keys_by_schema(
439439
validator, instance, schema,
440440
)
441-
unevaluated_property_keys = []
441+
unevaluated_keys = []
442442
for property in instance:
443-
if property not in evaluated_property_keys:
443+
if property not in evaluated_keys:
444444
for _ in validator.descend(
445445
instance[property],
446446
unevaluatedProperties,
447447
path=property,
448448
schema_path=property,
449449
):
450-
unevaluated_property_keys.append(property)
451-
452-
if unevaluated_property_keys:
453-
error = "Unevaluated properties are not allowed (%s %s unexpected)"
454-
yield ValidationError(error % extras_msg(unevaluated_property_keys))
450+
# FIXME: Include context for each unevaluated property
451+
# indicating why it's invalid under the subschema.
452+
unevaluated_keys.append(property)
453+
454+
if unevaluated_keys:
455+
if unevaluatedProperties is False:
456+
error = "Unevaluated properties are not allowed (%s %s unexpected)"
457+
yield ValidationError(error % extras_msg(unevaluated_keys))
458+
else:
459+
error = (
460+
"Unevaluated properties are not valid under "
461+
"the given schema (%s %s unevaluated and invalid)"
462+
)
463+
yield ValidationError(error % extras_msg(unevaluated_keys))
455464

456465

457466
def prefixItems(validator, prefixItems, instance, schema):

jsonschema/tests/test_validators.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,26 @@ def test_unevaluated_items_on_invalid_type(self):
634634
message = self.message_for(instance="foo", schema=schema)
635635
self.assertEqual(message, "'foo' is not of type 'array'")
636636

637-
def test_unevaluated_properties(self):
637+
def test_unevaluated_properties_invalid_against_subschema(self):
638+
schema = {
639+
"properties": {"foo": {"type": "string"}},
640+
"unevaluatedProperties": {"const": 12},
641+
}
642+
message = self.message_for(
643+
instance={
644+
"foo": "foo",
645+
"bar": "bar",
646+
"baz": 12,
647+
},
648+
schema=schema,
649+
)
650+
self.assertEqual(
651+
message,
652+
"Unevaluated properties are not valid under the given schema "
653+
"('bar' was unevaluated and invalid)",
654+
)
655+
656+
def test_unevaluated_properties_disallowed(self):
638657
schema = {"type": "object", "unevaluatedProperties": False}
639658
message = self.message_for(
640659
instance={

0 commit comments

Comments
 (0)