65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
_DEVICE_ID_RE = re.compile(r"^[A-Za-z0-9_.:\-]{4,128}$")
|
|
_FCM_TOKEN_RE = re.compile(r"^[A-Za-z0-9_.:\-]+$")
|
|
|
|
|
|
class PushTokenRegister(BaseModel):
|
|
device_id: str = Field(min_length=4, max_length=128)
|
|
platform: str = Field(default="android", min_length=2, max_length=20)
|
|
fcm_token: str = Field(min_length=20, max_length=4096)
|
|
|
|
@field_validator("device_id")
|
|
@classmethod
|
|
def validate_device_id(cls, value: str) -> str:
|
|
cleaned = value.strip()
|
|
if not _DEVICE_ID_RE.fullmatch(cleaned):
|
|
raise ValueError("Invalid device_id format")
|
|
return cleaned
|
|
|
|
@field_validator("platform")
|
|
@classmethod
|
|
def validate_platform(cls, value: str) -> str:
|
|
cleaned = value.strip().lower()
|
|
if cleaned not in {"android"}:
|
|
raise ValueError("Unsupported push platform")
|
|
return cleaned
|
|
|
|
@field_validator("fcm_token")
|
|
@classmethod
|
|
def validate_fcm_token(cls, value: str) -> str:
|
|
cleaned = value.strip()
|
|
if not _FCM_TOKEN_RE.fullmatch(cleaned):
|
|
raise ValueError("Invalid fcm_token format")
|
|
return cleaned
|
|
|
|
|
|
class PushTokenUnregister(BaseModel):
|
|
device_id: str = Field(min_length=4, max_length=128)
|
|
|
|
@field_validator("device_id")
|
|
@classmethod
|
|
def validate_device_id(cls, value: str) -> str:
|
|
cleaned = value.strip()
|
|
if not _DEVICE_ID_RE.fullmatch(cleaned):
|
|
raise ValueError("Invalid device_id format")
|
|
return cleaned
|
|
|
|
|
|
class PushDeviceTokenOut(BaseModel):
|
|
id: str
|
|
device_id: str
|
|
platform: str
|
|
is_active: bool
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
last_seen_at: datetime | None
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|