Skip to content

Commit 46a5828

Browse files
committed
python-jsonschema#708 - limit JSON formatting
Only use JSON formatting for exception messages when explicitly requested.
1 parent d3c2c03 commit 46a5828

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

jsonschema/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"""
44

55
from __future__ import absolute_import
6-
from textwrap import dedent
6+
77
import argparse
8-
import errno
98
import json
10-
import sys
119
import traceback
1210

1311
import attr
12+
import errno
13+
import sys
1414

1515
from jsonschema import __version__
1616
from jsonschema._reflect import namedAny
@@ -105,7 +105,7 @@ def validation_error(self, instance_path, error):
105105
return self._error_msg(
106106
path=instance_path,
107107
type=error.__class__.__name__,
108-
body=error,
108+
body=error.formatted_message(pretty=True),
109109
)
110110

111111
def validation_success(self, instance_path):

jsonschema/exceptions.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ def __init__(
6262
def __repr__(self):
6363
return "<%s: %r>" % (self.__class__.__name__, self.message)
6464

65-
def __unicode__(self):
65+
def formatted_message(self, pretty=False):
6666
essential_for_verbose = (
6767
self.validator, self.validator_value, self.instance, self.schema,
6868
)
6969
if any(m is _unset for m in essential_for_verbose):
7070
return self.message
7171

72-
pschema = json.dumps(self.schema, separators=(',\n', ': '), sort_keys=True)
72+
if (pretty is True):
73+
pschema = json.dumps(self.schema, separators=(',\n', ': '), sort_keys=True)
74+
else:
75+
pschema = pprint.pformat(self.schema, width=72)
76+
7377
pinstance = pprint.pformat(self.instance, width=72)
7478
return self.message + textwrap.dedent("""
7579
@@ -89,6 +93,9 @@ def __unicode__(self):
8993
_utils.indent(pinstance),
9094
)
9195

96+
def __unicode__(self):
97+
return self.formatted_message(pretty=False)
98+
9299
if PY3:
93100
__str__ = __unicode__
94101
else:
@@ -201,7 +208,7 @@ def __init__(self, type, instance, schema):
201208
self.schema = schema
202209

203210
def __unicode__(self):
204-
pschema = json.dumps(self.schema, separators=(',\n', ': '), sort_keys=True)
211+
pschema = pprint.pformat(self.schema, width=72)
205212
pinstance = pprint.pformat(self.instance, width=72)
206213
return textwrap.dedent("""
207214
Unknown type %r for validator with schema:

0 commit comments

Comments
 (0)