import json import re from typing import Any, Dict, Optional, Tuple from uuid import UUID USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$") IDEMPOTENCY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$") def parse_json_body(raw: bytes) -> Tuple[Optional[Dict[str, Any]], Optional[str]]: if not raw: return None, "empty body" try: data = json.loads(raw.decode("utf-8")) except (UnicodeDecodeError, json.JSONDecodeError): return None, "malformed JSON" if not isinstance(data, dict): return None, "JSON body must be an object" return data, None def require_exact_keys(data: Dict[str, Any], allowed: set, required: set) -> Optional[str]: keys = set(data.keys()) if keys - allowed: return "unexpected fields" if required - keys: return "missing required fields" return None def validate_username(value: Any) -> Optional[str]: if not isinstance(value, str) or not USERNAME_RE.fullmatch(value): return "invalid username" return None def validate_display_name(value: Any) -> Optional[str]: if not isinstance(value, str): return "invalid display_name" if not value.strip(): return "display_name must not be blank" if len(value) > 100: return "display_name too long" return None def validate_password(value: Any) -> Optional[str]: if not isinstance(value, str): return "invalid password" if not value.strip(): return "password must not be blank" if len(value) < 12 or len(value) > 128: return "password length" return None def validate_title(value: Any) -> Optional[str]: if not isinstance(value, str): return "invalid title" if not value.strip(): return "title must not be blank" if len(value) > 100: return "title too long" return None def validate_uuid(value: Any) -> Optional[str]: if not isinstance(value, str): return "invalid uuid" try: UUID(value) except ValueError: return "invalid uuid" return None def validate_idempotency_key(value: Optional[str]) -> Optional[str]: if value is None or IDEMPOTENCY_RE.fullmatch(value) is None: return "invalid idempotency key" return None def validate_message_text(value: Any, *, allow_blank: bool = False) -> Optional[str]: if not isinstance(value, str): return "invalid text" if len(value) > 4000: return "text too long" if not allow_blank and not value.strip(): return "text must not be blank" return None def validate_filename(value: Optional[str]) -> Optional[str]: if not isinstance(value, str) or not value.strip() or len(value) > 255: return "invalid filename" for char in value: code = ord(char) if code < 32 or code == 127 or char in "/\\": return "invalid filename" return None def parse_attachment_ids(value: Any) -> Tuple[Optional[list], Optional[str]]: if value is None: return [], None if not isinstance(value, list): return None, "invalid attachment_ids" if len(value) > 10: return None, "too many attachments" seen = set() ids = [] for item in value: if validate_uuid(item): return None, "invalid attachment id" if item in seen: return None, "duplicate attachment_ids" seen.add(item) ids.append(item) return ids, None def validate_limit(value: Optional[str], default: int = 50) -> Tuple[Optional[int], Optional[str]]: if value is None: return default, None try: limit = int(value) except ValueError: return None, "invalid limit" if limit < 1 or limit > 100: return None, "limit out of range" return limit, None