Spaces:
Sleeping
Sleeping
| // Input validation utilities | |
| import { SESSION_TITLE_MAX_LENGTH, MESSAGE_MAX_LENGTH } from './constants'; | |
| /** | |
| * Validate session title | |
| * @param title Session title to validate | |
| * @returns Error message if invalid, null if valid | |
| */ | |
| export function validateSessionTitle(title: string): string | null { | |
| const trimmed = title.trim(); | |
| if (trimmed.length === 0) { | |
| return 'Session title cannot be empty'; | |
| } | |
| if (trimmed.length > SESSION_TITLE_MAX_LENGTH) { | |
| return `Session title must be ${SESSION_TITLE_MAX_LENGTH} characters or less`; | |
| } | |
| return null; | |
| } | |
| /** | |
| * Validate message content | |
| * @param content Message content to validate | |
| * @returns Error message if invalid, null if valid | |
| */ | |
| export function validateMessageContent(content: string): string | null { | |
| const trimmed = content.trim(); | |
| if (trimmed.length === 0) { | |
| return 'Message cannot be empty'; | |
| } | |
| if (trimmed.length > MESSAGE_MAX_LENGTH) { | |
| return `Message must be ${MESSAGE_MAX_LENGTH} characters or less`; | |
| } | |
| return null; | |
| } | |
| /** | |
| * Validate email format | |
| * @param email Email address to validate | |
| * @returns True if valid email format, false otherwise | |
| */ | |
| export function isValidEmail(email: string): boolean { | |
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| return emailRegex.test(email); | |
| } | |
| /** | |
| * Validate UUID format | |
| * @param uuid UUID string to validate | |
| * @returns True if valid UUID v4 format, false otherwise | |
| */ | |
| export function isValidUUID(uuid: string): boolean { | |
| const uuidRegex = | |
| /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | |
| return uuidRegex.test(uuid); | |
| } | |
| /** | |
| * Sanitize HTML to prevent XSS | |
| * @param html HTML string to sanitize | |
| * @returns Sanitized HTML with dangerous tags removed | |
| */ | |
| export function sanitizeHtml(html: string): string { | |
| const div = document.createElement('div'); | |
| div.textContent = html; | |
| return div.innerHTML; | |
| } | |
| /** | |
| * Truncate text to specified length with ellipsis | |
| * @param text Text to truncate | |
| * @param maxLength Maximum length | |
| * @returns Truncated text with ellipsis if needed | |
| */ | |
| export function truncate(text: string, maxLength: number): string { | |
| if (text.length <= maxLength) { | |
| return text; | |
| } | |
| return text.substring(0, maxLength - 3) + '...'; | |
| } | |