diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index fa44fbf..0677882 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -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]) diff --git a/voluptuous/validators.py b/voluptuous/validators.py index d5e3ed5..8c6a86d 100644 --- a/voluptuous/validators.py +++ b/voluptuous/validators.py @@ -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 @@ -500,7 +500,7 @@ def Maybe(validator): ... s("string") """ - return Any(None, validator) + return Any(None, validator, msg=msg) class Range(object):