| Server IP : 3.147.158.171 / Your IP : 216.73.216.216 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/www/html/api.turmansolutions.ai/app/utilities/ |
Upload File : |
"""
Slug Validation Utility
Validates slugs used for subdomains, database names, directories, and usernames.
"""
import re
# Slugs reserved for infrastructure, services, or common subdomains.
RESERVED_SLUGS = frozenset({
'www', 'mail', 'ftp', 'admin', 'api', 'chat', 'store', 'test',
'app', 'blog', 'dev', 'staging', 'smtp', 'pop', 'imap', 'ns1',
'ns2', 'dns', 'cdn', 'vpn', 'ssh', 'sftp', 'webmail', 'cpanel',
'dashboard', 'login', 'signup', 'register', 'support', 'help',
'status', 'docs',
})
# Pattern: starts and ends with [a-z0-9], allows hyphens in middle.
SLUG_PATTERN = re.compile(r'^[a-z0-9]([a-z0-9-]*[a-z0-9])?$')
MIN_LENGTH = 3
MAX_LENGTH = 20
def validate_slug_format(slug: str) -> dict:
"""
Validate a slug's format.
Returns:
dict with 'valid' (bool) and 'message' (str) keys.
"""
if len(slug) < MIN_LENGTH:
return {'valid': False, 'message': 'Slug must be at least 3 characters long.'}
if len(slug) > MAX_LENGTH:
return {'valid': False, 'message': 'Slug must be no more than 20 characters long.'}
if not SLUG_PATTERN.match(slug):
return {
'valid': False,
'message': 'Slug must contain only lowercase letters, numbers, and hyphens, '
'and must start and end with a letter or number.',
}
if '--' in slug:
return {'valid': False, 'message': 'Slug must not contain consecutive hyphens.'}
if slug in RESERVED_SLUGS:
return {'valid': False, 'message': f'The slug "{slug}" is reserved and cannot be used.'}
return {'valid': True, 'message': ''}
def slug_to_db_name(slug: str) -> str:
"""Convert a slug to a valid MySQL database name component by replacing hyphens with underscores."""
return slug.replace('-', '_')