Spaces:
Runtime error
Runtime error
| import re | |
| from typing import List | |
| def validate_email(email: str) -> bool: | |
| """ | |
| Validate email format. | |
| Args: | |
| email: Email string to validate | |
| Returns: | |
| True if valid email format, False otherwise | |
| """ | |
| pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' | |
| return bool(re.match(pattern, email)) | |
| def validate_password(password: str) -> tuple[bool, str]: | |
| """ | |
| Validate password strength. | |
| Args: | |
| password: Password string to validate | |
| Returns: | |
| Tuple of (is_valid, error_message) | |
| """ | |
| if len(password) < 6: | |
| return False, "Password must be at least 6 characters long" | |
| # Bcrypt has a 72-byte limit | |
| if len(password.encode('utf-8')) > 72: | |
| return False, "Password is too long (max 72 bytes)" | |
| return True, "" | |
| def validate_file_type(filename: str, allowed_types: List[str]) -> bool: | |
| """ | |
| Validate file type by extension. | |
| Args: | |
| filename: Name of the file | |
| allowed_types: List of allowed extensions (e.g., ['pdf', 'docx']) | |
| Returns: | |
| True if file type is allowed, False otherwise | |
| """ | |
| if '.' not in filename: | |
| return False | |
| extension = filename.rsplit('.', 1)[1].lower() | |
| return extension in allowed_types | |
| def validate_file_size(file_size: int, max_size_mb: int = 10) -> bool: | |
| """ | |
| Validate file size. | |
| Args: | |
| file_size: File size in bytes | |
| max_size_mb: Maximum allowed size in MB | |
| Returns: | |
| True if file size is within limit, False otherwise | |
| """ | |
| max_size_bytes = max_size_mb * 1024 * 1024 | |
| return file_size <= max_size_bytes | |