adpa-enterprise-framework-automation
Version:
Modular, standards-compliant Node.js/TypeScript automation framework for enterprise requirements, project, and data management. Provides CLI and API for BABOK v3, PMBOK 7th Edition, and DMBOK 2.0 (in progress). Production-ready Express.js API with TypeSpe
68 lines • 2.19 kB
JavaScript
import { createRequire } from 'module';
import { logger } from '../../config/logger.js';
const require = createRequire(import.meta.url);
const Joi = require('joi');
export const validate = (schema) => {
return (req, res, next) => {
const errors = [];
// Validate request body
if (schema.body) {
const { error } = schema.body.validate(req.body);
if (error) {
errors.push({
field: 'body',
message: error.details[0].message,
path: error.details[0].path
});
}
}
// Validate request parameters
if (schema.params) {
const { error } = schema.params.validate(req.params);
if (error) {
errors.push({
field: 'params',
message: error.details[0].message,
path: error.details[0].path
});
}
}
// Validate query parameters
if (schema.query) {
const { error } = schema.query.validate(req.query);
if (error) {
errors.push({
field: 'query',
message: error.details[0].message,
path: error.details[0].path
});
}
}
// Validate headers
if (schema.headers) {
const { error } = schema.headers.validate(req.headers);
if (error) {
errors.push({
field: 'headers',
message: error.details[0].message,
path: error.details[0].path
});
}
}
if (errors.length > 0) {
logger.warn('Validation failed:', {
url: req.url,
method: req.method,
errors
});
return res.status(400).json({
error: true,
message: 'Validation failed',
code: 'VALIDATION_ERROR',
details: errors
});
}
next();
};
};
//# sourceMappingURL=validation.js.map