""" Input validation utilities """ import re from typing import List def validate_inputs( groq_key: str = None, hf_token: str = None, space_name: str = None, mode: str = "Convert Only" ) -> List[str]: """ Validate all inputs before processing. Returns a list of error messages. """ errors = [] # Groq API Key validation if mode in ["Convert Only", "Convert & Deploy"]: if not groq_key: errors.append("Groq API Key is required for conversion") elif not groq_key.startswith('gsk_'): errors.append("Groq API Key should start with 'gsk_'") elif len(groq_key) < 40: errors.append("Invalid Groq API Key format") # Hugging Face Token validation for deployment if mode == "Deploy" and hf_token: if not hf_token.startswith('hf_'): errors.append("Hugging Face token should start with 'hf_'") elif len(hf_token) < 10: errors.append("Invalid Hugging Face token format") # Space Name validation for deployment if mode == "Deploy" and space_name: if len(space_name) < 3 or len(space_name) > 30: errors.append("Space name must be 3-30 characters") elif not re.match(r'^[a-z0-9-]+$', space_name): errors.append("Space name can only contain lowercase letters, numbers, and hyphens") elif space_name.startswith('-') or space_name.endswith('-'): errors.append("Space name cannot start or end with a hyphen") elif '--' in space_name: errors.append("Space name cannot contain consecutive hyphens") return errors def validate_python_code(code: str) -> List[str]: """ Validate Python code for common issues. Returns a list of warnings (not errors). """ warnings = [] if not code.strip(): warnings.append("Code is empty") return warnings # Check for basic Python syntax markers if 'import' not in code and 'def' not in code and 'class' not in code: warnings.append("Code may not be valid Python (missing imports, functions, or classes)") # Check for potentially dangerous imports dangerous_imports = ['os.system', 'subprocess', 'eval', 'exec', '__import__'] for dangerous in dangerous_imports: if dangerous in code: warnings.append(f"Code contains potentially dangerous operation: {dangerous}") return warnings def validate_file_size(file_size: int, max_size_mb: int = 10) -> bool: """Validate file size.""" max_size_bytes = max_size_mb * 1024 * 1024 return file_size <= max_size_bytes