We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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))
app.middlewares.insert(0, validation_middleware(400))
The text was updated successfully, but these errors were encountered:
Hi! Yes, that's a good idea! But it should be a new function in api (like create_validation_middleware).
create_validation_middleware
I already add error_callback parameter to setup_aiohttp_apispec so you can make your custom error handler.
error_callback
setup_aiohttp_apispec
And if we will decide to add this middleware fabric to the library, it should not have conflicts in interface with setup_aiohttp_apispec.
Sorry, something went wrong.
No branches or pull requests
I offer this idea:
and
app.middlewares.insert(0, validation_middleware(400))
The text was updated successfully, but these errors were encountered: