mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-09-15 12:50:42 -07:00
64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
"""
|
|
Input validation utilities.
|
|
"""
|
|
|
|
from typing import Tuple, Optional
|
|
from pathlib import Path
|
|
|
|
|
|
def validate_text(text: str, max_length: int = 5000) -> Tuple[bool, Optional[str]]:
|
|
"""
|
|
Validate text input.
|
|
|
|
Args:
|
|
text: Text to validate
|
|
max_length: Maximum length
|
|
|
|
Returns:
|
|
Tuple of (is_valid, error_message)
|
|
"""
|
|
if not text or not text.strip():
|
|
return False, "Text cannot be empty"
|
|
|
|
if len(text) > max_length:
|
|
return False, f"Text too long (maximum {max_length} characters)"
|
|
|
|
return True, None
|
|
|
|
|
|
def validate_language(language: str) -> Tuple[bool, Optional[str]]:
|
|
"""
|
|
Validate language code.
|
|
|
|
Args:
|
|
language: Language code
|
|
|
|
Returns:
|
|
Tuple of (is_valid, error_message)
|
|
"""
|
|
valid_languages = ["en", "zh"]
|
|
if language not in valid_languages:
|
|
return False, f"Invalid language (must be one of: {', '.join(valid_languages)})"
|
|
|
|
return True, None
|
|
|
|
|
|
def validate_file_path(path: str) -> Tuple[bool, Optional[str]]:
|
|
"""
|
|
Validate file path exists.
|
|
|
|
Args:
|
|
path: File path
|
|
|
|
Returns:
|
|
Tuple of (is_valid, error_message)
|
|
"""
|
|
file_path = Path(path)
|
|
if not file_path.exists():
|
|
return False, f"File not found: {path}"
|
|
|
|
if not file_path.is_file():
|
|
return False, f"Path is not a file: {path}"
|
|
|
|
return True, None
|