使用
from marshmallow import validate, fields
name = fields.String(validate=validate.Length(max=256)) # name为字符串,最大长度为256
支持的属性
| Compose multiple validators and combine their error messages. |
| Validator which fails if |
| Validator which succeeds if |
| Validate an email address. |
| Validator which succeeds if the |
| Validator which succeeds if the value passed to it has a length between a minimum and maximum. |
| Validator which fails if |
| Validator which succeeds if |
| Call the specified |
| Validator which succeeds if the value passed to it is within the specified range. |
| Validator which succeeds if the |
| Validate a URL. |
| Abstract base class for validators. |
自定义:
from marshmallow import ValidationError
def my_validate(test_str):
if len(test_str) <= 5:
raise ValidationError("字符串长度必须大于5")
if len(test_str) >= 20:
raise ValidationError("字符串长度必须小于20")