mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 19:11:00 +00:00
✨ Feature: 优化 pydantic 兼容函数 model_dump
和 type_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:
@ -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)
|
||||
|
Reference in New Issue
Block a user