question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to define schema for recursive structure?

See original GitHub issue

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 are person too. How can I do that?

Basically, I would like to do:

person = {'firstname': str, 'lastname': str, Optional('children'): [person]}

Obviously, this does not work. Is there a solution?

Currently, I do something like:

person = {'firstname': str, 'lastname': str}
person[Optional('children')] = [person]

… but I find it not really readable…

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

3reactions
AlexandreDecancommented, Feb 5, 2021

Create and store the inner dict separately, following this idea:

person = dict()
person.update({'name': str, 'gender': str, 'children': [person])
1reaction
RagingRooseveltcommented, Feb 5, 2021

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({...})]})]
Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Schema - Recursive Schema Definition - Stack Overflow
Yes, your schema will work. The "$ref": "#" points back to the root of the schema document. However, the "type": "object" is useless:...
Read more >
Recursive entity representation in JSON schema.
question is focused around statements that a JSON schema can NOT facilitate a recursive structure. This is a problem for my data
Read more >
Recursive schema composition · Issue #558 · json ... - GitHub
Re-using recursive schemas is a challenge. $recurse is a specialized version of $ref with a context-dependent target; The target is the root ...
Read more >
How to design a recursive menu schema?
The only option that worked was to create dedicated embedded schemas for each level. Obviously, this required relaxing the initial condition of ...
Read more >
Recursive Relationships in ER diagrams - GeeksforGeeks
Here the same entity type participates more than once in a relationship type with a different role for each instance. In other words,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found