-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional Enums with SWC throw validation errors (400) #14430
Labels
needs triage
This issue has not been looked into
Comments
12 tasks
I found a workaround for this issue in an SWC environment which seems to work well on my project. import { ArgumentMetadata, ValidationPipe as NestValidationPipe } from '@nestjs/common';
import { isObject } from '@nestjs/common/utils/shared.utils';
export class ValidationPipe extends NestValidationPipe {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
override transform(value: any, metadata: ArgumentMetadata): any {
// Change metatype of enums to String to avoid SWC unwanted behavior: https://github.com/nestjs/nest/issues/14430
if (isObject(metadata.metatype) && typeof metadata.metatype !== 'function') {
return super.transform(value, { ...metadata, metatype: String });
}
return super.transform(value, metadata);
}
} |
kamilmysliwiec
added a commit
that referenced
this issue
Feb 6, 2025
12 tasks
Thanks @kamilmysliwiec, it works 🙏 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: