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[];
}- ✓Correct answer: Use
@ValidateNested()together with@Type(() => ItemDto)soclass-transformercreates instances to validate - 2Use
@IsObject()only - 3Nested objects are validated automatically
Explanation
Without @Type, the nested values stay plain objects and their decorators are ignored.