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

1"""Tenant identifier validation and normalization helpers.""" 

2 

3from __future__ import annotations 

4 

5import re 

6import unicodedata 

7 

8DEFAULT_TENANT_ID = "default" 

9TENANT_ID_PATTERN = re.compile(r"^[a-z0-9][a-z0-9-]{0,62}$") 

10 

11 

12class TenantIdError(ValueError): 

13 """Raised when a tenant identifier fails validation.""" 

14 

15 

16def validate_tenant_id(value: str | None) -> str: 

17 """Normalize and validate a tenant identifier. 

18 

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 """ 

23 

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