diff --git a/apps/pydiscordsh/pydiscordsh/api/schema.py b/apps/pydiscordsh/pydiscordsh/api/schema.py index ee59cae96..f39c01628 100644 --- a/apps/pydiscordsh/pydiscordsh/api/schema.py +++ b/apps/pydiscordsh/pydiscordsh/api/schema.py @@ -40,7 +40,29 @@ class DiscordServer(SQLModel, table=True): class Config: arbitrary_types_allowed = True validate_assignment = True + @validator("lang", pre=True, always=True) + def validate_lang(cls, value): + if value: + if len(value) > 2: + raise ValueError("Language list cannot have more than two languages.") + valid_languages = {"en", "es", "zh", "hi", "fr", "ar", "de", "ja", "ru", "pt", "it", "ko", "tr", "vi", "pl"} + for lang in value: + if lang not in valid_languages: + raise ValueError(f"Invalid language code: {lang}. Must be one of {', '.join(cls.valid_languages)}.") + return value + @validator("invite", pre=True, always=True) + def validate_invite(cls, value): + if not value or not isinstance(value, str): + raise ValueError("Invite must be a valid string.") + discord_invite_pattern = r"(?:https?://(?:www\.)?discord(?:\.com)?/invite/|discord\.gg/)([a-zA-Z0-9_-]+)" + match = re.match(discord_invite_pattern, value) + if match: + return match.group(1) + if re.match(r"^[a-zA-Z0-9_-]{1,100}$", value): + return value + raise ValueError("Invalid invite link or invite code.") + @validator("categories", pre=True, always=True) def validate_categories(cls, value): if value and len(value) > 2: @@ -49,13 +71,12 @@ def validate_categories(cls, value): @validator("video", pre=True, always=True) def validate_video(cls, value): - youtube_url_pattern = r"(https?://(?:www\.)?(?:youtube\.com/(?:[^/]+/)*[^/]+(?:\?v=|\/)([a-zA-Z0-9_-]{11}))|youtu\.be/([a-zA-Z0-9_-]{11}))" - + youtube_url_pattern = r"(https?://(?:www\.)?(?:youtube\.com/(?:[^/]+/)*[^/]+(?:\?v=|\/)([a-zA-Z0-9_-]{1,50}))|youtu\.be/([a-zA-Z0-9_-]{1,50}))" if value: match = re.match(youtube_url_pattern, value) if match: return match.group(2) if match.group(2) else match.group(3) - if len(value) == 11 and re.match(r"^[a-zA-Z0-9_-]{11}$", value): + if len(value) < 50 and re.match(r"^[a-zA-Z0-9_-]{1,50}$", value): return value raise ValueError("Invalid YouTube video ID or URL.")