73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
|
|
// frontend/src/api/utils/validation.ts
|
||
|
|
/**
|
||
|
|
* Request Validation Utilities
|
||
|
|
*/
|
||
|
|
|
||
|
|
export class RequestValidator {
|
||
|
|
static validateEmail(email: string): boolean {
|
||
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||
|
|
return emailRegex.test(email);
|
||
|
|
}
|
||
|
|
|
||
|
|
static validatePassword(password: string): {
|
||
|
|
valid: boolean;
|
||
|
|
errors: string[];
|
||
|
|
} {
|
||
|
|
const errors: string[] = [];
|
||
|
|
|
||
|
|
if (password.length < 8) {
|
||
|
|
errors.push('Password must be at least 8 characters long');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!/(?=.*[a-z])/.test(password)) {
|
||
|
|
errors.push('Password must contain at least one lowercase letter');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!/(?=.*[A-Z])/.test(password)) {
|
||
|
|
errors.push('Password must contain at least one uppercase letter');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!/(?=.*\d)/.test(password)) {
|
||
|
|
errors.push('Password must contain at least one number');
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
valid: errors.length === 0,
|
||
|
|
errors,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
static validateFile(file: File, allowedTypes: string[], maxSize: number): {
|
||
|
|
valid: boolean;
|
||
|
|
errors: string[];
|
||
|
|
} {
|
||
|
|
const errors: string[] = [];
|
||
|
|
|
||
|
|
if (!allowedTypes.includes(file.type)) {
|
||
|
|
errors.push(`File type ${file.type} is not allowed`);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (file.size > maxSize) {
|
||
|
|
errors.push(`File size exceeds maximum of ${maxSize} bytes`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
valid: errors.length === 0,
|
||
|
|
errors,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
static validatePhoneNumber(phone: string): boolean {
|
||
|
|
// Basic international phone number validation
|
||
|
|
const phoneRegex = /^\+?[1-9]\d{1,14}$/;
|
||
|
|
return phoneRegex.test(phone.replace(/\s/g, ''));
|
||
|
|
}
|
||
|
|
|
||
|
|
static validateRequired(value: any, fieldName: string): string | null {
|
||
|
|
if (value === null || value === undefined || value === '') {
|
||
|
|
return `${fieldName} is required`;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|