Files
saura_shakti_website/backend/app/schemas/common.py
T
Shubhankar45 53dbfa976b -
2026-11-13 12:14:55 +00:00

42 lines
977 B
Python

import re
from typing import Annotated, Generic, TypeVar
from pydantic import AfterValidator, BaseModel, ConfigDict
T = TypeVar("T")
_PHONE_ALLOWED = re.compile(r"^\+?[0-9\s\-()]{7,20}$")
def _validate_phone(value: str) -> str:
value = value.strip()
if not _PHONE_ALLOWED.match(value):
raise ValueError("Enter a valid phone number")
digits = re.sub(r"\D", "", value)
if not 7 <= len(digits) <= 15:
raise ValueError("Phone number must have 7 to 15 digits")
return ("+" if value.startswith("+") else "") + digits
Phone = Annotated[str, AfterValidator(_validate_phone)]
class InputModel(BaseModel):
model_config = ConfigDict(str_strip_whitespace=True, extra="forbid")
class OrmModel(BaseModel):
model_config = ConfigDict(from_attributes=True)
class Page(BaseModel, Generic[T]):
items: list[T]
total: int
page: int
page_size: int
class MessageResponse(BaseModel):
success: bool = True
message: str