validator: hostname init

This commit is contained in:
Johannes Kirschbauer
2025-07-22 18:06:27 +02:00
parent a94cc4b7f7
commit 9257cb02ee

View File

@@ -0,0 +1,30 @@
import re
from clan_lib.errors import ClanError
def hostname(host: str) -> str:
"""
Validates a hostname according to the expected format in NixOS.
Usage Example
@dataclass
class Clan:
name: str
def validate(self) -> None:
from clan_lib.validator.hostname import hostname
try:
hostname(self.name)
except ValueError as e:
raise ClanError(str(e), location="name")
"""
# TODO: Generate from nix schema
hostname_regex = r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$"
if not re.fullmatch(hostname_regex, host):
msg = "Machine name must be a valid hostname"
raise ClanError(msg, location="Create Machine")
return host