redis implemntations and api setup completed
This commit is contained in:
@@ -2,30 +2,37 @@ from typing import Dict, Any, Type, Optional
|
||||
from pydantic import BaseModel
|
||||
from fastapi import APIRouter, Header
|
||||
|
||||
|
||||
class ValidationMessages(BaseModel):
|
||||
"""Messages for Zod validation"""
|
||||
|
||||
required: str
|
||||
invalid_type: 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
|
||||
too_small: Dict[str, str] # string, array, number
|
||||
too_big: Dict[str, str] # string, array, number
|
||||
invalid_date: str
|
||||
invalid_enum: str
|
||||
custom: Dict[str, str]
|
||||
|
||||
|
||||
class SchemaField(BaseModel):
|
||||
"""Schema field definition"""
|
||||
|
||||
type: str
|
||||
items: Optional[str] = None # For arrays
|
||||
values: Optional[list] = None # For enums
|
||||
validations: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
class SchemaDefinition(BaseModel):
|
||||
"""Complete schema definition"""
|
||||
|
||||
name: str
|
||||
fields: Dict[str, SchemaField]
|
||||
messages: ValidationMessages
|
||||
|
||||
|
||||
class UnifiedSchemaService:
|
||||
def __init__(self):
|
||||
self.messages = {
|
||||
@@ -35,24 +42,24 @@ class UnifiedSchemaService:
|
||||
invalid_string={
|
||||
"email": "Geçerli bir e-posta adresi giriniz",
|
||||
"url": "Geçerli bir URL giriniz",
|
||||
"uuid": "Geçerli bir UUID 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"
|
||||
"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"
|
||||
"number": "En fazla {max} olabilir",
|
||||
},
|
||||
invalid_date="Geçerli bir tarih giriniz",
|
||||
invalid_enum="Geçersiz seçim",
|
||||
custom={
|
||||
"password_match": "Şifreler eşleşmiyor",
|
||||
"strong_password": "Şifre güçlü değil"
|
||||
}
|
||||
"strong_password": "Şifre güçlü değil",
|
||||
},
|
||||
),
|
||||
"en": ValidationMessages(
|
||||
required="This field is required",
|
||||
@@ -60,48 +67,44 @@ class UnifiedSchemaService:
|
||||
invalid_string={
|
||||
"email": "Please enter a valid email",
|
||||
"url": "Please enter a valid URL",
|
||||
"uuid": "Please enter a valid UUID"
|
||||
"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}"
|
||||
"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}"
|
||||
"number": "Must be at most {max}",
|
||||
},
|
||||
invalid_date="Please enter a valid date",
|
||||
invalid_enum="Invalid selection",
|
||||
custom={
|
||||
"password_match": "Passwords do not match",
|
||||
"strong_password": "Password is not strong enough"
|
||||
}
|
||||
)
|
||||
"strong_password": "Password is not strong enough",
|
||||
},
|
||||
),
|
||||
}
|
||||
|
||||
def get_schema_with_messages(
|
||||
self,
|
||||
model: Type[BaseModel],
|
||||
lang: str = "tr"
|
||||
self, model: Type[BaseModel], lang: str = "tr"
|
||||
) -> SchemaDefinition:
|
||||
"""Get schema definition with validation messages"""
|
||||
fields: Dict[str, SchemaField] = {}
|
||||
|
||||
|
||||
for field_name, field in model.__fields__.items():
|
||||
field_info = SchemaField(
|
||||
type=self._get_field_type(field.outer_type_),
|
||||
items=self._get_items_type(field.outer_type_),
|
||||
values=self._get_enum_values(field.outer_type_),
|
||||
validations=self._get_validations(field)
|
||||
validations=self._get_validations(field),
|
||||
)
|
||||
fields[field_name] = field_info
|
||||
|
||||
return SchemaDefinition(
|
||||
name=model.__name__,
|
||||
fields=fields,
|
||||
messages=self.messages[lang]
|
||||
name=model.__name__, fields=fields, messages=self.messages[lang]
|
||||
)
|
||||
|
||||
def _get_field_type(self, type_: Type) -> str:
|
||||
@@ -120,13 +123,14 @@ class UnifiedSchemaService:
|
||||
# Implementation similar to SchemaConverter
|
||||
pass
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/schema", tags=["Schema"])
|
||||
schema_service = UnifiedSchemaService()
|
||||
|
||||
|
||||
@router.get("/model/{model_name}")
|
||||
async def get_model_schema(
|
||||
model_name: str,
|
||||
accept_language: Optional[str] = Header(default="tr")
|
||||
model_name: str, accept_language: Optional[str] = Header(default="tr")
|
||||
) -> SchemaDefinition:
|
||||
"""Get model schema with validation messages"""
|
||||
# You'd need to implement model lookup
|
||||
@@ -135,12 +139,11 @@ async def get_model_schema(
|
||||
"Product": ProductModel,
|
||||
# Add your models here
|
||||
}
|
||||
|
||||
|
||||
if model_name not in models:
|
||||
raise ValueError(f"Model {model_name} not found")
|
||||
|
||||
|
||||
lang = accept_language.split(",")[0][:2]
|
||||
return schema_service.get_schema_with_messages(
|
||||
models[model_name],
|
||||
lang if lang in ["tr", "en"] else "tr"
|
||||
models[model_name], lang if lang in ["tr", "en"] else "tr"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user