You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue does not occur when using TypeScript as builder.
It is related to a previous problem reported in #12680, which was partially fixed in #14181. While the fix resolved the 500 error, it now throws a 400 error when using SWC.
Run the project using SWC as the builder (npm run start:swc).
Open a browser on port 300 or send a GET request without the test query parameter or with an empty value.
Observe the 400 error response.
Switch to TypeScript as the builder (npm run start)
Send the same request, and note that no error is thrown (200) => result:undefined is returned as expected.
Expected behavior
Optional enums should not throw a validation error when the value is undefined, null or not provided in the request payload, regardless of whether SWC or TypeScript is used as the builder.
Therefore in the exemple it must pass undefined in the test parameter
I found a workaround for this issue in an SWC environment which seems to work well on my project.
The goal is to extends the ValidationPipe in order to force the handling of Enums as String.
import{ArgumentMetadata,ValidationPipeasNestValidationPipe}from'@nestjs/common';import{isObject}from'@nestjs/common/utils/shared.utils';exportclassValidationPipeextendsNestValidationPipe{// eslint-disable-next-line @typescript-eslint/no-explicit-anyoverridetransform(value: any,metadata: ArgumentMetadata): any{// Change metatype of enums to String to avoid SWC unwanted behavior: https://github.com/nestjs/nest/issues/14430if(isObject(metadata.metatype)&&typeofmetadata.metatype!=='function'){returnsuper.transform(value,{ ...metadata,metatype: String});}returnsuper.transform(value,metadata);}}
Is there an existing issue for this?
Current behavior
When using SWC as builder, optional enums in NestJS fail validation and throw a 400 error.
If I define a controller like this:
When querying without the
test
query param I get this:This issue does not occur when using TypeScript as builder.
It is related to a previous problem reported in #12680, which was partially fixed in #14181. While the fix resolved the 500 error, it now throws a 400 error when using SWC.
Minimum reproduction code
https://codesandbox.io/p/devbox/goofy-darwin-lmzv37?file=%2Fsrc%2Fapp.controller.ts
Steps to reproduce
npm run start:swc
).npm run start
)result:undefined
is returned as expected.Expected behavior
Optional enums should not throw a validation error when the value is
undefined
,null
or not provided in the request payload, regardless of whether SWC or TypeScript is used as the builder.Therefore in the exemple it must pass
undefined
in thetest
parameterPackage
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
No response
NestJS version
10.4.15
Packages versions
Node.js version
20.12.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: