1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
import collections
import itertools
import pprint
import textwrap
from jsonschema import _utils
from jsonschema.compat import PY3, iteritems
_unset = _utils.Unset()
class _Error(Exception):
def __init__(
self, message, validator=_unset, path=(), cause=None, context=(),
validator_value=_unset, instance=_unset, schema=_unset, schema_path=(),
):
self.message = message
self.path = collections.deque(path)
self.schema_path = collections.deque(schema_path)
self.context = list(context)
self.cause = self.__cause__ = cause
self.validator = validator
self.validator_value = validator_value
self.instance = instance
self.schema = schema
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self._contents() == other._contents()
def __lt__(self, other):
if not isinstance(other, self.__class__):
# On Py2 Python will "helpfully" make this succeed. So be more
# forceful, because we really don't want this to work, it probably
# means a ValidationError and a SchemaError are being compared
# accidentally.
if not PY3:
message = "unorderable types: %s() < %s()" % (
self.__class__.__name__, other.__class__.__name__,
)
raise TypeError(message)
return NotImplemented
is_deeper = len(self.path) > len(other.path)
is_weak_matcher = self.validator in ("anyOf", "oneOf")
other_is_weak_matcher = other.validator in ("anyOf", "oneOf")
return is_deeper or is_weak_matcher > other_is_weak_matcher
def __ne__(self, other):
return not self == other
def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.message)
def __str__(self):
return unicode(self).encode("utf-8")
def __unicode__(self):
if _unset in (
self.validator, self.validator_value, self.instance, self.schema,
):
return self.message
path = _utils.format_as_index(self.path)
schema_path = _utils.format_as_index(list(self.schema_path)[:-1])
pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72)
return self.message + textwrap.dedent("""
Failed validating %r in schema%s:
%s
On instance%s:
%s
""".rstrip()
) % (
self.validator,
schema_path,
_utils.indent(pschema),
path,
_utils.indent(pinstance),
)
if PY3:
__str__ = __unicode__
@classmethod
def create_from(cls, other):
return cls(**other._contents())
def _set(self, **kwargs):
for k, v in iteritems(kwargs):
if getattr(self, k) is _unset:
setattr(self, k, v)
def _contents(self):
return dict(
(attr, getattr(self, attr)) for attr in (
"message", "cause", "context", "path", "schema_path",
"validator", "validator_value", "instance", "schema"
)
)
class ValidationError(_Error):
pass
class SchemaError(_Error):
pass
class RefResolutionError(Exception):
pass
class UnknownType(Exception):
def __init__(self, type, instance, schema):
self.type = type
self.instance = instance
self.schema = schema
def __str__(self):
return unicode(self).encode("utf-8")
def __unicode__(self):
pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72)
return textwrap.dedent("""
Unknown type %r for validator with schema:
%s
While checking instance:
%s
""".rstrip()
) % (self.type, _utils.indent(pschema), _utils.indent(pinstance))
if PY3:
__str__ = __unicode__
class FormatError(Exception):
def __init__(self, message, cause=None):
super(FormatError, self).__init__(message, cause)
self.message = message
self.cause = self.__cause__ = cause
def __str__(self):
return self.message.encode("utf-8")
def __unicode__(self):
return self.message
if PY3:
__str__ = __unicode__
def best_match(errors):
first = next(iter(errors), None)
if first is None:
return
best = max(itertools.chain([first], errors))
while best.context:
best = min(best.context)
return best
|