Feature: 优化 pydantic 兼容函数 model_dumptype_validate_json (#2579)

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
MingxuanGame
2024-02-17 23:18:00 +08:00
committed by GitHub
parent fbb8320a25
commit a830346545
2 changed files with 58 additions and 4 deletions

View File

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Optional
from dataclasses import dataclass
import pytest
@ -11,6 +11,7 @@ from nonebot.compat import (
PydanticUndefined,
model_dump,
custom_validation,
type_validate_json,
type_validate_python,
)
@ -66,3 +67,26 @@ async def test_custom_validation():
assert type_validate_python(TestModel, {"test": 1}) == TestModel(test=1)
assert called == [1, 2]
@pytest.mark.asyncio
async def test_validate_json():
class TestModel(BaseModel):
test1: int
test2: str
test3: bool
test4: dict
test5: list
test6: Optional[int]
assert type_validate_json(
TestModel,
"{"
' "test1": 1,'
' "test2": "2",'
' "test3": true,'
' "test4": {},'
' "test5": [],'
' "test6": null'
"}",
) == TestModel(test1=1, test2="2", test3=True, test4={}, test5=[], test6=None)