import re from typing import Optional EMAIL_RE = re.compile(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$") USERNAME_RE = re.compile(r"^[a-zA-Z0-9_-]{3,32}$") def is_valid_email(email: str) -> bool: """Return True if the email address is syntactically valid.""" return bool(EMAIL_RE.match(email)) def is_valid_username(username: str) -> bool: """Return True if username is 3-32 chars, alphanumeric/underscore/hyphen only.""" return bool(USERNAME_RE.match(username)) def is_strong_password(password: str) -> tuple[bool, Optional[str]]: """ Check password strength. Returns: (True, None) if valid, (False, reason) if invalid. """ if len(password) < 8: return False, "Password must be at least 8 characters" if not re.search(r"[A-Z]", password): return False, "Password must contain at least one uppercase letter" if not re.search(r"[0-9]", password): return False, "Password must contain at least one number" return True, None def sanitize_string(value: str, max_length: int = 500) -> str: """Strip whitespace and truncate a string to max_length.""" return value.strip()[:max_length]