shell
pip install cerberus
python
from cerberus import Validator
validator = Validator()
schema = {
'name': {'type': 'string', 'required': True},
'age': {'type': 'integer', 'required': True, 'min': 18},
'email': {'type': 'string', 'required': True, 'regex': '\w+@\w+\.[a-zA-Z]+', 'coerce': lambda v: v.lower()},
'address': {'type': 'string', 'required': True},
'phone': {'type': 'string', 'required': True, 'regex': '^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$'}
}
data = {
'age': 25,
'email': 'zhangsan@example.com',
'phone': '123-4567-8901'
}
result = validator.validate(data, schema)
if result:
else:
print(validator.errors)