validate nested objects using class-validator in nest.js controller

后端 未结 1 1119
天命终不由人
天命终不由人 2021-01-04 05:27

I want to validate body payload using class-validator in a nest.js controller. My currency.dto.ts file is like this:

1条回答
  •  攒了一身酷
    2021-01-04 05:52

    Try specifying the nested type with @Type:

    import { Type } from 'class-transformer';
    
    export class CurrencyDTO {
      @ValidateNested({ each: true })
      @Type(() => Data)
      data: Data[];
    }
    

    For a nested type to be validated, it needs to be an instance of a class not just a plain data object. With the @Type decorator you tell class-transformer to instantiate a class for the given property when plainToClass is called in your VaildationPipe.

    If you are using the built-in ValidationPipe make sure you have set the option transform: true.

    0 讨论(0)
提交回复
热议问题