-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathuser-validation.ts
More file actions
65 lines (54 loc) · 1.5 KB
/
user-validation.ts
File metadata and controls
65 lines (54 loc) · 1.5 KB
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
export interface ValidationResult {
isValid: boolean;
errors: string[];
}
/**
* Validates user email and password according to security requirements
* @param email - Email address to validate
* @param password - Password to validate
* @returns ValidationResult with isValid flag and error messages
*/
export function validateUser(email: string, password: string): ValidationResult {
const errors: string[] = [];
// Email validation
if (!validateEmail(email)) {
errors.push('Invalid email format');
}
// Password validation
const passwordErrors = validatePassword(password);
errors.push(...passwordErrors);
return {
isValid: errors.length === 0,
errors,
};
}
/**
* Validates email format using a basic regex pattern
*/
function validateEmail(email: string): boolean {
if (!email || email.trim() === '') {
return false;
}
// Basic email regex: localpart@domain
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
/**
* Validates password strength requirements
*/
function validatePassword(password: string): string[] {
const errors: string[] = [];
// Minimum length check
if (password.length < 8) {
errors.push('Password must be at least 8 characters');
}
// Uppercase letter check
if (!/[A-Z]/.test(password)) {
errors.push('Password must contain at least one uppercase letter');
}
// Number check
if (!/\d/.test(password)) {
errors.push('Password must contain at least one number');
}
return errors;
}