-
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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
|
||||
Reference in New Issue
Block a user