Skip to content

Commit

Permalink
style: 重命名翻译错误的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Feb 5, 2024
1 parent b22ca43 commit 9ff335c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/utils/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pydantic import ValidationError

from .constants import CUSTOM_MESSAGES
from .models import (
AdapterPublishInfo,
BotPublishInfo,
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/validation/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)

# Pydantic 错误信息翻译
CUSTOM_MESSAGES = {
MESSAGE_TRANSLATIONS = {
"model_type": "值不是合法的字典",
"list_type": "值不是合法的列表",
"set_type": "值不是合法的集合",
Expand Down
8 changes: 3 additions & 5 deletions src/utils/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9ff335c

Please sign in to comment.