summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2013-04-19 12:54:19 -0400
committerJulian Berman <Julian@GrayVines.com>2013-04-19 12:54:19 -0400
commit9e4191e9e158ac643bd555934776542e839dd23a (patch)
tree6b11bb4e3c7411d88d37be4f72b1f300d3dfb9db
parent9d4c68ec20e1cb9100d884b556162ba423b3c02a (diff)
downloadjsonschema-9e4191e9e158ac643bd555934776542e839dd23a.tar.gz
Raise exceptions on LookupErrors in ErrorTrees
Closes #92
-rw-r--r--jsonschema.py10
-rw-r--r--tests.py7
2 files changed, 17 insertions, 0 deletions
diff --git a/jsonschema.py b/jsonschema.py
index d1cb280..9b528d2 100644
--- a/jsonschema.py
+++ b/jsonschema.py
@@ -1228,6 +1228,8 @@ class ErrorTree(object):
"""
+ _instance = _unset
+
def __init__(self, errors=()):
self.errors = {}
self._contents = collections.defaultdict(self.__class__)
@@ -1238,6 +1240,8 @@ class ErrorTree(object):
container = container[element]
container.errors[error.validator] = error
+ self._instance = error.instance
+
def __contains__(self, k):
return k in self._contents
@@ -1245,8 +1249,14 @@ class ErrorTree(object):
"""
Retrieve the child tree with key ``k``.
+ If the key is not in the instance that this tree corresponds to,
+ whatever error would be raised by ``instance.__getitem__`` will be
+ propagated (usually this is some subclass of :class:`LookupError`.
+
"""
+ if self._instance is not _unset:
+ self._instance[k]
return self._contents[k]
def __setitem__(self, k, v):
diff --git a/tests.py b/tests.py
index 05ef02f..3160f90 100644
--- a/tests.py
+++ b/tests.py
@@ -608,6 +608,13 @@ class TestErrorTree(unittest.TestCase):
tree = ErrorTree([e1, e2])
self.assertEqual(tree["bar"][0].errors, {"foo" : e1, "quux" : e2})
+ def test_it_does_not_contain_subtrees_that_are_not_in_the_instance(self):
+ error = ValidationError("a message", validator="foo", instance=[])
+ tree = ErrorTree([error])
+
+ with self.assertRaises(IndexError):
+ tree[0]
+
class ValidatorTestMixin(object):
def setUp(self):