-
Notifications
You must be signed in to change notification settings - Fork 218
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
Implement nested schema support and validators compilation #318
Conversation
This allows to refer to the current schema using voluptuous.Self and have nested definitions. Fixes alecthomas#128
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this. Just one minor comment.
voluptuous/validators.py
Outdated
@@ -181,7 +181,33 @@ def Boolean(v): | |||
return bool(v) | |||
|
|||
|
|||
class Any(object): | |||
class _WithSubValidators(object): | |||
def __init__(self, *validators, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a docstring here describing how this is intended to be used.
This allows any validator to be compiled by implementing the __voluptuous_compile__ method. This avoids having voluptuous.Any and voluptuous.All defining new Schema for sub-validators: they can be compiled recursively using the same parent schema. This solves the recursive Self case. Fixes alecthomas#18
Add docstring for _WithSubValidators |
Both of those changes are great, thanks! |
Upstream changes are many minor improvements and bugfixes, plus **Changes** - [#378](alecthomas/voluptuous#378): Allow `extend()` of a `Schema` to return a subclass of a `Schema` as well. **Changes**: - [#349](alecthomas/voluptuous#349): Support Python 3.7. - [#343](alecthomas/voluptuous#343): Drop support for Python 3.3. **Changes**: - [#293](alecthomas/voluptuous#293): Support Python 3.6. - [#294](alecthomas/voluptuous#294): Drop support for Python 2.6, 3.1 and 3.2. - [#318](alecthomas/voluptuous#318): Allow to use nested schema and allow any validator to be compiled. - [#324](alecthomas/voluptuous#324): Default values MUST now pass validation just as any regular value. This is a backward incompatible change if a schema uses default values that don't pass validation against the specified schema. - [#328](alecthomas/voluptuous#328): Modify `__lt__` in Marker class to allow comparison with non Marker objects, such as str and int.
This PR is made of 2 commits:
Allow to use nested schema that adds
voluptuous.Self
as a reference to theSchema
being compiled.Allow any validator to be compiled which allows to compile validators and implements that compilation on validators with subvalidators (and/or/someof) in order to avoid the sub
Schema
creation. This solves the recursivevoluptuous.Self
case elegantly.Hope you'll enjoy it!