diff --git a/pyproject.toml b/pyproject.toml index ffaa3e7..8661221 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "rcs-pydantic" -version = "0.2.0" +version = "0.2.1" description = "" authors = ["xncbf "] keywords = ["pydantic", "rcs", "fastapi"] diff --git a/rcs_pydantic/scheme.py b/rcs_pydantic/scheme.py index ef00e44..201e737 100644 --- a/rcs_pydantic/scheme.py +++ b/rcs_pydantic/scheme.py @@ -2,6 +2,7 @@ from typing import Dict, List, Optional, Union from pydantic import BaseModel, Field, validator +from pydantic.error_wrappers import ValidationError from . import enums from .errors import ErrorCodeEnum, LegacyErrorCodeEnum @@ -501,7 +502,7 @@ class MessageInfo(BaseModel): postBackId: Optional[str] = Field(max_length=40) postBackData: Optional[str] = Field(max_length=2048) displayText: Optional[str] = Field(max_length=200) - messageBody: Union[TextMessageInfo, UserLocationInfo, FileMessageInfo, None] + messageBody: Optional[str] """ eventType이message인 경우에만 설정 - 텍스트 메시지의 경우 샘플 @@ -535,7 +536,13 @@ class MessageInfo(BaseModel): def check_message_body(cls, v, values, **kwargs): if v: if values["eventType"] == enums.EventTypeEnum.MESSAGE: - return json.loads(v) + v = json.loads(v) + for basemodel in [TextMessageInfo, UserLocationInfo, FileMessageInfo]: + try: + return basemodel(**v) + except ValidationError: + pass + raise ValueError("Invalid messageBody") raise ValueError("messageBody is not allowed") userContact: str = Field(max_length=40)