nestjs - dynamic validation typeorm

642 Views Asked by At

I want to create a form builder and I have dynamics fields and validation. for example, sometimes I have fields like this

@IsNotEmpty()
 name: string

@IsNotEmpty()
@IsEmail()
email: string

@IsNotEmpty()
@Length(11)
mobile: string

and sometimes I have fields like this:

@IsNotEmpty()
name: string
@IsNumber()
age: number

How can I handle it?

1

There are 1 best solutions below

0
Youba On

I didn't understand your question but I think you need to add IsOptional() on stuff that you won't check unless they have value, on the other hand you can use ValidateIf() on fields depends on others,

      @ValidateIf((object) => object.name!= undefined)
      @IsNumber()
      age: number