-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Query use ParseEnumPipe throw error when query property is optional #12680
Comments
Maybe the expression above is not good. The query string |
Would you like to create a PR for this issue? If not, please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project). |
create a custom validation to ensure the parameter exists before trying to parse it. Here's an example: // At this point, you know that 'type' exists and is a valid LoginType enum value. This way, you avoid the ParseEnumPipe from directly trying to parse an undefined value. |
I didn't manage to reproduce that why reproductions are required |
@recallwei please double-check if this isn't due to the line |
Sorry for wasting everyone's time. My origin repo at https://github.com/bit-ocean-studio/dolphin-admin-nest I tried to make another reproduction at https://codesandbox.io/p/github/bit-ocean-studio/dolphin-admin-nest/main?file=%2Fsrc%2Fmodules%2Fauth%2Fauth.controller.ts%3A32%2C48&workspaceId=9227e773-346e-4830-aeb4-a342dcced73b API endpoint is |
I ran into a similar problem today. It happens only when I use swc. If I disable it, everything works correctly. Method example: export enum City {
KRD = 'krd',
SOCHI = 'sochi',
}
...
@Get(':query')
@ApiOperation({
tags: ['search'],
summary: 'Search for services/doctors/price by name',
})
@ApiParam({
name: 'query',
description: 'Query text',
})
@ApiQuery({
name: 'city',
required: false,
enum: City,
description:
'City identifier. May be omitted, default value is `krd`.',
})
@ApiOkResponse({
description: 'Returns an object with a successful or empty search result',
})
getSearch(
@Param('query') query: string,
@Query('city') city: City,
) {
return this.searchService.search(query, city);
} I also have ValidationPipe globally installed app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
}),
); If I set the variable type as string then everything works, if I set enum then it doesn't work with swc. When setting ParseEnumPipe the situation is similar. @Query('city', new ParseEnumPipe(City, { optional: true })) city: City, |
@JoCat please share a minimum reproduction repository. |
I remember that I also used swc. |
https://github.com/JoCat/nest-swc-error-reproduction |
I am facing the exact same issue and disabling SWC indeed helps ! Should we open an issue on swc repo, so a fix can be implemented ? |
@micalevisk The repo was already shared above. If you use Here was the bug report I made on the discord server: I have an endpoint with an optional query param called enum MapType {
regionMap = 'regionMap',
regionMapPolygon = 'regionMapPolygon',
}
all(
@Query('mapType', new ParseEnumPipe(MapType)) mapType: MapType,
): Promise<MapResponseDto> { When there is a
all(
@Query('mapType', new ParseEnumPipe(MapType)) mapType: string,
): Promise<MapResponseDto> {
all(
@Query('mapType', new ParseEnumPipe(MapType)) mapType: keyof typeof MapType,
): Promise<MapResponseDto> {
|
Let's track this here #14181 |
Is there an existing issue for this?
Current behavior
I want to parse a query property which is an enum type using ParseEnumPipe.
But if query
type
is undefined, nest will throw an error below:To avoid this error, I must change
type: LoginType
totype: string
.Is this expected behaviour?
Minimum reproduction code
The above code
Steps to reproduce
Parse an enum query string using ParseEnumPipe.
Expected behavior
type: LoginType
should work, expect this query string to be parsed as an enum type.Package
@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.2.7
Packages versions
Node.js version
20 lts
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: