[x] Feature request
Using @Query('foo')
will always make a query parameter required.
The Query decorator should have an optional: boolean
property available, similar to how the Args decorator for gql resolvers has nullable and other options.
e.g.
@Query({ name: 'foo', optional: true })
I'm ok with it always being a string and then using pipes to validate / transform the value downstream but at least nullable
or optional
, whatever, would be nice.
NA
I'm creating a controller and would like to have optional parameters with sensible defaults.
NA
I believe this is the correct location in code:
https://github.com/nestjs/nest/blob/master/packages/common/decorators/http/route-params.decorator.ts#L95
Using @Query('foo') will always make a query parameter required.
I'm not sure what do you mean by this. Query parameters are always optional.
Hmm, I was interpretting it as required because swagger was doing so.
If you're saying that its always optional then maybe there is a different way to describe it for swagger?
@justinmchase I had something similar happen when i used swagger, i had to create Dto objects with the @ApiModelProperty
decorator, and then i used ({ required: false })
as the param for it.
This is done for a post, but i believe you should be able to do something similar, if there's a better way to do it i'd love to see it.
It appears the Swagger UI when it add's parameters they're defaulted as required and (as far as i can tell) the only way to change that is to use the ApiModelProperty
decorator.
This is an example of my CreateUserDto
export class CreateUserDto {
@ApiModelProperty()
name: string;
@ApiModelProperty()
email: string;
@ApiModelProperty({ required: false })
password?: string;
@ApiModelProperty({ required: false })
confirmPassword?: string;
}
@justinmchase
@ApiImplicitQuery({
name: 'foo',
required: false,
type: String
})
myMethod(@Query('foo') foo: string){
}
Both solutions are fine :)
For me, I still keep the rule of DTO, but I use @ApiImplicitQuery like @hwalyonglance above to control @Query is optional or required in swagger params, and it worked. Thanks you @hwalyonglance.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
@justinmchase