generated from dopt-python/py311
refactoring
This commit is contained in:
@@ -13,10 +13,47 @@ from pydantic import (
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
from pydantic_core import ErrorDetails
|
||||
|
||||
ValidAge = Annotated[int, Field(ge=0, le=99)]
|
||||
|
||||
|
||||
# // validation translation
|
||||
GERMAN_ERROR_MESSAGES: Final[dict[str, str]] = {
|
||||
"missing": "Dieses Feld ist ein Pflichtfeld.",
|
||||
"int_parsing": "Bitte eine gültige Ganzzahl eingeben.",
|
||||
"float_parsing": "Bitte eine gültige Dezimalzahl eingeben.",
|
||||
"string_too_short": "Der Text ist zu kurz. Er muss mindestens {min_length} Zeichen lang sein.",
|
||||
"string_too_long": "Der Text ist zu lang. Er darf maximal {max_length} Zeichen lang sein.",
|
||||
"greater_than": "Der Wert muss größer als {gt} sein.",
|
||||
}
|
||||
|
||||
|
||||
def translate_pydantic_errors(
|
||||
errors: list[ErrorDetails],
|
||||
) -> list[ErrorDetails]:
|
||||
translated_errors: list[ErrorDetails] = []
|
||||
|
||||
for error in errors:
|
||||
err_type = error.get("type", None)
|
||||
|
||||
if err_type is None or err_type not in GERMAN_ERROR_MESSAGES:
|
||||
error["msg"] = "[fehleden Übersetzung, nur Englisch]: " + error["msg"]
|
||||
translated_errors.append(error)
|
||||
continue
|
||||
|
||||
msg_template = GERMAN_ERROR_MESSAGES[err_type]
|
||||
ctx = error.get("ctx", {})
|
||||
try:
|
||||
error["msg"] = msg_template.format(**ctx)
|
||||
except KeyError:
|
||||
error["msg"] = msg_template
|
||||
|
||||
translated_errors.append(error)
|
||||
|
||||
return translated_errors
|
||||
|
||||
|
||||
def _parse_json(value: Any) -> str:
|
||||
if isinstance(value, datetime.date):
|
||||
return value.isoformat()
|
||||
|
||||
Reference in New Issue
Block a user