Node.js / NestJS

Validation & DTO

class-validator, class-transformer, DTOs, ValidationPipe, custom validators, sanitization

20 interview questionsยท
Junior
1

What is a DTO (Data Transfer Object) in NestJS?

Answer

A DTO is a TypeScript class that defines the structure of data transferred between client and server. It acts as a data contract and enables automatic input validation using decorators like @IsString or @IsNumber. DTOs improve security by filtering unauthorized properties and make code maintenance easier.

2

Which package allows adding validation constraints to DTOs?

Answer

The class-validator package provides validation decorators like @IsString, @IsEmail, @Min, @Max that allow defining validation rules on DTO properties. It works in tandem with ValidationPipe to automatically validate incoming data. This package is widely used in the NestJS ecosystem to ensure data integrity.

3

Which decorator validates that a property is a string?

Answer

The @IsString decorator verifies that a property is of type string. It applies directly to the DTO property and automatically rejects any value that is not a string. This decorator is one of the most commonly used as it covers a very common validation case in REST APIs.

4

Where should ValidationPipe be configured to validate all application routes?

5

Which decorator validates that a field is not empty?

+17 interview questions

Master Node.js / NestJS for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free