feat: update pydantic
This commit is contained in:
@@ -4,9 +4,10 @@ from typing import Iterator, List, Optional
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_response import json_response
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, RootModel
|
||||
|
||||
from aiohttp_pydantic import PydanticView
|
||||
from aiohttp_pydantic.view import PydanticValidationError
|
||||
|
||||
|
||||
class ArticleModel(BaseModel):
|
||||
@@ -14,11 +15,11 @@ class ArticleModel(BaseModel):
|
||||
nb_page: Optional[int]
|
||||
|
||||
|
||||
class ArticleModels(BaseModel):
|
||||
__root__: List[ArticleModel]
|
||||
class ArticleModels(RootModel):
|
||||
root: List[ArticleModel]
|
||||
|
||||
def __iter__(self) -> Iterator[ArticleModel]:
|
||||
return iter(self.__root__)
|
||||
return iter(self.root)
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
@@ -30,14 +31,12 @@ class ArticleView(PydanticView):
|
||||
|
||||
async def on_validation_error(self, exception, context):
|
||||
errors = exception.errors()
|
||||
for error in errors:
|
||||
error["in"] = context
|
||||
error["custom"] = "custom"
|
||||
return json_response(data=errors, status=400)
|
||||
own_errors = [PydanticValidationError(**x, loc_in=context) for x in errors]
|
||||
return json_response(data=own_errors, status=400)
|
||||
|
||||
|
||||
async def test_post_an_article_with_wrong_type_field_should_return_an_error_message(
|
||||
aiohttp_client, event_loop
|
||||
aiohttp_client, event_loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
@@ -47,12 +46,14 @@ async def test_post_an_article_with_wrong_type_field_should_return_an_error_mess
|
||||
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"in": "body",
|
||||
"loc": ["nb_page"],
|
||||
"msg": "value is not a valid integer",
|
||||
"custom": "custom",
|
||||
"type": "type_error.integer",
|
||||
'loc_in': 'body',
|
||||
'input': 'foo',
|
||||
'loc': ['nb_page'],
|
||||
'msg': 'Input should be a valid integer, unable to parse string as an integer',
|
||||
'type': 'int_parsing',
|
||||
'url': 'https://errors.pydantic.dev/2.1.2/v/int_parsing'
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user