Spaces:
Sleeping
Sleeping
File size: 2,218 Bytes
1fff71f |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// 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) + '...';
}
|