-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
49 lines (42 loc) · 1.26 KB
/
validation.js
File metadata and controls
49 lines (42 loc) · 1.26 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
/**
* Input Validation Middleware
* Validates request data against defined schemas
*/
const { validationResult } = require('express-validator');
const logger = require('../utils/logger');
const { errorResponse } = require('../utils/response');
/**
* Validate request against provided validation rules
* @param {Array} validationRules - Array of express-validator validation rules
* @returns {Function} Express middleware function
*/
const validate = (validationRules) => {
return async (req, res, next) => {
try {
// Apply validation rules
for (const validation of validationRules) {
await validation.run(req);
}
// Check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
logger.warn('Validation error:', {
path: req.path,
method: req.method,
errors: errors.array()
});
return errorResponse(res, {
message: 'Validation error',
errors: errors.array()
}, 400);
}
next();
} catch (error) {
logger.error('Error in validation middleware:', error);
return errorResponse(res, 'Server error during validation', 500);
}
};
};
module.exports = {
validate
};