Nest: Please Add `optional: boolean` to @Query route param decorator

Created on 10 May 2019  路  7Comments  路  Source: nestjs/nest

I'm submitting a...

[x] Feature request

Current behavior

Using @Query('foo') will always make a query parameter required.

Expected behavior

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.

Minimal reproduction of the problem with instructions

NA

What is the motivation / use case for changing the behavior?

I'm creating a controller and would like to have optional parameters with sensible defaults.

Environment

NA

Extra

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

Most helpful comment

@justinmchase

@ApiImplicitQuery({
    name: 'foo',
    required: false,
    type: String
})
myMethod(@Query('foo') foo: string){

}

All 7 comments

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.

Screen Shot 2019-05-10 at 8 37 16 AM

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FranciZ picture FranciZ  路  3Comments

mishelashala picture mishelashala  路  3Comments

tronginc picture tronginc  路  3Comments

menme95 picture menme95  路  3Comments

cojack picture cojack  路  3Comments