python-schema
Writing Recursive Nested Objects
TL;DR
from schema import Schema
person = dict()
person.update({
'name': str,
Optional('children'): [Schema(person)],
})
person = Schema(person)
And that’s it. We got a working recursive nested object…
Example JSON data
{
"name": "chi",
"children": [
{
"name": "cken",
"children": [
{
"name": "birb"
}
]
}
]
}
Then, simply validate
person.validate(json_data)
Do not use .json_schema() function for this since it will crash due to maximum recursion error.
|