forked from WildCodeSchool/Express-Quests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateUser.js
More file actions
26 lines (22 loc) · 847 Bytes
/
validateUser.js
File metadata and controls
26 lines (22 loc) · 847 Bytes
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
const validateUser = (req, res, next) => {
const { firstname, lastname, email, language, city } = req.body;
const errors = [];
const emailRegex = /[a-z0-9._]+@[a-z0-9-]+\.[a-z]{2,3}/;
for (const [key, value] of Object.entries({ firstname, lastname, email, language, city })) {
if (value == null) {
errors.push({ field: key, message: "This field is required" })
} else if (value.length >= 255) { // pour ne pas mettre de restriction sur email afin de test
errors.push({ field: key, message: "Should contain less than 255 characters" });
} else if (key === "email" && !emailRegex.test(email)) {
errors.push({ field: key, message: "Invalid email" })
}
}
if (errors.length) {
res.status(422).json({ validationErrors: errors, });
} else {
next();
}
};
module.exports = {
validateUser,
};