Challenge - 5 Problems
NestJS Pipes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of Pipes in NestJS
What is the primary reason NestJS pipes transform and validate input data?
Attempts:
2 left
💡 Hint
Think about how pipes help keep your application safe and predictable.
✗ Incorrect
Pipes in NestJS are used to transform and validate input data so that the data passed to route handlers is clean and correctly typed. This prevents errors and security issues.
❓ component_behavior
intermediate2:00remaining
Effect of Validation Pipe on Controller Input
Given a NestJS controller using a ValidationPipe, what happens if the input data does not match the DTO validation rules?
NestJS
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common'; import { IsInt, Min } from 'class-validator'; class AgeDto { @IsInt() @Min(0) age: number; } @Controller('users') export class UserController { @Post('age') @UsePipes(new ValidationPipe()) setAge(@Body() ageDto: AgeDto) { return `Age set to ${ageDto.age}`; } }
Attempts:
2 left
💡 Hint
ValidationPipe stops invalid data from reaching the controller.
✗ Incorrect
ValidationPipe automatically checks input against DTO rules. If validation fails, it throws an exception that results in a 400 error response with details.
📝 Syntax
advanced1:30remaining
Correct Use of Transform in a Custom Pipe
Which option correctly implements a NestJS pipe that transforms a string input to uppercase before passing it to the handler?
NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class UppercasePipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { // Transform logic here } }
Attempts:
2 left
💡 Hint
Check the correct JavaScript string method for uppercase conversion.
✗ Incorrect
The correct method to convert a string to uppercase in JavaScript is toUpperCase(). Other options are invalid or undefined methods.
🔧 Debug
advanced2:00remaining
Identifying Pipe Validation Error Cause
A NestJS app uses a ValidationPipe globally. A POST request with body {"age": "twenty"} is sent to a route expecting a DTO with @IsInt() age property. What error will occur and why?
Attempts:
2 left
💡 Hint
ValidationPipe enforces type and format rules strictly.
✗ Incorrect
The ValidationPipe checks that 'age' is an integer. The string 'twenty' fails this check, causing a 400 error response.
❓ state_output
expert2:30remaining
Output of a Pipe Transforming and Validating Input
Consider this NestJS pipe that transforms input to a number and validates it is positive. What is the output when input is '-5'?
NestJS
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common'; @Injectable() export class PositiveNumberPipe implements PipeTransform { transform(value: any) { const val = Number(value); if (isNaN(val) || val <= 0) { throw new BadRequestException('Value must be a positive number'); } return val; } } // Usage in controller: // @UsePipes(new PositiveNumberPipe()) // someMethod(@Body('amount') amount: number) { return amount; }
Attempts:
2 left
💡 Hint
Check the condition that triggers the exception in the transform method.
✗ Incorrect
The pipe converts input to number and checks if it is positive. Since -5 is not positive, it throws a BadRequestException.