Performance: Built-in pipes (ParseIntPipe, ParseBoolPipe)
These pipes affect request processing speed and input validation before controller logic runs.
async getUser(@Query('id', ParseIntPipe) id: number) { return this.userService.findById(id); }
async getUser(@Query('id') id: string) { const userId = parseInt(id); if (isNaN(userId)) throw new BadRequestException('Invalid id'); return this.userService.findById(userId); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual parsing in controller | N/A (server-side) | N/A | N/A | [X] Bad |
| Using ParseIntPipe and ParseBoolPipe | N/A (server-side) | N/A | N/A | [OK] Good |
ParseIntPipe and ParseBoolPipe?ParseIntPipe in a NestJS controller method parameter?number.@Get(':active')
getStatus(@Param('active', ParseBoolPipe) active: boolean) {
return active ? 'Active' : 'Inactive';
}
What will be the response if the client requests /true?@Get(':id')
getUser(@Param('id', ParseIntPipe) id: number) {
return `User ID is ${id}`;
}
If a client sends a request to /abc, what will happen?enabled which should be a boolean. Which of the following is the best way to ensure enabled is automatically converted and validated as a boolean?Pass the test and this pattern moves unknown