Performance: class-validator decorators
This affects the server-side validation step before data processing, impacting request handling speed and response time.
import { IsString, IsInt, Min, Max, Length } from 'class-validator'; export class UserDto { @IsString() @Length(5, 50) username: string; @IsInt() @Min(0) @Max(150) age: number; @IsString() @Length(0, 255) bio: string; // Removed description field to reduce validation overhead }
import { IsString, IsInt, Min, Max, Length } from 'class-validator'; export class UserDto { @IsString() @Length(5, 50) username: string; @IsInt() @Min(0) @Max(150) age: number; @IsString() @Length(0, 255) bio: string; @IsString() @Length(0, 1000) description: string; }
| Pattern | Validation CPU Cost | Request Latency Impact | Memory Usage | Verdict |
|---|---|---|---|---|
| Many complex decorators on large DTO | High CPU usage | Adds 10-30ms latency | Moderate | [X] Bad |
| Minimal decorators on essential fields | Low CPU usage | Adds 2-5ms latency | Low | [OK] Good |
@IsEmail() decorator in class-validator when used in a NestJS DTO class?@IsEmail() decorator validates that the decorated property contains a valid email format string.@IsEmail(). Other options describe unrelated behaviors.@IsEmail() validates email format [OK]@Length() decorator to require a string property to be between 5 and 10 characters in NestJS?@Length() decorator takes two numeric arguments: minimum length first, then maximum length.@Length(5, 10). Options A, B, and D use incorrect argument order or syntax.username = 'abc' and age = 17?
class UserDto {
@Length(5, 15)
username: string;
@Min(18)
age: number;
}@Length(5, 15). So username is invalid.@Min(18). So age is invalid.class-validator decorators:
class ProductDto {
@IsInt()
@Min(1)
price: string;
}price is declared as string, but decorators @IsInt() and @Min(1) expect a number type.price is string, validation will fail because @IsInt() requires a number. The type should be number.tags which is an array of strings, each string must be at least 3 characters. Which combination of class-validator decorators correctly enforces this?@IsArray() to ensure tags is an array type.@IsString({ each: true }) and @Length(3, 50, { each: true }) to apply string and length validation to every item in the array.@ArrayMinSize(1) ensures the array is not empty.Pass the test and this pattern moves unknown