-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.validator.js
61 lines (56 loc) · 1.31 KB
/
signup.validator.js
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
const Joi = require('helpers/joi.adapter');
const userService = require('resources/user/user.service');
const schema = {
firstName: Joi.string()
.trim()
.options({
language: {
any: { empty: '!!Your first name must be longer than 1 letter' },
},
}),
lastName: Joi.string()
.trim()
.options({
language: {
any: { empty: '!!Your last name must be longer than 1 letter' },
},
}),
email: Joi.string()
.email({ minDomainSegments: 2 })
.trim()
.lowercase()
.options({
language: {
string: { email: '!!Please enter a valid email address' },
any: { empty: '!!Email is required' },
},
}),
password: Joi.string()
.trim()
.min(6)
.max(40)
.options({
language: {
string: {
min: '!!Password is too short',
max: '!!Password is too long',
},
any: { empty: '!!Password is required' },
},
}),
};
const validateFunc = async (data) => {
const userExists = await userService.exists({ email: data.email });
const errors = [];
if (userExists) {
errors.push({ email: 'User with this email is already registered.' });
return {
errors,
};
}
return {
value: data,
errors,
};
};
module.exports = [Joi.validate(schema), validateFunc];