2022/04/14

[Python] flask restx 如何增加Email欄位驗證

https://flask-restx.readthedocs.io/en/latest/swagger.html#the-api-expect-decorator

 看了這段是這麼寫的


import re
EMAIL_REGEX = re.compile(r'\S+@\S+\.\S+')


user = api.model('User', {
    'id': fields.Integer(readonly=True, description='The user unique identifier'),
    'name': fields.String(required=True, description='The user name'),
    'email': fields.String(required=True, description='The user name', pattern='\S+@\S+.\S+')
})

在你要做這個額外檢查的API加入expect這個裝飾器

@ns_users.expect(user, validate=True)



沒有留言:

張貼留言