-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathvalidator.ts
More file actions
120 lines (95 loc) · 2.42 KB
/
Copy pathvalidator.ts
File metadata and controls
120 lines (95 loc) · 2.42 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Validation utilities demonstrating branch coverage
*/
export interface ValidationResult {
valid: boolean;
errors: string[];
}
/**
* Validate an email address
* Multiple branches for different validation rules
*/
export function validateEmail(email: string): ValidationResult {
const errors: string[] = [];
if (!email) {
errors.push('Email is required');
return { valid: false, errors };
}
if (!email.includes('@')) {
errors.push('Email must contain @');
}
if (!email.includes('.')) {
errors.push('Email must contain a domain');
}
const [local, domain] = email.split('@');
if (local && local.length > 64) {
errors.push('Local part too long');
}
if (domain && domain.length > 255) {
errors.push('Domain too long');
}
return {
valid: errors.length === 0,
errors,
};
}
/**
* Validate a password with multiple criteria
*/
export function validatePassword(password: string): ValidationResult {
const errors: string[] = [];
if (!password) {
errors.push('Password is required');
return { valid: false, errors };
}
if (password.length < 8) {
errors.push('Password must be at least 8 characters');
}
if (password.length > 128) {
errors.push('Password must be at most 128 characters');
}
if (!/[A-Z]/.test(password)) {
errors.push('Password must contain an uppercase letter');
}
if (!/[a-z]/.test(password)) {
errors.push('Password must contain a lowercase letter');
}
if (!/[0-9]/.test(password)) {
errors.push('Password must contain a number');
}
if (!/[!@#$%^&*]/.test(password)) {
errors.push('Password must contain a special character');
}
return {
valid: errors.length === 0,
errors,
};
}
/**
* This function has branches that are intentionally not fully tested
* to demonstrate partial branch coverage
*/
export function validateAge(age: unknown): ValidationResult {
const errors: string[] = [];
if (age === null || age === undefined) {
errors.push('Age is required');
return { valid: false, errors };
}
if (typeof age !== 'number') {
errors.push('Age must be a number');
return { valid: false, errors };
}
if (!Number.isInteger(age)) {
errors.push('Age must be an integer');
}
if (age < 0) {
errors.push('Age cannot be negative');
}
if (age > 150) {
errors.push('Age seems unrealistic');
}
return {
valid: errors.length === 0,
errors,
};
}