Skip to content

Commit

Permalink
Include path in AnyInvalid errors
Browse files Browse the repository at this point in the history
Fixes #347
  • Loading branch information
jd committed Jul 5, 2018
1 parent 4b5cb5b commit 3c48ef9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
47 changes: 47 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,53 @@ def test_self_validation():
schema({"follow": {"follow": {"number": 123456}}})


def test_any_error_has_path():
"""https://github.com/alecthomas/voluptuous/issues/347"""
s = Schema({
Optional('q'): int,
Required('q2'): Any(int, msg='toto')
})
try:
s({'q': 'str', 'q2': 'tata'})
except MultipleInvalid as exc:
assert (
(exc.errors[0].path == ['q'] and exc.errors[1].path == ['q2']) or
(exc.errors[1].path == ['q'] and exc.errors[0].path == ['q2'])
)
else:
assert False, "Did not raise AnyInvalid"


def test_all_error_has_path():
"""https://github.com/alecthomas/voluptuous/issues/347"""
s = Schema({
Optional('q'): int,
Required('q2'): All([str, Length(min=10)], msg='toto'),
})
try:
s({'q': 'str', 'q2': 12})
except MultipleInvalid as exc:
assert (
(exc.errors[0].path == ['q'] and exc.errors[1].path == ['q2']) or
(exc.errors[1].path == ['q'] and exc.errors[0].path == ['q2'])
)
else:
assert False, "Did not raise AllInvalid"


def test_match_error_has_path():
"""https://github.com/alecthomas/voluptuous/issues/347"""
s = Schema({
Required('q2'): Match("a"),
})
try:
s({'q2': 12})
except MultipleInvalid as exc:
assert exc.errors[0].path == ['q2']
else:
assert False, "Did not raise MatchInvalid"


def test_self_any():
schema = Schema({"number": int,
"follow": Any(Self, "stop")})
Expand Down
8 changes: 5 additions & 3 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ def _exec(self, funcs, v, path=None):
error = e
else:
if error:
raise error if self.msg is None else AnyInvalid(self.msg)
raise AnyInvalid(self.msg or 'no valid value found')
raise error if self.msg is None else AnyInvalid(
self.msg, path=path)
raise AnyInvalid(self.msg or 'no valid value found',
path=path)


# Convenience alias
Expand Down Expand Up @@ -281,7 +283,7 @@ def _exec(self, funcs, v, path=None):
else:
v = func(path, v)
except Invalid as e:
raise e if self.msg is None else AllInvalid(self.msg)
raise e if self.msg is None else AllInvalid(self.msg, path=path)
return v


Expand Down

0 comments on commit 3c48ef9

Please sign in to comment.