Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple validation parameters in a marshmallow schema

I have the following schema in one of my class models:

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

Aside from validating the length, I also want to be able to make sure that facebook_profile_url is never equal to the string "http://www.facebook.com/".

like image 875
Bargain23 Avatar asked Jan 04 '18 02:01

Bargain23


1 Answers

You can pass a list as the validate parameter:

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

From the documentation:

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

like image 88
ash Avatar answered Nov 19 '22 15:11

ash