From 1622d4889a2d7e07b4d5c61547fbc2db21886aff Mon Sep 17 00:00:00 2001 From: medchedli Date: Mon, 26 May 2025 15:09:01 +0100 Subject: [PATCH] fix: enhance validation error handling with custom exception messages --- api/src/main.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/src/main.ts b/api/src/main.ts index 7b38a68e..a156ccef 100644 --- a/api/src/main.ts +++ b/api/src/main.ts @@ -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(), );