-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexpress-validator.js
65 lines (65 loc) · 1.8 KB
/
express-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
62
63
64
65
const { body } = require('express-validator')
module.exports = {
newTodo: [
// validate name field
body('name')
.isLength({ min: 1, max: 10 })
.withMessage('Name is required, max 10 letters'),
// check status field
body('status')
.custom(value => {
if (value !== 'done' && value !== 'notDone') {
throw new Error('Please choose a task status')
}
// if status passed validation
return true
}),
// check detail field
body('detail')
.isLength({ max: 60 })
.withMessage('Detail length must be less than 60 words')
],
editTodo: [
// validate name field
body('name')
.isLength({ min: 1, max: 10 })
.withMessage('Name is required, max 10 letters'),
// check status field
body('status')
.custom(value => {
if (value !== 'done' && value !== 'notDone') {
throw new Error('Please choose a task status')
}
// if status passed validation
return true
}),
// check detail field
body('detail')
.isLength({ max: 60 })
.withMessage('Detail length must be less than 60 words')
],
registerUser: [
body('name')
.isLength({ min: 1, max: 10 })
.withMessage('Name is required, max 10 letters'),
body('email')
.isEmail()
.withMessage('Please provide a valid email'),
body('password')
.custom(value => {
const regex = /^\S{8,12}$/
const result = value.match(regex)
if (!result) {
throw new Error('Password length must be between 8-12')
}
return true
}),
body('password2')
.custom((value, { req }) => {
if (value !== req.body.password) {
throw new Error('Password does not matched')
}
return true
})
]
}