NestJS · Middle · Question 21 of 75

How do you validate nested objects in a DTO?

export class CreateOrderDto {
  @ValidateNested({ each: true })
  @Type(() => OrderItemDto)
  items: OrderItemDto[];
}

Explanation

Without @Type, the nested values stay plain objects and their decorators are ignored.

Further reading

Problem with a question

NestJS · Middle · question 21

Test yourself

Take the NestJS · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz