如何在棉花糖模式中添加多个验证参数

Bar*_*n23 3 validation unit-testing python-3.x marshmallow

我的一个班级模型中有以下架构:

class SocialMediaSchema(Schema):
    facebook_profile_url = fields.String(required=False, validate=validate.Length(0, 71, 'Facebook username is too long.')
Run Code Online (Sandbox Code Playgroud)

除了验证长度之外,我还希望能够确保该facebook_profile_url长度永远不等于string "http://www.facebook.com/"

Jos*_*osh 5

您可以将列表作为validates参数传递:

class SocialMediaSchema(Schema):
    facebook_profile_url = fields.String(required=False, validate=[
        validate.Length(0, 71, 'Facebook username is too long.'),
        lambda x: x != "http://www.facebook.com/"
    ])
Run Code Online (Sandbox Code Playgroud)

文档中

validate (可调用)–反序列化期间调用的验证器或验证器集合。