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

33 lines
1.1 KiB
Python
Raw Normal View History

2024-03-07 14:33:01 +01:00
from pydantic import BaseModel, computed_field, field_validator, NonNegativeInt
2024-03-07 14:06:53 +01:00
from .TabSchema import TabSchema
import datetime
import re
class RequestSchema(BaseModel):
username: str
2024-03-07 14:33:01 +01:00
expiry: NonNegativeInt = 1
2024-03-07 14:06:53 +01:00
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
2024-03-07 14:33:01 +01:00
@computed_field(return_type=NonNegativeInt)
2024-03-07 14:06:53 +01:00
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