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

How to add multiple validation parameters in a marshmallow schema

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

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

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

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

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/"
    ])

来自documentation

validate (callable) – Validator or collection of validators that are called during deserialization.