Skip to content

Add instance and validator_value attributes to ValidationError #80

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

Closed
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
14 changes: 12 additions & 2 deletions jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@


class _Error(Exception):
def __init__(self, message, validator=None, path=(), cause=None):
def __init__(
self, message, validator=None, path=(), cause=None, instance=None,
validator_value=None
):
super(_Error, self).__init__(message, validator, path)
self.message = message
self.path = collections.deque(path)
self.instance = instance
self.validator = validator
self.validator_value = validator_value
self.cause = cause

def __str__(self):
Expand Down Expand Up @@ -217,9 +222,14 @@ def iter_errors(self, instance, _schema=None):

errors = validator(v, instance, _schema) or ()
for error in errors:
# set validator if it wasn't already set by the called fn
# set validator attributes if they weren't already set by
# the called function
if error.validator is None:
error.validator = k
if error.validator_value is None:
error.validator_value = v
if error.instance is None:
error.instance = instance
yield error

def validate(self, *args, **kwargs):
Expand Down
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ def test_single_nesting(self):
self.assertEqual(e3.validator, "maximum")
self.assertEqual(e4.validator, "type")

self.assertEqual(e1.validator_value, 2)
self.assertEqual(e2.validator_value, [2, 4, 6, 8])
self.assertEqual(e3.validator_value, 10)
self.assertEqual(e4.validator_value, "string")

self.assertEqual(e1.instance, [1])
self.assertEqual(e2.instance, 15)
self.assertEqual(e3.instance, 15)
self.assertEqual(e4.instance, 2)

def test_multiple_nesting(self):
instance = [1, {"foo" : 2, "bar" : {"baz" : [1]}}, "quux"]
schema = {
Expand Down