Cerberus Dict Validation With Schema

Validate nested dictionaries with Cerberus schemas for flexible form input validation.

PythonForm Validation

Code

snippet
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}