【发布时间】:2021-09-19 05:49:12
【问题描述】:
有没有办法在 Flask 中验证刺痛以检查以下内容。我希望它不仅出现在 Swagger 文档中,而且尽可能将其作为字符串验证错误捕获
- 最小长度10
- 最大长度 1,024
- 以
[a-zA-Z]开头 - 包含
[a-zA-Z0-9=!@#$%^&*()-] - 以
[a-zA-z0-9]结尾
到目前为止,我的端点设置如下
from flask import Flask
from flask_restplus import Api, Resource, fields
flask_app= Flask (__name__)
app = Api(app = flask_app,
version = "1.0",
title = "Test API",
description = "Example endpoint to test validation of POST body parameter using Flaskplus")
model = app.model (
'username model',
{
'username': fields.String (
required = True,
description = "Username of the user",
help = "Username cannot be blank, must be of length 10, can start with an upper or lower case letter, can contain upper/lower case, numbers, and the following special characters [=!@#$%^&*()-], and must end with an upper/lower case letter or a number",
example = "john.smith123"
)
}
)
name_space = app.namespace ('', 'Main API')
@name_space.route ("/test")
class test (Resource)
@app.doc(
responses = {
200: 'OK',
400: 'Invalid Argument',
500: 'Mapping Key Error'
}
)
@app.expect (model)
def post (self):
try:
username = request.json.get('username')
return {
'status': 'Username valid',
'status_code': 200
}
except KeyError as e:
name_space.abort (
500,
e.__doc__,
status = "Key error",
statusCode = "500"
)
【问题讨论】:
标签: api validation flask swagger documentation