【问题标题】:Python flask response validationPython烧瓶响应验证
【发布时间】:2022-03-10 03:52:35
【问题描述】:

我正在使用 python rebar 来验证 request_body_schema,它运行良好。我们可以验证输入正文参数。很酷,我们不想实现任何手动输入验证,例如添加 if 语句。

我无法验证响应参数。

但是flask_rebar提到我们可以实现Link

Opting In to Response Validation
There are two ways to opt-in to response validation:

Globally, via validate_on_dump attribute of your Rebar instance. Using this method, it is easy to turn on validation for things like test cases, while reaping performance gains by leaving it off in your production endpoints (assuming your API contract testing is sufficient to guarantee that your API can’t return invalid data).
At schema level, via flask_rebar.validation.RequireOnDumpMixin (including if you use our legacy pre-canned ResponseSchema as the base class for your schemas). Any schema that includes that mixin is automatically opted in to response validation, regardless of global setting. Note that in Flask-Rebar 2, that mixin serves only as a “marker” to trigger validation; we plan to augment/replace this with ability to use SchemaOpts as a more logical way of accomplishing the same thing in the near future (https://github.com/plangrid/flask-rebar/issues/252).

但我没有得到任何例子,任何人都可以帮助我举例

我的代码:

from marshmallow import fields, Schema
from flask import Flask
from flask_rebar import Rebar, RequestSchema, get_validated_body

class CreateAccountSchema(RequestSchema):
    email = fields.String(required=True)
    country = fields.String(required=True)
    default_currency = fields.String(required=True)


class AccountSchema(Schema):
    id = fields.String()
    email = fields.String()
    country = fields.String()
    default_currency = fields.String(required=True) # if this is not passed raise error


rebar = Rebar()
registry = rebar.create_handler_registry(prefix="/v1")

@registry.handles(
    rule='/accounts',
    method='POST',
    marshal_schema={201: AccountSchema()},
    request_body_schema=CreateAccountSchema(),)

def get_todos():
    """
    This docstring will be rendered as the operation's description in
    the auto-generated OpenAPI specification.
    """
    body = get_validated_body()
    body = rebar.validated_body
    data = {"id": "myname", "email": "myemail", "country": "any"}
    return data, 201

@registry.handles(
    rule='/values',
    method='GET',
    marshal_schema=None,)

def get_values():
    """
    This docstring will be rendered as the operation's description in
    the auto-generated OpenAPI specification.
    """
    data = {"id": "myname", "email": "myemail", "country": "any"}
    return 'Hello, Poorvika'

def create_app(name) -> Flask:
    app = Flask(name)
    rebar.init_app(app)
    return app

if __name__ == '__main__':
    create_app(__name__).run()

【问题讨论】:

    标签: python python-3.x flask flask-restful


    【解决方案1】:

    要让示例在 Flask-Rebar 2.0 中运行,您必须将 @registry.handle 装饰器中的 marshal_schema 参数替换为 response_body_schemamarshal_schema 是旧的已弃用名称,已在 Rebar 2.0 中删除。 Rebar 1.7 中引入了新名称 response_body_schema

    response_body_schema 是一个 Marshmallow 模式,将用于编组函数的返回值。 marshmallow.Schema.dump 将在返回值上调用。 response_body_schema 也可以是将状态代码映射到 Marshmallow 模式的字典 - 请参阅编组。注意:在 Flask-Rebar 1.0-1.7.0 中,这被称为marshal_schema。它正在被重命名,并且两个名称都将在 2.0 版之前起作用。 – Basics - Flask-Rebar documentation

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 2018-08-17
      • 2017-12-04
      • 1970-01-01
      • 2016-05-03
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多