forked from amand33p/bug-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.ts
More file actions
123 lines (99 loc) · 2.8 KB
/
Copy pathvalidators.ts
File metadata and controls
123 lines (99 loc) · 2.8 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
121
122
123
interface AuthErrors {
username?: string;
password?: string;
}
interface ProjectErrors {
name?: string;
members?: string;
}
interface BugErrors {
title?: string;
description?: string;
priority?: string;
}
export const registerValidator = (username: string, password: string) => {
const errors: AuthErrors = {};
if (
!username ||
username.trim() === '' ||
username.length > 20 ||
username.length < 3
) {
errors.username = 'Username must be in range of 3-20 characters length.';
}
if (!/^[a-zA-Z0-9-_]*$/.test(username)) {
errors.username = 'Username must have alphanumeric characters only.';
}
if (!password || password.length < 6) {
errors.password = 'Password must be atleast 6 characters long.';
}
return {
errors,
valid: Object.keys(errors).length < 1,
};
};
export const loginValidator = (username: string, password: string) => {
const errors: AuthErrors = {};
if (!username || username.trim() === '') {
errors.username = 'Username field must not be empty.';
}
if (!password) {
errors.password = 'Password field must not be empty.';
}
return {
errors,
valid: Object.keys(errors).length < 1,
};
};
export const projectNameError = (name: string) => {
if (!name || name.trim() === '' || name.length > 60) {
return 'Project name length must not be more than 60.';
}
};
export const projectMembersError = (members: string[]) => {
if (!Array.isArray(members)) {
return 'Members field must be an array.';
}
if (members.filter((m, i) => members.indexOf(m) !== i).length !== 0) {
return 'Members field must not have already-added/duplicate IDs.';
}
if (members.some((m) => m.length !== 36)) {
return 'Members array must contain valid UUIDs.';
}
};
export const createProjectValidator = (name: string, members: string[]) => {
const errors: ProjectErrors = {};
const nameError = projectNameError(name);
const membersError = projectMembersError(members);
if (nameError) {
errors.name = nameError;
}
if (membersError) {
errors.members = membersError;
}
return {
errors,
valid: Object.keys(errors).length < 1,
};
};
export const createBugValidator = (
title: string,
description: string,
priority: string
) => {
const errors: BugErrors = {};
const validPriorities = ['low', 'medium', 'high'];
if (!title || title.trim() === '' || title.length > 60 || title.length < 3) {
errors.title = 'Title must be in range of 3-60 characters length.';
}
if (!description || description.trim() === '') {
errors.description = 'Description field must not be empty.';
}
if (!priority || !validPriorities.includes(priority)) {
errors.priority = 'Priority can only be - low, medium or high.';
}
return {
errors,
valid: Object.keys(errors).length < 1,
};
};