From de761479a69a3be58dc2048f7df84c6260d65bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 6 Feb 2025 14:29:44 +0100 Subject: [PATCH] fix(common): revert to original value (swc builders regression) #14430 --- packages/common/pipes/validation.pipe.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/common/pipes/validation.pipe.ts b/packages/common/pipes/validation.pipe.ts index ecc7dbdd40c..3b5ac08f9f2 100644 --- a/packages/common/pipes/validation.pipe.ts +++ b/packages/common/pipes/validation.pipe.ts @@ -143,6 +143,13 @@ export class ValidationPipe implements PipeTransform { if (errors.length > 0) { throw await this.exceptionFactory(errors); } + + if (originalValue === undefined && originalEntity === '') { + // Since SWC requires empty string for validation (to avoid an error), + // a fallback is needed to revert to the original value (when undefined). + // @see https://github.com/nestjs/nest/issues/14430 + return originalValue; + } if (isPrimitive) { // if the value is a primitive value and the validation process has been successfully completed // we have to revert the original value passed through the pipe @@ -155,6 +162,7 @@ export class ValidationPipe implements PipeTransform { // if the value was originally undefined or null, revert it back return originalValue; } + // we check if the number of keys of the "validatorOptions" is higher than 1 (instead of 0) // because the "forbidUnknownValues" now fallbacks to "false" (in case it wasn't explicitly specified) const shouldTransformToPlain = @@ -230,7 +238,7 @@ export class ValidationPipe implements PipeTransform { ) { return {} as object; } - // Builder like SWC require empty string to be returned instead of an empty object + // SWC requires empty string to be returned instead of an empty object // when the value is nil and the metatype is not a class instance, but a plain object (enum, for example). // Otherwise, the error will be thrown. // @see https://github.com/nestjs/nest/issues/12680