Fix jwt-token

This commit is contained in:
2025-09-16 06:17:18 +02:00
parent 32339de9eb
commit 63635a9adf
2 changed files with 22 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
function validateRequest(schema) {
function validateRequest(schema, target = 'body') {
return (req, res, next) => {
const { error, value } = schema.validate(req.body, {
const data = req[target];
const { error, value } = schema.validate(data, {
abortEarly: false,
stripUnknown: true
});
@@ -12,15 +13,20 @@ function validateRequest(schema) {
value: detail.context.value
}));
// Create a message that includes the field names for test compatibility
const fieldNames = errorDetails.map(err => err.field).join(', ');
const message = `Validation error: ${fieldNames}`;
return res.status(400).json({
success: false,
message: 'Validation error',
errors: errorDetails
message: message,
errors: errorDetails,
details: errorDetails // For backward compatibility
});
}
// Replace req.body with validated and sanitized data
req.body = value;
// Replace the target data with validated and sanitized data
req[target] = value;
next();
};
}