Skip to content

Commit

Permalink
Allow msg to be specified for Maybe
Browse files Browse the repository at this point in the history
This makes it possible to supply a good error message to the user.
This got reported in #369
  • Loading branch information
svisser committed Dec 14, 2018
1 parent e72fd3b commit 43ea94b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ def test_maybe():
assert_raises(Invalid, s, {'foo': 'bar'})


def test_maybe_accepts_msg():
s = Schema(Maybe(int, msg='int or None expected'))
with raises(MultipleInvalid, 'int or None expected'):
assert s([])


def test_empty_list_as_exact():
s = Schema([])
assert_raises(Invalid, s, [1])
Expand Down
4 changes: 2 additions & 2 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def PathExists(v):
raise PathInvalid("Not a Path")


def Maybe(validator):
def Maybe(validator, msg=None):
"""Validate that the object matches given validator or is None.
:raises Invalid: if the value does not match the given validator and is not
Expand All @@ -500,7 +500,7 @@ def Maybe(validator):
... s("string")
"""
return Any(None, validator)
return Any(None, validator, msg=msg)


class Range(object):
Expand Down

0 comments on commit 43ea94b

Please sign in to comment.