rumine-api-wrapper/db/schemas/RequestSchema.py

33 lines
1.1 KiB
Python

from pydantic import BaseModel, computed_field, field_validator, NonNegativeInt
from .TabSchema import TabSchema
import datetime
import re
class RequestSchema(BaseModel):
username: str
expiry: NonNegativeInt = 1
tab: TabSchema = TabSchema()
@field_validator('username')
def validate_username(cls, value):
if not re.fullmatch(r'[a-zA-Z0-9_-]{4,16}', value):
raise ValueError("Invalid username format")
return value
@computed_field(return_type=NonNegativeInt)
def validated_expiry(self):
if self.expiry > 10**5:
try:
expiry_datetime = datetime.datetime.utcfromtimestamp(self.expiry)
if expiry_datetime <= datetime.datetime.utcnow():
raise ValueError("Expiry timestamp must be in the future")
return self.expiry
except ValueError:
raise ValueError("Invalid timestamp format")
else:
if self.expiry != 0:
now = datetime.datetime.utcnow()
return int((now + datetime.timedelta(days=self.expiry)).timestamp())
else:
return 0