官网:https://webargs.readthedocs.io/en/latest/
from webargs import fields
from marshmallow import validate
from verify import Verify
from Flask import request
class Webargs():
webargs = {
'a': fields.Str(validate=validate.Length(equal=32)),
'b': fields.DelimitedList(fields.Str(validate=Verify.prefix)),
'c': fields.Str(validate=validate.OneOf(['true', 'false'])),
'd': fields.Str(required=True),
'e': fields.Int(missing=30),
"f": fields.Nested(
{
"g": fields.Bool(missing=True)
}
}
def post():
kwargs = self.parser.parse(self.webargs , request)
# 此时kwargs拿到的时通过webargs校验的各项参数
verify.py
import re
class Verify():
def prefix(cls, prefix):
verify = re.match("正则表达式", prefix)
if not verify:
raise ValidationError("Parameter not satisfied")