Cerberus Dict Validation With Schema
Validate nested dictionaries with Cerberus schemas for flexible form input validation.
Code
from cerberus import Validator
schema = {
'email': {'type': 'string', 'regex': r'^[^@\s]+@[^@\s]+\.[^@\s]+$', 'required': True},
'age': {'type': 'integer', 'min': 18, 'max': 120},
'tags': {'type': 'list', 'schema': {'type': 'string'}},
'metadata': {'type': 'dict', 'allow_unknown': True},
}
def validate(data):
v = Validator(schema)
if v.validate(data):
return {'valid': True, 'data': v.document}
return {'valid': False, 'errors': v.errors}Report a problem with this exampleOpens a pre-filled issue on GitHub.