mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
36 lines
911 B
Python
36 lines
911 B
Python
from datetime import datetime
|
|
from typing import Annotated, Any
|
|
|
|
from pydantic import BaseModel, ConfigDict, EmailStr, Field, validate_email
|
|
|
|
from backend.utils.timezone import timezone
|
|
|
|
CustomPhoneNumber = Annotated[str, Field(pattern=r'^1[3-9]\d{9}$')]
|
|
|
|
|
|
class CustomEmailStr(EmailStr):
|
|
"""自定义邮箱类型"""
|
|
|
|
@classmethod
|
|
def _validate(cls, input_value: str, /) -> str:
|
|
return None if not input_value else validate_email(input_value)[1]
|
|
|
|
|
|
class SchemaBase(BaseModel):
|
|
"""基础模型配置"""
|
|
|
|
model_config = ConfigDict(
|
|
use_enum_values=True,
|
|
json_encoders={
|
|
datetime: lambda x: timezone.to_str(timezone.from_datetime(x))
|
|
if x.tzinfo is not None and x.tzinfo != timezone.tz_info
|
|
else timezone.to_str(x),
|
|
},
|
|
)
|
|
|
|
|
|
def ser_string(value: Any) -> str | None:
|
|
if value:
|
|
return str(value)
|
|
return value
|