Skip to content

Commit

Permalink
feat(pydiscordsh): adding discord invite validation, also fix regex l…
Browse files Browse the repository at this point in the history
…imits
  • Loading branch information
Fudster committed Jan 9, 2025
1 parent d4f5bdb commit ee660cb
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions apps/pydiscordsh/pydiscordsh/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class Config:
arbitrary_types_allowed = True
validate_assignment = True

@validator("invite", pre=True, always=True)
def validate_invite(cls, value):
discord_invite_pattern = r"(?:https?://(?:www\.)?discord(?:\.com)?/invite/|discord\.gg/)([a-zA-Z0-9_-]+)"
if value:
match = re.match(discord_invite_pattern, value)
if match:
return match.group(1) # Return only the invite code
if len(value) < 100 and re.match(r"^[a-zA-Z0-9_-]{100}$", value):
return value

@validator("categories", pre=True, always=True)
def validate_categories(cls, value):
if value and len(value) > 2:
Expand All @@ -49,13 +59,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_-]{50}))|youtu\.be/([a-zA-Z0-9_-]{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_-]{50}$", value):
return value
raise ValueError("Invalid YouTube video ID or URL.")

Expand Down

0 comments on commit ee660cb

Please sign in to comment.