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

Ability to have recursive schema #128

Closed
jd opened this issue Aug 30, 2015 · 6 comments
Closed

Ability to have recursive schema #128

jd opened this issue Aug 30, 2015 · 6 comments

Comments

@jd
Copy link
Contributor

jd commented Aug 30, 2015

Today there's no easy syntax to have a recursive schema. The best way to do it is to have a wrapper like that:

>>> import voluptuous
>>> def s2(v):
...     return s1(v)
...
>>> s1 = voluptuous.Schema({"abc": voluptuous.Any(s2, "def")})
>>> s1({"abc": {"abc": "def"}})
{'abc': {'abc': 'def'}}
@alecthomas
Copy link
Owner

It's not clear to me how that could be expressed any other way than something similar to what you have?

@jd
Copy link
Contributor Author

jd commented Oct 12, 2015

It's not clear to me either, I didn't find any other elegant solution. But it'd be nice if someone did. :)

@harlowja
Copy link
Contributor

So I tried the following (might be something I'm missing since it doesn't work, ha).

harlowja@afeac72

But that seems like it could be a way to do this...

@alecthomas
Copy link
Owner

I think the solution @jd came up with is fine.

@miso-belica
Copy link
Contributor

@alecthomas Solution is good enough but It would be fine to have it in documentation somewhere for others. I can prepare PR if you are busy :)

@alecthomas
Copy link
Owner

That would be great, thanks!

alecthomas added a commit that referenced this issue Mar 7, 2016
jd added a commit to jd/voluptuous that referenced this issue Dec 22, 2017
This allows to refer to the current schema using voluptuous.Self and have
nested definitions.

Fixes alecthomas#128
alecthomas pushed a commit that referenced this issue Dec 26, 2017
* Allow to use nested schema

This allows to refer to the current schema using voluptuous.Self and have
nested definitions.

Fixes #128

* Allow any validator to be compiled

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 #18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants