-
Notifications
You must be signed in to change notification settings - Fork 215
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
Question: how to define schema for recursive structure? #119
Comments
I was just going to post that but you updated the post. Does the recursion you tried work well? I think that is the most reasonable way to do it, currently. I agree that it's not very readable, but that's a Python issue, not a schema issue... |
Yes, it works. But in case of a validation error, the error message is not very useful/accurate, but that's another issue ;-) Btw, thanks for your answer! |
@keleshev Would be nice to have this in documentation. I used library voluptuous just because I thought this is not possible with Schema. |
Indeed. I also looked at voluptuous, but I didn't like much its "non pythonic" approach to declare schema. |
Wait, so does this actually work? I tried doing (for example) [...]
Person = Schema(
{
'name': str
,'gender': str
}
, name='Person', as_reference=True
)
person['children'] = [person] and I got an error message of |
Create and store the inner dict separately, following this idea:
|
Ah, ok, that makes sense. For anyone finding this in the future: >>>from schema import Schema
>>>person = dict()
>>>Person = Schema(person)
>>>person.update({'name': str, 'children': [Person]})
>>>print(Person)
Schema({'name': <class 'str'>, 'children': [Schema({...})]})
>>>print(Person.schema.get('children'))
[Schema({'name': <class 'str'>, 'children': [Schema({...})]})] |
Hello,
I've a question about the definition of a schema for a recursive structure. Say for example I want to encode a
person
which has a firstname, a lastname, and a list of children which areperson
too.How can I do that?
Basically, I would like to do:
Obviously, this does not work. Is there a solution?
Currently, I do something like:
... but I find it not really readable...
The text was updated successfully, but these errors were encountered: