fix: enhance validation error handling with custom exception messages

This commit is contained in:
medchedli 2025-05-26 15:09:01 +01:00
parent 933daaa221
commit 1622d4889a

View File

@ -6,7 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { ValidationPipe } from '@nestjs/common';
import { BadRequestException, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import bodyParser from 'body-parser';
import session from 'express-session';
@ -71,6 +71,16 @@ async function bootstrap() {
whitelist: true,
transform: true,
// forbidNonWhitelisted: true,
exceptionFactory: (errors) => {
const messages = errors.map((error) => {
const constraints = error.constraints;
if (constraints) {
return Object.values(constraints)[0];
}
return `${error.property} has invalid value`;
});
return new BadRequestException(messages.join(', '));
},
}),
new ObjectIdPipe(),
);