Skip to content
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

Fix #379: Regression with Any and required #380

Merged
merged 1 commit into from
Jan 13, 2019
Merged
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
27 changes: 27 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,3 +1322,30 @@ def test_strip_util_handles_various_inputs():
assert Strip(u"3") == u"3"
assert Strip(b'\xe2\x98\x83'.decode("UTF-8")) == b'\xe2\x98\x83'.decode("UTF-8")
assert Strip(u" aaa ") == u"aaa"


def test_any_required():
schema = Schema(Any({'a': int}, {'b': str}, required=True))

try:
schema({})
except MultipleInvalid as e:
assert_equal(str(e),
"required key not provided @ data['a']")
else:
assert False, "Did not raise Invalid for MultipleInvalid"


def test_any_required_with_subschema():
schema = Schema(Any({'a': Any(float, int)},
{'b': int},
{'c': {'aa': int}},
required=True))

try:
schema({})
except MultipleInvalid as e:
assert_equal(str(e),
"required key not provided @ data['a']")
else:
assert False, "Did not raise Invalid for MultipleInvalid"
9 changes: 5 additions & 4 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,13 @@ class _WithSubValidators(object):
def __init__(self, *validators, **kwargs):
self.validators = validators
self.msg = kwargs.pop('msg', None)
self.required = kwargs.pop('required', False)

def __voluptuous_compile__(self, schema):
self._compiled = [
schema._compile(v)
for v in self.validators
]
self._compiled = []
for v in self.validators:
schema.required = self.required
CedricCabessa marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a leftover? Should this be v.required = self.required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is correct. Test fail otherwise

Also note that the line must be inside the loop. I'm not totally clear on what happen... but tests are passing ...

self._compiled.append(schema._compile(v))
return self._run

def _run(self, path, value):
Expand Down