Spaces:
Sleeping
Sleeping
| """Input Validation Utilities""" | |
| from pathlib import Path | |
| from typing import Optional | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| def validate_code(code: str) -> None: | |
| """ | |
| Validate code input. | |
| Args: | |
| code: Source code string | |
| Raises: | |
| ValueError: If code is invalid | |
| """ | |
| if not code or not code.strip(): | |
| raise ValueError("Code cannot be empty") | |
| if len(code) > 1_000_000: # 1MB limit | |
| raise ValueError("Code size exceeds maximum limit (1MB)") | |
| def validate_path(path: str) -> None: | |
| """ | |
| Validate file/directory path. | |
| Args: | |
| path: File or directory path | |
| Raises: | |
| ValueError: If path is invalid | |
| """ | |
| if not path: | |
| raise ValueError("Path cannot be empty") | |
| path_obj = Path(path) | |
| if not path_obj.exists(): | |
| raise ValueError(f"Path does not exist: {path}") | |
| # Security: prevent path traversal | |
| try: | |
| path_obj.resolve() | |
| except Exception as e: | |
| raise ValueError(f"Invalid path: {e}") | |
| def validate_language(language: str) -> str: | |
| """ | |
| Validate and normalize language parameter. | |
| Args: | |
| language: Programming language | |
| Returns: | |
| str: Normalized language name | |
| Raises: | |
| ValueError: If language is not supported | |
| """ | |
| valid_languages = ["auto", "python", "javascript", "typescript"] | |
| language = language.lower().strip() | |
| if language not in valid_languages: | |
| raise ValueError( | |
| f"Unsupported language: {language}. " | |
| f"Supported: {', '.join(valid_languages)}" | |
| ) | |
| return language | |