0
0
NestJSframework~20 mins

Why pipes transform and validate input in NestJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Pipes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Pipes in NestJS
What is the primary reason NestJS pipes transform and validate input data?
ATo delay the processing of requests until the server is less busy
BTo encrypt input data for security before processing
CTo automatically generate database queries from input data
DTo ensure input data meets expected format and types before reaching route handlers
Attempts:
2 left
💡 Hint
Think about how pipes help keep your application safe and predictable.
component_behavior
intermediate
2: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}`;
  }
}
AThe request is rejected with a 400 Bad Request error and validation details
BThe input is accepted but logged as a warning
CThe controller sets age to zero by default and returns success
DThe server crashes due to unhandled validation errors
Attempts:
2 left
💡 Hint
ValidationPipe stops invalid data from reaching the controller.
📝 Syntax
advanced
1: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
  }
}
Areturn value.uppercase();
Breturn value.toUpperCase();
Creturn value.toUpper();
Dreturn value.toUpperCase
Attempts:
2 left
💡 Hint
Check the correct JavaScript string method for uppercase conversion.
🔧 Debug
advanced
2: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?
A400 Bad Request because 'twenty' is not an integer
B500 Internal Server Error due to type mismatch
CRequest succeeds but age is set to 0
DNo error, input is accepted as string
Attempts:
2 left
💡 Hint
ValidationPipe enforces type and format rules strictly.
state_output
expert
2: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; }
AReturns string '-5' without change
BReturns -5 as a number
CThrows BadRequestException with message 'Value must be a positive number'
DReturns 0 because negative values are reset
Attempts:
2 left
💡 Hint
Check the condition that triggers the exception in the transform method.