DEV Community

Cover image for Default values in nestjs
Visakh Vijayan
Visakh Vijayan

Posted on • Edited on

3 3

Default values in nestjs

While passing query params in nestjs if you have come across a situation where you want the node to exist without an explicit value for it, then here is how I wasted a lot of time behind it.

import { IsNotEmpty, IsString } from "class-validator";

export class IssueSearch
{
    @IsString()
    search: string;

    @IsNotEmpty()
    length: number = 10;

    @IsNotEmpty()
    lastId: string = "0"
}
Enter fullscreen mode Exit fullscreen mode

Above is my DTO which is applied on the validation params of a controller method as below.

@Get("issues/all")
@UsePipes(new ValidationPipe({transform: true}))
async fetchAllIssues(@Query() search: IssueSearch)
{
    ...
Enter fullscreen mode Exit fullscreen mode

Hence if you access this method like
http://baseurl/controller/issues/all it will throw up an error saying it didn't find the search node.

However, if you access it like this -
http://baseurl/controller/issues/all?search=

Then it's fooled.

Let me know if there is an easier way to do this.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
orimdominic profile image
Orim Dominic Adah

Why not use class-validator

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series