Visakh Vijayan
Visakh Vijayan

Reputation: 718

How to set default values for DTO in nest js?

I want to set default values for a node in a DTO. So if the value for that node is not passed, a default value will be used. Although this is working, I want the node should be present, the values is optional.

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

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

    @IsNotEmpty()
    length: number = 10;

    @IsNotEmpty()
    lastId: string = "0"
}

This doesn't serve my purpose. I want the url to be searched like so http://base-url/folder?search=value

If the value is not passed it should not throw an error.

But if the param search is not there it should throw an error.

Upvotes: 4

Views: 9957

Answers (1)

Daniel Alejandro
Daniel Alejandro

Reputation: 695

If you want to set a default value go to entity and set in the field for example in mongo db

export class DumpDoc extends Document {
  @Prop()
  title: string;
  
  @Prop({ default: new Date() }) //set as default
  createdAt: string;
}

Upvotes: 3

Related Questions