diff --git a/src/utils/validation/__init__.py b/src/utils/validation/__init__.py index b9f19651..f9244d6a 100644 --- a/src/utils/validation/__init__.py +++ b/src/utils/validation/__init__.py @@ -4,7 +4,6 @@ from pydantic import ValidationError -from .constants import CUSTOM_MESSAGES from .models import ( AdapterPublishInfo, BotPublishInfo, @@ -14,7 +13,7 @@ ) from .models import PublishType as PublishType from .models import ValidationDict as ValidationDict -from .utils import color_to_hex, convert_errors +from .utils import color_to_hex, translate_errors validation_model_map: dict[PublishType, type[PublishInfo]] = { PublishType.BOT: BotPublishInfo, @@ -50,7 +49,7 @@ def validate_info( data: dict[str, Any] = validation_context["valid_data"] # 翻译错误 - errors = convert_errors(errors, CUSTOM_MESSAGES) + errors = translate_errors(errors) # tags 会被转成 list[Tag],需要转成 dict if "tags" in data: diff --git a/src/utils/validation/constants.py b/src/utils/validation/constants.py index 126e631f..f4c2e896 100644 --- a/src/utils/validation/constants.py +++ b/src/utils/validation/constants.py @@ -23,7 +23,7 @@ ) # Pydantic 错误信息翻译 -CUSTOM_MESSAGES = { +MESSAGE_TRANSLATIONS = { "model_type": "值不是合法的字典", "list_type": "值不是合法的列表", "set_type": "值不是合法的集合", diff --git a/src/utils/validation/utils.py b/src/utils/validation/utils.py index ebf9b546..a641d49b 100644 --- a/src/utils/validation/utils.py +++ b/src/utils/validation/utils.py @@ -5,7 +5,7 @@ from nonebot import logger from pydantic_extra_types.color import Color, float_to_255 -from .constants import STORE_ADAPTERS_URL +from .constants import MESSAGE_TRANSLATIONS, STORE_ADAPTERS_URL if TYPE_CHECKING: from pydantic_core import ErrorDetails @@ -55,13 +55,11 @@ def color_to_hex(color: Color) -> str: return f"#{hex}" -def convert_errors( - errors: list["ErrorDetails"], custom_messages: dict[str, str] -) -> list["ErrorDetails"]: +def translate_errors(errors: list["ErrorDetails"]) -> list["ErrorDetails"]: """翻译 Pydantic 错误信息""" new_errors: list["ErrorDetails"] = [] for error in errors: - custom_message = custom_messages.get(error["type"]) + custom_message = MESSAGE_TRANSLATIONS.get(error["type"]) if custom_message: ctx = error.get("ctx") error["msg"] = custom_message.format(**ctx) if ctx else custom_message