I have a schema for a input dict in Python that goes like this,
SCHEMA = {
'mode': ('mode_1', 'mode_2', 'mode_3'),
'method': ('method_1', 'method_2'),
'other': {
'x1': [1, 365],
'x2': [0., 1.0]
}
}
I want to validate the given input dict,
inp = {
'mode': 'mode_1',
'method': 'method_1',
'other': {
'x1': 1,
'x2': 1.0
}
}
- Contains the same keys
- If any of the value is a dict check that dict too have same keys
- Check the values of input dict are in the schema range. For example, for the key 'mode' the input dict should have value from
('mode_1', 'mode_2', 'mode_3'). Like wise for all the key-value pair. - If the type of a value in SCHEMA is a list, I also want to check the corresponding value from the input dict is greater than value[0] and less than value[1]
I can think about doing it with loops. But can't think of a generalized way to do it. If I just looped over the two dicts and compared the values, I have to rewrite the logic if the schema changes.
Is there any straight forward way to validate the schema?