Fix jwt-token

This commit is contained in:
2025-09-16 08:06:50 +02:00
parent b3ada7ccfe
commit f8fcfbb5be
3 changed files with 42 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
function validateRequest(schema, target = 'body') {
function validateRequest(schema, source = 'body') {
return (req, res, next) => {
const data = req[target];
const { error, value } = schema.validate(data, {
const dataToValidate = req[source];
const { error, value } = schema.validate(dataToValidate, {
abortEarly: false,
stripUnknown: true
});
@@ -13,20 +13,15 @@ function validateRequest(schema, target = 'body') {
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: message,
errors: errorDetails,
details: errorDetails // For backward compatibility
message: 'Validation error',
errors: errorDetails
});
}
// Replace the target data with validated and sanitized data
req[target] = value;
// Replace the validated data source with validated and sanitized data
req[source] = value;
next();
};
}