Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e169504

Browse files
committed
Make the error message for additionals prettier
Also fixes an unexpected bug in additionalItems.
1 parent c205e54 commit e169504

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

jsonschema.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ def validate_additionalProperties(self, aP, instance, schema):
308308
for extra in extras:
309309
self._validate(instance[extra], aP)
310310
elif not aP and extras:
311-
self._error(u"Additional properties are not allowed")
311+
error = u"Additional properties are not allowed (%s %s unexpected)"
312+
self._error(error % _extras_msg(extras))
312313

313314
def validate_items(self, items, instance, schema):
314315
if self._is_type(items, "object"):
@@ -325,8 +326,9 @@ def validate_additionalItems(self, aI, instance, schema):
325326
if self._is_type(aI, "object"):
326327
for item in instance[len(schema):]:
327328
self._validate(item, aI)
328-
elif not aI and len(instance) > len(schema):
329-
self._error(u"Additional items are not allowed")
329+
elif not aI and len(instance) > len(schema.get("items", [])):
330+
error = u"Additional items are not allowed (%s %s unexpected)"
331+
self._error(error % _extras_msg(instance[len(schema) - 1:]))
330332

331333
def validate_minimum(self, minimum, instance, schema):
332334
if schema.get(u"exclusiveMinimum", False):
@@ -410,6 +412,19 @@ def validate_disallow(self, disallow, instance, schema):
410412
)
411413

412414

415+
def _extras_msg(extras):
416+
"""
417+
Create an error message for extra items or properties.
418+
419+
"""
420+
421+
if len(extras) == 1:
422+
verb = u"was"
423+
else:
424+
verb = u"were"
425+
return u", ".join(repr(extra) for extra in extras), verb
426+
427+
413428
def _list(thing):
414429
"""
415430
Wrap ``thing`` in a list if it's a single str.

tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,21 @@ def additionalProperties(self, aP):
258258
def test_additionalProperties_ignores_nonobjects(self):
259259
validate(None, {"additionalProperties" : False})
260260

261+
@parametrized(
262+
("single_extra", {"foo" : 2}, ["('foo' was unexpected)"]),
263+
("multiple_extras",
264+
dict.fromkeys(["foo", "bar", "quux"]),
265+
["'bar'", "'foo'", "'quux'", "were unexpected)"],
266+
),
267+
)
268+
def additionalProperties_errorMessage(self, instance, errs):
269+
schema = {"additionalProperties" : False}
270+
271+
with self.assertRaises(ValidationError) as error:
272+
validate(instance, schema)
273+
274+
self.assertTrue(all(err in error.exception.message for err in errs))
275+
261276
items = parametrized(
262277
("", "valid", [1, 2, 3]),
263278
("wrong_type", "invalid", [1, u"x"]),
@@ -293,6 +308,16 @@ def test_additionalItems_allowed_by_default(self):
293308
def test_additionalItems_ignores_nonarrays(self):
294309
validate(None, {"additionalItems" : False})
295310

311+
@parametrized(
312+
("single_extra", [2], "(2 was unexpected)"),
313+
("multiple_extras", [1, 2, 3], "(1, 2, 3 were unexpected)"),
314+
)
315+
def additionalItems_errorMessage(self, instance, err):
316+
schema = {"additionalItems" : False}
317+
self.assertRaisesRegexp(
318+
ValidationError, err, validate, instance, schema
319+
)
320+
296321
@parametrized(
297322
("false_by_default", "valid", {}, {}),
298323
("false_explicit", "valid", {u"required" : False}, {}),

0 commit comments

Comments
 (0)