-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (27 loc) · 878 Bytes
/
index.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
const { general } = require('./validators/general.validator');
const { validateString } = require('./validators/string.validator');
const { validateNumber } = require('./validators/number.validator');
module.exports.validate = (options) => (req, res, next) => {
const errors = [];
for (const it of options) {
try {
const { validations, source, type } = it;
const dataSource = req[source]; // source == body || query || params
errors.push(...general(it, dataSource, req));
switch (type) {
case 'string':
errors.push(...validateString(it, dataSource));
break;
case 'number':
errors.push(...validateNumber(it, dataSource));
break;
default:
break;
}
} catch (error) {
console.error(error);
}
}
if (errors.length) req.errors = errors;
next();
};