besper-frontend-site-dev-0935
Version:
Professional B-esper Frontend Site - Site-wide integration toolkit for full website bot deployment
70 lines (59 loc) • 1.82 kB
text/typescript
// Validation utility functions
export function validateEmail(email: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
export function validateUrl(url: string): boolean {
try {
new URL(url);
return true;
} catch {
return false;
}
}
export function validateBotId(botId: string): boolean {
return (
typeof botId === 'string' &&
botId.length > 0 &&
/^[a-zA-Z0-9-_]+$/.test(botId)
);
}
export function validateCredentials(credentials: any): boolean {
return (
credentials &&
typeof credentials.botId === 'string' &&
typeof credentials.managementId === 'string' &&
typeof credentials.managementSecret === 'string' &&
credentials.botId.length > 0 &&
credentials.managementId.length > 0 &&
credentials.managementSecret.length > 0
);
}
export function validateFileType(file: File, allowedTypes: string[]): boolean {
const fileExtension = '.' + file.name.split('.').pop()?.toLowerCase();
return allowedTypes.includes(fileExtension);
}
export function validateFileSize(file: File, maxSize: number): boolean {
return file.size <= maxSize;
}
export function validateHexColor(color: string): boolean {
const hexRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
return hexRegex.test(color);
}
export function sanitizeHtml(html: string): string {
const div = document.createElement('div');
div.textContent = html;
return div.innerHTML;
}
export function validateEnvironment(
env: string
): env is 'dev' | 'int' | 'prod' {
return ['dev', 'int', 'prod'].includes(env);
}
export function validatePosition(
position: string
): position is 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left' {
return ['bottom-right', 'bottom-left', 'top-right', 'top-left'].includes(
position
);
}