-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.js
More file actions
58 lines (53 loc) · 1.71 KB
/
validators.js
File metadata and controls
58 lines (53 loc) · 1.71 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
const { body, query, validationResult } = require('express-validator');
const validateRegisterUser = [
body('name').notEmpty().withMessage('Name is required'),
body('email').isEmail().withMessage('Invalid email format'),
body('phone').isMobilePhone().withMessage('Invalid phone number'),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
}
];
const validateAddExpense = [
body('amount').isNumeric().withMessage('Amount must be a number'),
body('purpose').notEmpty().withMessage('Purpose is required'),
body('option').isIn(['equal', 'percentage','exact']).withMessage('Invalid option'),
body('split').isObject().withMessage('Split must be an object'),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
}
];
const validateGetUser = [
query('email').optional().isEmail().withMessage('Invalid email format'),
query('phone').optional().isMobilePhone().withMessage('Invalid phone number'),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
}
];
const validateGetExpenseById = [
query('expId').isNumeric().withMessage('Expense ID must be a number'),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
}
];
module.exports = {
validateRegisterUser,
validateAddExpense,
validateGetUser,
validateGetExpenseById
};