Skip to content
Advertisement

How to validate structure (or schema) of dictionary in Python?

I have a dictionary with config info:

JavaScript

I want to check if the dictionary follows the structure I need.

I’m looking for something like this:

JavaScript

Is there any solution done to this problem or any library that could make implementing check_structure more easy?

Advertisement

Answer

Without using libraries, you could also define a simple recursive function like this:

JavaScript

This assumes that the config can have keys that are not in your structure, as in your example.


Update: New version also supports lists, e.g. like 'foo': [{'bar': int}]

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement