feat: update pydantic

This commit is contained in:
Georg K
2023-07-14 03:31:33 +03:00
parent 26fd6fa19f
commit a94c9d4863
9 changed files with 159 additions and 138 deletions

View File

@@ -10,6 +10,8 @@ from aiohttp.web_exceptions import HTTPMethodNotAllowed
from aiohttp.web_response import StreamResponse
from pydantic import ValidationError
from pydantic_core import ErrorDetails
from .injectors import (
AbstractInjector,
BodyGetter,
@@ -22,6 +24,10 @@ from .injectors import (
)
class PydanticValidationError(ErrorDetails):
loc_in: CONTEXT
class PydanticView(AbstractView):
"""
An AIOHTTP View that validate request using function annotations.
@@ -91,7 +97,7 @@ class PydanticView(AbstractView):
return injectors
async def on_validation_error(
self, exception: ValidationError, context: CONTEXT
self, exception: ValidationError, context: CONTEXT
) -> StreamResponse:
"""
This method is a hook to intercept ValidationError.
@@ -101,14 +107,13 @@ class PydanticView(AbstractView):
"headers", "path" or "query string"
"""
errors = exception.errors()
for error in errors:
error["in"] = context
own_errors = [PydanticValidationError(**x, loc_in=context) for x in errors]
return json_response(data=errors, status=400)
return json_response(data=own_errors, status=400)
def inject_params(
handler, parse_func_signature: Callable[[Callable], Iterable[AbstractInjector]]
handler, parse_func_signature: Callable[[Callable], Iterable[AbstractInjector]]
):
"""
Decorator to unpack the query string, route path, body and http header in
@@ -146,6 +151,7 @@ def is_pydantic_view(obj) -> bool:
__all__ = (
"PydanticValidationError",
"AbstractInjector",
"BodyGetter",
"HeadersGetter",