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:
		| @@ -13,10 +13,13 @@ def setup( | |||||||
|     apps_to_expose: Iterable[web.Application] = (), |     apps_to_expose: Iterable[web.Application] = (), | ||||||
|     url_prefix: str = "/oas", |     url_prefix: str = "/oas", | ||||||
|     enable: bool = True, |     enable: bool = True, | ||||||
|  |     raise_validation_errors: bool = False, | ||||||
| ): | ): | ||||||
|     if enable: |     if enable: | ||||||
|         oas_app = web.Application() |         oas_app = web.Application() | ||||||
|         oas_app["apps to expose"] = tuple(apps_to_expose) or (app,) |         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( |         oas_app["index template"] = jinja2.Template( | ||||||
|             resources.read_text("aiohttp_pydantic.oas", "index.j2") |             resources.read_text("aiohttp_pydantic.oas", "index.j2") | ||||||
|         ) |         ) | ||||||
|   | |||||||
| @@ -83,7 +83,10 @@ def inject_params( | |||||||
|                 else: |                 else: | ||||||
|                     injector.inject(self.request, args, kwargs) |                     injector.inject(self.request, args, kwargs) | ||||||
|             except ValidationError as error: |             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) |         return await handler(self, *args, **kwargs) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user