Consider the following NestJS DTOs with validation decorators. What will happen if the nested object is missing a required field?
import { IsString, ValidateNested } from 'class-validator'; import { Type } from 'class-transformer'; class AddressDto { @IsString() street: string; @IsString() city: string; } class UserDto { @IsString() name: string; @ValidateNested() @Type(() => AddressDto) address: AddressDto; } // Input object: const input = { name: 'Alice', address: { city: 'Wonderland' } }; // What happens when validating input against UserDto?
Think about how @ValidateNested() and @Type() work together to validate nested objects.
The @ValidateNested() decorator tells NestJS to validate the nested object. The @Type(() => AddressDto) decorator ensures the nested plain object is transformed into an instance of AddressDto. Since street is required and missing, validation fails.
Given two DTO classes, which code snippet correctly applies nested validation so that the nested object is validated?
class ChildDto { @IsString() childProp: string; } class ParentDto { @IsString() parentProp: string; // What decorators should be here for nested validation? nested: ChildDto; }
Remember that @ValidateNested() triggers validation of nested objects and @Type() transforms plain objects to class instances.
Option C correctly uses @ValidateNested() to validate the nested object and @Type(() => ChildDto) to transform the plain object into a ChildDto instance. Other options either misuse decorators or miss transformation.
Given this DTO, why does validation not run on the nested array items?
class ItemDto { @IsString() name: string; } class ContainerDto { @ValidateNested({ each: true }) @Type(() => ItemDto) items: ItemDto[]; } // Input: const input = { items: [{ name: 'Item1' }, { name: 123 }] }; // Validation is run on ContainerDto with input. Why does the invalid second item not cause an error?
Think about how class-transformer transforms arrays of objects.
By default, class-transformer does not transform array items unless the validation pipe is configured with transform: true. Without transformation, nested validation does not run on plain objects inside arrays.
Given these nested DTOs, what validation error message will be produced if the deepest nested field is missing?
class Level3Dto { @IsString() detail: string; } class Level2Dto { @ValidateNested() @Type(() => Level3Dto) level3: Level3Dto; } class Level1Dto { @ValidateNested() @Type(() => Level2Dto) level2: Level2Dto; } const input = { level2: { level3: {} } }; // After validating input against Level1Dto, what error appears?
Consider how validation errors are nested and reported for nested DTOs.
Validation errors for nested DTOs are nested inside the children array of the parent property. Since detail is missing and required, the error appears nested inside level2 and level3.
In NestJS, why must you use the @Type(() => NestedDto) decorator alongside @ValidateNested() for nested DTO validation?
Think about how class-transformer and class-validator work together in NestJS.
The @Type() decorator tells class-transformer how to convert plain nested objects into instances of the nested DTO class. Without this transformation, class-validator cannot apply validation decorators on nested properties because they are plain objects, not class instances.