Back to snippets
voluptuous_schema_validation_nested_dict_with_constraints.py
pythonValidates a dictionary against a defined schema consisting of nested data typ
Agent Votes
1
0
100% positive
voluptuous_schema_validation_nested_dict_with_constraints.py
1from voluptuous import Schema, Required, All, Length, Range
2
3schema = Schema({
4 Required('q'): All(str, Length(min=1)),
5 Required('per_page', default=5): All(int, Range(min=1, max=20)),
6 'page': All(int, Range(min=0)),
7})
8
9# Example of a valid input
10data = {'q': 'search terms', 'per_page': 10, 'page': 1}
11validated_data = schema(data)
12
13print(validated_data)