Skip to content

work on more detailed messages for errors #90

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

Merged
merged 8 commits into from
Apr 19, 2013
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ The error messages in this situation are not very helpful on their own:

>>> for error in errors:
... print(error.message)
The instance is not valid under any of the given schemas
The instance is not valid under any of the given schemas
The instance is not valid under any of the given schemas
{} is not valid under any of the given schemas
3 is not valid under any of the given schemas
'foo' is not valid under any of the given schemas

If we look at :attr:`ValidationError.path` on each of the errors, we can find
out which elements in the instance correspond to each of the errors. In
Expand Down Expand Up @@ -129,14 +129,27 @@ the schema each of these errors come from. In the case of sub-errors from the

>>> for error in errors:
... for suberror in sorted(error.context, key=lambda e: e.schema_path):
... print(list(suberror.schema_path), suberror, sep=",")
... print(list(suberror.schema_path), suberror.message, sep=", ")
[0, 'type'], {} is not of type 'string'
[1, 'type'], {} is not of type 'integer'
[0, 'type'], 3 is not of type 'string'
[1, 'minimum'], 3.0 is less than the minimum of 5
[0, 'maxLength'], 'foo' is too long
[1, 'type'], 'foo' is not of type 'integer'

The string representation of an error combines some of these attributes for
easier debugging.

.. code-block:: python

>>> print(errors[1])
ValidationError: 3 is not valid under any of the given schemas
Failed validating 'anyOf' in schema['items']:
{'anyOf': [{'maxLength': 2, 'type': 'string'},
{'minimum': 5, 'type': 'integer'}]}
On instance[1]:
3
<BLANKLINE>

ErrorTrees
----------
Expand Down
2 changes: 1 addition & 1 deletion docs/validate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ adhere to.
... }
>>> v = Draft3Validator(schema)
>>> for error in sorted(v.iter_errors([2, 3, 4]), key=str):
... print(error)
... print(error.message)
4 is not one of [1, 2, 3]
[2, 3, 4] is too long

Expand Down
31 changes: 30 additions & 1 deletion jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import json
import numbers
import operator
import pprint
import re
import socket
import sys
Expand Down Expand Up @@ -108,7 +109,35 @@ def __str__(self):
return unicode(self).encode("utf-8")

def __unicode__(self):
return self.message
if any(attr is _unset for attr in (
self.validator, self.validator_value, self.instance, self.schema
)):
return self.message
schema_path = ""
if len(self.schema_path) > 1:
schema_path = (
"[%s]" % "][".join(
map(repr, list(self.schema_path)[:-1])
)
)
path = ""
if self.path:
path = "[%s]" % "][".join(map(repr, self.path))
pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72)
return textwrap.dedent("""\
%s: %s
Failed validating '%s' in schema%s:
%s
On instance%s:
%s
""") % (
self.__class__.__name__, self.message,
self.validator, schema_path,
'\n'.join(" " * 8 + line for line in pschema.splitlines()),
path,
'\n'.join(" " * 8 + line for line in pinstance.splitlines()),
)

if PY3:
__str__ = __unicode__
Expand Down
150 changes: 150 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import subprocess
import sys
import textwrap

if sys.version_info[:2] < (2, 7): # pragma: no cover
import unittest2 as unittest
Expand Down Expand Up @@ -361,6 +362,155 @@ def test_repr(self):
self.assertEqual(repr(error), "<ValidationError: %r>" % message)


class TestErrorStr(unittest.TestCase):
def make_error(self, **kwargs):
# Creates an error with all the required attributes set
default = {
"message": "message",
"validator": "type",
"validator_value": "string",
"instance": 5,
"schema": {"type": "string"}
}
default.update(kwargs)
return ValidationError(**default)

def prep_message(self, message):
# Strips leading whitespace, dedents, replaces u' with ' on py3
if PY3:
message = message.replace("u'", "'")
return textwrap.dedent(message).lstrip()

def test_unset_error(self):
error = ValidationError("message")
self.assertEqual(str(error), "message")
kwargs = {
"validator": "type",
"validator_value": "string",
"instance": 5,
"schema": {"type": "string"}
}
# Just the message should show if any of the attributes are unset
for attr in kwargs:
k = dict(kwargs)
del k[attr]
error = ValidationError("message", **k)
self.assertEqual(str(error), "message")

def test_basic_message(self):
# Make sure all the defaults from make_error print correctly
error = self.make_error()
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema:
{u'type': u'string'}
On instance:
5
""")
self.assertEqual(str(error), message)

def test_empty_paths(self):
error = self.make_error(path=[], schema_path=[])
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema:
{u'type': u'string'}
On instance:
5
""")
self.assertEqual(str(error), message)

def test_one_item_paths(self):
error = self.make_error(path=[0], schema_path=["items"])
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema:
{u'type': u'string'}
On instance[0]:
5
""")
self.assertEqual(str(error), message)

def test_two_item_paths(self):
error = self.make_error(
path=[0, 1], schema_path=["items", "additionalItems"]
)
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema[u'items']:
{u'type': u'string'}
On instance[0][1]:
5
""")
self.assertEqual(str(error), message)

def test_mixed_type_paths(self):
error = self.make_error(
path=["a", 1, "b"], schema_path=["anyOf", 5, "type"]
)
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema[u'anyOf'][5]:
{u'type': u'string'}
On instance[u'a'][1][u'b']:
5
""")
self.assertEqual(str(error), message)

def test_pprint_schema(self):
schema = {
"type": ["string"],
"items": {
"allOf": [
{"type": "string", "maxLength": 2},
{"type": "integer", "minimum": 5},
{"items": [{"type": "string"}, {"type": "integer"}],
"type": "array"}
]
}
}
error = self.make_error(schema=schema)
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema:
{u'items': {u'allOf': [{u'maxLength': 2, u'type': u'string'},
{u'minimum': 5, u'type': u'integer'},
{u'items': [{u'type': u'string'},
{u'type': u'integer'}],
u'type': u'array'}]},
u'type': [u'string']}
On instance:
5
""")
self.assertEqual(str(error), message)

def test_pprint_instance(self):
instance = {
"type": ["string"],
"items": {
"allOf": [
{"type": "string", "maxLength": 2},
{"type": "integer", "minimum": 5},
{"items": [{"type": "string"}, {"type": "integer"}],
"type": "array"}
]
}
}
error = self.make_error(instance=instance)
message = self.prep_message("""
ValidationError: message
Failed validating 'type' in schema:
{u'type': u'string'}
On instance:
{u'items': {u'allOf': [{u'maxLength': 2, u'type': u'string'},
{u'minimum': 5, u'type': u'integer'},
{u'items': [{u'type': u'string'},
{u'type': u'integer'}],
u'type': u'array'}]},
u'type': [u'string']}
""")
self.assertEqual(str(error), message)


class TestValidationErrorDetails(unittest.TestCase):
def setUp(self):
Expand Down