Skip to content

Commit

Permalink
fix(common): revert to original value (swc builders regression) #14430
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 6, 2025
1 parent 3e62be8 commit de76147
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/common/pipes/validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ export class ValidationPipe implements PipeTransform<any> {
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
Expand All @@ -155,6 +162,7 @@ export class ValidationPipe implements PipeTransform<any> {
// 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 =
Expand Down Expand Up @@ -230,7 +238,7 @@ export class ValidationPipe implements PipeTransform<any> {
) {
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
Expand Down

0 comments on commit de76147

Please # to comment.