Coverage for node / src / stigmem_node / tenant.py: 100%
15 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-25 01:49 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-25 01:49 +0000
1"""Tenant identifier validation and normalization helpers."""
3from __future__ import annotations
5import re
6import unicodedata
8DEFAULT_TENANT_ID = "default"
9TENANT_ID_PATTERN = re.compile(r"^[a-z0-9][a-z0-9-]{0,62}$")
12class TenantIdError(ValueError):
13 """Raised when a tenant identifier fails validation."""
16def validate_tenant_id(value: str | None) -> str:
17 """Normalize and validate a tenant identifier.
19 Tenant IDs are NFKC-normalized, stripped, lowercased, and then restricted
20 to 1-63 URL-safe characters: lowercase ASCII alphanumerics plus hyphen,
21 starting with an alphanumeric character.
22 """
24 if value is None:
25 raise TenantIdError("tenant_id_empty")
26 normalized = unicodedata.normalize("NFKC", str(value)).strip().lower()
27 if not normalized:
28 raise TenantIdError("tenant_id_empty")
29 if not TENANT_ID_PATTERN.fullmatch(normalized):
30 raise TenantIdError(f"tenant_id_invalid: {value!r}")
31 return normalized