refactor: use NamedTuple for validation results

This commit is contained in:
2025-12-20 22:36:51 +01:00
parent 54c3353667
commit 35c8b5b8d1

View File

@@ -3,19 +3,25 @@
import getpass import getpass
import re import re
from pathlib import Path from pathlib import Path
from typing import NamedTuple
import yaml import yaml
from .logger import setup_logger from .logger import setup_logger
def _validate_handle(handle: str) -> tuple[bool, str]: class ValidationResult(NamedTuple):
is_valid: bool
error_message: str
def _validate_handle(handle: str) -> ValidationResult:
if not handle: if not handle:
return False, "Handle cannot be empty" return ValidationResult(False, "Handle cannot be empty")
if len(handle) > 253: if len(handle) > 253:
return False, "Handle is too long (max 253 characters)" return ValidationResult(False, "Handle is too long (max 253 characters)")
if " " in handle: if " " in handle:
return False, "Handle cannot contain spaces" return ValidationResult(False, "Handle cannot contain spaces")
handle_pattern = re.compile( handle_pattern = re.compile(
r"^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$|" r"^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$|"
@@ -24,23 +30,23 @@ def _validate_handle(handle: str) -> tuple[bool, str]:
) )
if not handle_pattern.match(handle): if not handle_pattern.match(handle):
return False, ( return ValidationResult(False, (
"Invalid handle format. " "Invalid handle format. "
"Use a username (e.g., 'alice'), full handle (e.g., 'alice.bsky.social'), " "Use a username (e.g., 'alice'), full handle (e.g., 'alice.bsky.social'), "
"or DID (e.g., 'did:plc:...')" "or DID (e.g., 'did:plc:...')"
) ))
return True, "" return ValidationResult(True, "")
def _validate_password(password: str) -> tuple[bool, str]: def _validate_password(password: str) -> ValidationResult:
if not password: if not password:
return False, "Password cannot be empty" return ValidationResult(False, "Password cannot be empty")
if len(password) < 8: if len(password) < 8:
return False, "Password is too short (minimum 8 characters)" return ValidationResult(False, "Password is too short (minimum 8 characters)")
return True, "" return ValidationResult(True, "")
class Configuration: class Configuration: