Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 511
Visakh Vijayan Avatar asked Nov 14 '25 17:11

Visakh Vijayan


1 Answers

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;
}
like image 139
Daniel Alejandro Avatar answered Nov 17 '25 07:11

Daniel Alejandro