feat: add raise_validation_errors
parameter to setup()
To control how pydantic.ValidationError will be handled, own handler (return json) or raise exception to allow intercept in aiohttp middleware
This commit is contained in:
parent
efbaaa5e6f
commit
6ba671532b
@ -13,10 +13,13 @@ def setup(
|
||||
apps_to_expose: Iterable[web.Application] = (),
|
||||
url_prefix: str = "/oas",
|
||||
enable: bool = True,
|
||||
raise_validation_errors: bool = False,
|
||||
):
|
||||
if enable:
|
||||
oas_app = web.Application()
|
||||
oas_app["apps to expose"] = tuple(apps_to_expose) or (app,)
|
||||
for a in oas_app["apps to expose"]:
|
||||
a['raise_validation_errors'] = raise_validation_errors
|
||||
oas_app["index template"] = jinja2.Template(
|
||||
resources.read_text("aiohttp_pydantic.oas", "index.j2")
|
||||
)
|
||||
|
@ -83,7 +83,10 @@ def inject_params(
|
||||
else:
|
||||
injector.inject(self.request, args, kwargs)
|
||||
except ValidationError as error:
|
||||
return json_response(text=error.json(), status=400)
|
||||
if self.request.app['raise_validation_errors']:
|
||||
raise
|
||||
else:
|
||||
return json_response(text=error.json(), status=400)
|
||||
|
||||
return await handler(self, *args, **kwargs)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user