-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-validation.js
More file actions
76 lines (75 loc) · 2.21 KB
/
input-validation.js
File metadata and controls
76 lines (75 loc) · 2.21 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
const { body } = require("express-validator");
const { isUserExists } = require("./database");
exports.validate = (method) => {
switch (method) {
case "sendMessage": {
return [
body("username", "Receiver username could not be empty")
.escape()
.trim()
.exists()
.notEmpty()
.custom((value) => {
return isUserExists(value).then((exists) => {
if (!exists) {
return Promise.reject("Receiver does not exists");
}
});
}),
body("content", "Message's text should not be empty")
.escape()
.trim()
.exists()
.notEmpty(),
];
}
case "login": {
return [
body("username")
.escape()
.trim()
.notEmpty()
.withMessage("Username could not be empty")
.isAlphanumeric()
.withMessage("Username should consists only of letters and numbers"),
body("password").notEmpty().withMessage("Password could not be empty"),
];
}
case "register": {
return [
body("username")
.escape()
.trim()
.notEmpty()
.withMessage("Username could not be empty")
.isAlphanumeric()
.withMessage("Username should consists only of letters and numbers")
.custom((value) => {
return isUserExists(value).then((exists) => {
if (exists) {
return Promise.reject("Username already in use");
}
});
}),
body("password")
.isStrongPassword({
minLength: 8,
minLowercase: 1,
minUppercase: 1,
minNumbers: 1,
minSymbols: 0,
returnScore: false,
pointsPerUnique: 0,
pointsPerRepeat: 0,
pointsForContainingLower: 0,
pointsForContainingUpper: 0,
pointsForContainingNumber: 0,
pointsForContainingSymbol: 0,
})
.withMessage(
"Password must be greater than 8 and contain at least one uppercase letter, one lowercase letter, and one number"
),
];
}
}
};