wag-managment-api-service-v.../ApiEvents/LanguageServiceApi/zod_messages.py

82 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Dict
from fastapi import APIRouter, Header
from pydantic import BaseModel
from typing import Optional
class ZodMessages(BaseModel):
"""Messages that match Zod's error types"""
required_error: str
invalid_type_error: str
invalid_string: Dict[str, str] # email, url, etc
too_small: Dict[str, str] # string, array, number
too_big: Dict[str, str] # string, array, number
custom: Dict[str, str] # custom validation messages
class LanguageService:
def __init__(self):
self.messages = {
"tr": {
"required_error": "Bu alan zorunludur",
"invalid_type_error": "Geçersiz tip",
"invalid_string": {
"email": "Geçerli bir e-posta adresi giriniz",
"url": "Geçerli bir URL giriniz",
"uuid": "Geçerli bir UUID giriniz"
},
"too_small": {
"string": "{min} karakterden az olamaz",
"array": "En az {min} öğe gereklidir",
"number": "En az {min} olmalıdır"
},
"too_big": {
"string": "{max} karakterden fazla olamaz",
"array": "En fazla {max} öğe olabilir",
"number": "En fazla {max} olabilir"
},
"custom": {
"password_match": "Şifreler eşleşmiyor",
"unique_email": "Bu e-posta adresi zaten kullanılıyor",
"strong_password": "Şifre en az bir büyük harf, bir küçük harf ve bir rakam içermelidir"
}
},
"en": {
"required_error": "This field is required",
"invalid_type_error": "Invalid type",
"invalid_string": {
"email": "Please enter a valid email",
"url": "Please enter a valid URL",
"uuid": "Please enter a valid UUID"
},
"too_small": {
"string": "Must be at least {min} characters",
"array": "Must contain at least {min} items",
"number": "Must be at least {min}"
},
"too_big": {
"string": "Must be at most {max} characters",
"array": "Must contain at most {max} items",
"number": "Must be at most {max}"
},
"custom": {
"password_match": "Passwords do not match",
"unique_email": "This email is already in use",
"strong_password": "Password must contain at least one uppercase letter, one lowercase letter, and one number"
}
}
}
def get_messages(self, lang: str = "tr") -> Dict:
"""Get all Zod messages for a specific language"""
return self.messages.get(lang, self.messages["tr"])
router = APIRouter(prefix="/api/language", tags=["Language"])
language_service = LanguageService()
@router.get("/zod-messages")
async def get_zod_messages(
accept_language: Optional[str] = Header(default="tr")
) -> Dict:
"""Get Zod validation messages based on Accept-Language header"""
lang = accept_language.split(",")[0][:2] # Get primary language code
return language_service.get_messages(lang if lang in ["tr", "en"] else "tr")