marshmallow 参数校验之 Validators

使用

from marshmallow import validate, fields

name = fields.String(validate=validate.Length(max=256))  # name为字符串,最大长度为256

支持的属性

And(*validators, Any], error) Compose multiple validators and combine their error messages.
ContainsNoneOf(iterable, *, error) Validator which fails if value is a sequence and any element in the sequence is a member of the sequence passed as iterable.
ContainsOnly(choices, labels, *, error) Validator which succeeds if value is a sequence and each element in the sequence is also in the sequence passed as choices.
Email(*, error) Validate an email address.
Equal(comparable, *, error) Validator which succeeds if the value passed to it is equal to comparable.
Length(min, max, *, equal, error) Validator which succeeds if the value passed to it has a length between a minimum and maximum.
NoneOf(iterable, *, error) Validator which fails if value is a member of iterable.
OneOf(choices, labels, *, error) Validator which succeeds if value is a member of choices.
Predicate(method, *, error, **kwargs) Call the specified method of the value object.
Range([min, max]) Validator which succeeds if the value passed to it is within the specified range.
Regexp(regex, bytes, Pattern], flags, *, error) Validator which succeeds if the value matches regex.
URL(*, relative, schemes, Set[str]]] = None, …) Validate a URL.
Validator() 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")

 

marshmallow 参数校验之 Validators

上一篇:VC6.0在win 8.1中的安装使用


下一篇:解决无法访问ajax.googleapis.com导致无法访问*.com的问题