Performance: DTO class creation
This affects the bundle size and initial load time by adding extra code for data validation and transformation.
import { IsString, IsInt, Min, Max } from 'class-validator'; export class CreateUserDto { @IsString() name: string; @IsInt() @Min(0) @Max(120) age: number; }
import { IsString, IsInt, Min, Max } from 'class-validator'; export class CreateUserDto { @IsString() name: string; @IsInt() @Min(0) @Max(120) age: number; @IsString() bio: string; @IsString() extraLargeField: string; // very large string field not always needed }
| Pattern | Bundle Size Impact | Execution Time | Validation Complexity | Verdict |
|---|---|---|---|---|
| Large DTO with many validations and fields | High (~5kb+) | High (10-15ms blocking) | Complex | [X] Bad |
| Minimal DTO with essential validations only | Low (~1-2kb) | Low (2-5ms blocking) | Simple | [OK] Good |
DTO class in a NestJS application?name in a NestJS DTO class using class-validator decorators?@IsString() decorator from class-validator ensures the property is a string and required by default.{ name: "Alice", age: 25 }?
import { IsString, IsInt, IsOptional } from 'class-validator';
export class UserDto {
@IsString()
name: string;
@IsInt()
@IsOptional()
age?: number;
}email property:
import { IsEmail } from 'class-validator';
export class EmailDto {
@IsEmail
email: string;
}CreateProductDto with these rules: title is a required string price is a required positive number description is an optional string Pass the test and this pattern moves unknown