question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
hwalyonglancecommented, May 11, 2019

@justinmchase

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

}
1reaction
kamilmysliwieccommented, May 12, 2019

Both solutions are fine 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to use optional url parameters with NestjS - Stack Overflow
Opinion: You can use query params if the params are optional. It is never a good idea to create optional param routes (disagreements...
Read more >
Optional Parameters in ASP.NET Core Web API Routing
We can make a route parameter optional by adding “?” to it. The only gimmick is while declaring a route parameter as optional,...
Read more >
Controllers | NestJS - A progressive Node.js framework
The route path for a handler is determined by concatenating the (optional) prefix declared for the controller, and any path specified in the...
Read more >
Decorators | tsoa - GitHub Pages
Use on @Query parameters to exclude query params from the generated OpenAPI Specification document. The parameter must either allow undefined or have a...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
Subtypes are accepted as arguments. New features are frequently added to the typing module. The typing_extensions package provides backports of these new ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found