migrating
This commit is contained in:
95
src/lib/validation.ts
Normal file
95
src/lib/validation.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Form validation utilities
|
||||
*/
|
||||
|
||||
/**
|
||||
* Validate email format
|
||||
*/
|
||||
export function isValidEmail(email: string): boolean {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
return emailRegex.test(email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate password strength
|
||||
*/
|
||||
export function validatePassword(password: string): {
|
||||
isValid: boolean;
|
||||
errors: string[];
|
||||
} {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (password.length < 8) {
|
||||
errors.push("Password must be at least 8 characters long");
|
||||
}
|
||||
|
||||
// Optional: Add more password requirements
|
||||
// if (!/[A-Z]/.test(password)) {
|
||||
// errors.push("Password must contain at least one uppercase letter");
|
||||
// }
|
||||
// if (!/[a-z]/.test(password)) {
|
||||
// errors.push("Password must contain at least one lowercase letter");
|
||||
// }
|
||||
// if (!/[0-9]/.test(password)) {
|
||||
// errors.push("Password must contain at least one number");
|
||||
// }
|
||||
|
||||
return {
|
||||
isValid: errors.length === 0,
|
||||
errors,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two passwords match
|
||||
*/
|
||||
export function passwordsMatch(password: string, confirmation: string): boolean {
|
||||
return password === confirmation && password.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate display name
|
||||
*/
|
||||
export function isValidDisplayName(name: string): boolean {
|
||||
return name.trim().length >= 1 && name.trim().length <= 50;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize user input (basic XSS prevention)
|
||||
*/
|
||||
export function sanitizeInput(input: string): string {
|
||||
return input
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'")
|
||||
.replace(/\//g, "/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is a valid URL
|
||||
*/
|
||||
export function isValidUrl(url: string): boolean {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate file type for uploads
|
||||
*/
|
||||
export function isValidImageType(file: File): boolean {
|
||||
const validTypes = ["image/jpeg", "image/jpg", "image/png", "image/gif", "image/webp"];
|
||||
return validTypes.includes(file.type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate file size (in bytes)
|
||||
*/
|
||||
export function isValidFileSize(file: File, maxSizeMB: number = 5): boolean {
|
||||
const maxBytes = maxSizeMB * 1024 * 1024;
|
||||
return file.size <= maxBytes;
|
||||
}
|
||||
Reference in New Issue
Block a user