Spaces:
Sleeping
Sleeping
File size: 1,679 Bytes
ec37394 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """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
|