I want to validate body payload using class-validator in a nest.js controller. My currency.dto.ts
file is like this:
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
.