如何在 Flask 中验证参数
How to validate parameters in Flask
有没有办法验证 Flask 中的 sting 以检查以下内容。我希望它不仅能出现在 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"
)
您可以使用 flask-parameter-validation 项目来执行此操作。您将能够使用类似于以下内容的方法来实现它:
from flask_parameter_validation import ValidateParameters, Json
@name_space.route("/test", methods=["POST"])
@ValidateParameters()
def hello(
username: str = Json(
min_str_length=10,
max_str_length=1024,
pattern=r"REGEX"
)
):
return f"Hello, {username}"
您需要将 REGEX 替换为满足开始、中间和结束条件的正则表达式模式。
希望对您有所帮助!
有没有办法验证 Flask 中的 sting 以检查以下内容。我希望它不仅能出现在 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"
)
您可以使用 flask-parameter-validation 项目来执行此操作。您将能够使用类似于以下内容的方法来实现它:
from flask_parameter_validation import ValidateParameters, Json
@name_space.route("/test", methods=["POST"])
@ValidateParameters()
def hello(
username: str = Json(
min_str_length=10,
max_str_length=1024,
pattern=r"REGEX"
)
):
return f"Hello, {username}"
您需要将 REGEX 替换为满足开始、中间和结束条件的正则表达式模式。
希望对您有所帮助!