Add input validation for help command name and category
The help command creation modal was accepting names with spaces and special characters (e.g., "scorecard links"), which passed to the API but caused Pydantic validation errors when reading the records back. Changes: - Add regex validation in modal on_submit for topic name and category - Only allow lowercase letters, numbers, dashes, and underscores - Show clear error messages with valid examples when validation fails - Normalize name/category to lowercase before storing This prevents invalid records from being created in the database. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
c138276797
commit
213e754673
@ -59,12 +59,44 @@ class HelpCommandCreateModal(BaseModal):
|
||||
|
||||
async def on_submit(self, interaction: discord.Interaction):
|
||||
"""Handle form submission."""
|
||||
import re
|
||||
|
||||
# Validate topic name format
|
||||
name = self.topic_name.value.strip().lower()
|
||||
if not re.match(r'^[a-z0-9_-]+$', name):
|
||||
embed = EmbedTemplate.error(
|
||||
title="Invalid Topic Name",
|
||||
description=(
|
||||
f"Topic name `{self.topic_name.value}` contains invalid characters.\n\n"
|
||||
"**Allowed:** lowercase letters, numbers, dashes, and underscores only.\n"
|
||||
"**Examples:** `trading-rules`, `how_to_draft`, `faq1`\n\n"
|
||||
"Please try again with a valid name."
|
||||
)
|
||||
)
|
||||
await interaction.response.send_message(embed=embed, ephemeral=True)
|
||||
return
|
||||
|
||||
# Validate category format if provided
|
||||
category = self.topic_category.value.strip().lower() if self.topic_category.value else None
|
||||
if category and not re.match(r'^[a-z0-9_-]+$', category):
|
||||
embed = EmbedTemplate.error(
|
||||
title="Invalid Category",
|
||||
description=(
|
||||
f"Category `{self.topic_category.value}` contains invalid characters.\n\n"
|
||||
"**Allowed:** lowercase letters, numbers, dashes, and underscores only.\n"
|
||||
"**Examples:** `rules`, `guides`, `faq`\n\n"
|
||||
"Please try again with a valid category."
|
||||
)
|
||||
)
|
||||
await interaction.response.send_message(embed=embed, ephemeral=True)
|
||||
return
|
||||
|
||||
# Store results
|
||||
self.result = {
|
||||
'name': self.topic_name.value.strip(),
|
||||
'name': name,
|
||||
'title': self.topic_title.value.strip(),
|
||||
'content': self.topic_content.value.strip(),
|
||||
'category': self.topic_category.value.strip() if self.topic_category.value else None
|
||||
'category': category
|
||||
}
|
||||
|
||||
self.is_submitted = True
|
||||
|
||||
Loading…
Reference in New Issue
Block a user