Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize HTTP status on validation error #54

Open
v-v-vishnevskiy opened this issue May 31, 2019 · 1 comment
Open

Customize HTTP status on validation error #54

v-v-vishnevskiy opened this issue May 31, 2019 · 1 comment

Comments

@v-v-vishnevskiy
Copy link

v-v-vishnevskiy commented May 31, 2019

I offer this idea:

from aiohttp import web
from webargs.aiohttpparser import AIOHTTPParser

from .utils import issubclass_py37fix


def validation_middleware(status: int = AIOHTTPParser.DEFAULT_VALIDATION_STATUS):

    class Parser(AIOHTTPParser):
        DEFAULT_VALIDATION_STATUS = status

    parser = Parser()

    @web.middleware
    async def middleware(request: web.Request, handler) -> web.Response:
        """
        Validation middleware for aiohttp web app

        Usage:

        .. code-block:: python

            app.middlewares.append(validation_middleware)


        """
        orig_handler = request.match_info.handler
        if not hasattr(orig_handler, "__schemas__"):
            if not issubclass_py37fix(orig_handler, web.View):
                return await handler(request)
            sub_handler = getattr(orig_handler, request.method.lower(), None)
            if sub_handler is None:
                return await handler(request)
            if not hasattr(sub_handler, "__schemas__"):
                return await handler(request)
            schemas = sub_handler.__schemas__
        else:
            schemas = orig_handler.__schemas__
        kwargs = {}
        for schema in schemas:
            data = await parser.parse(
                schema["schema"], request, locations=schema["locations"]
            )
            if data:
                kwargs.update(data)
        kwargs.update(request.match_info)
        request[request.app["_apispec_request_data_name"]] = kwargs
        return await handler(request)
    return middleware

and app.middlewares.insert(0, validation_middleware(400))

@maximdanilchenko
Copy link
Owner

Hi!
Yes, that's a good idea! But it should be a new function in api (like create_validation_middleware).

I already add error_callback parameter to setup_aiohttp_apispec so you can make your custom error handler.

And if we will decide to add this middleware fabric to the library, it should not have conflicts in interface with setup_aiohttp_apispec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants