简体   繁体   中英

Python flask response validation

I am using python rebar for validating request_body_schema, it works well. we can validate input body arguments. Its cool, we do not want to implement any manual input validations like adding if statements.

Same way I could not able to validate response arguments.

But flask_rebar mentioned we can implement 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).

But I am not getting any example, Can any body help me with example

my code:

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()

To get the example to work in Flask-Rebar 2.0, you have to replace marshal_schema argument in the @registry.handle decorator with response_body_schema . marshal_schema is the old deprecated name which was removed in Rebar 2.0. The new name response_body_schema was introduced in Rebar 1.7.

response_body_schema is a Marshmallow schema that will be used marshal the return value of the function. marshmallow.Schema.dump will be called on the return value. response_body_schema can also be a dictionary mapping status codes to Marshmallow schemas - see Marshaling. NOTE: In Flask-Rebar 1.0-1.7.0, this was referred to as marshal_schema . It is being renamed and both names will function until version 2.0. Basics - Flask-Rebar documentation

https://flask-restplus.readthedocs.io/en/stable/marshalling.html

`@api.marshal_with`

user reponse marshaling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM