Challenge - 5 Problems
NestJS Pipes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this custom pipe transformation?
Consider this NestJS custom pipe that transforms input to uppercase. What will be the output when the pipe is applied to the string 'hello'?
NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class UppercasePipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { return typeof value === 'string' ? value.toUpperCase() : value; } } // Usage example: // const result = new UppercasePipe().transform('hello', { type: 'body', metatype: String, data: '' });
Attempts:
2 left
💡 Hint
Think about what the transform method does to string inputs.
✗ Incorrect
The pipe checks if the input is a string and converts it to uppercase. So 'hello' becomes 'HELLO'.
📝 Syntax
intermediate2:00remaining
Which option correctly implements a custom pipe that validates a positive integer?
You want to create a custom pipe that throws an error if the input is not a positive integer. Which code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Check for integer parsing and proper error throwing.
✗ Incorrect
Option D correctly parses the input as an integer, checks if it's positive, and throws the right exception. Others either miss integer check or throw generic errors.
🔧 Debug
advanced2:00remaining
Why does this custom pipe cause a runtime error?
This pipe is intended to trim string inputs. Why does it cause a runtime error when a number is passed?
NestJS
import { PipeTransform, Injectable } from '@nestjs/common'; @Injectable() export class TrimPipe implements PipeTransform { transform(value: any) { return value.trim(); } }
Attempts:
2 left
💡 Hint
Think about what happens when you call trim on a non-string.
✗ Incorrect
Calling trim() on a number causes a TypeError since numbers don't have that method. The pipe should check the type before calling trim.
❓ state_output
advanced2:00remaining
What is the value returned by this custom pipe after transformation?
Given this pipe that parses JSON strings, what is the returned value when input is '{"a":1,"b":2}'?
NestJS
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common'; @Injectable() export class ParseJsonPipe implements PipeTransform { transform(value: any) { try { return JSON.parse(value); } catch { throw new BadRequestException('Invalid JSON string'); } } }
Attempts:
2 left
💡 Hint
Remember what JSON.parse returns.
✗ Incorrect
JSON.parse converts the JSON string into a JavaScript object with keys and values.
🧠 Conceptual
expert2:00remaining
Which statement about custom pipes in NestJS is true?
Select the correct statement about how custom pipes work in NestJS.
Attempts:
2 left
💡 Hint
Think about when pipes are executed in the request lifecycle.
✗ Incorrect
Pipes run before the route handler and can transform or validate data. They can return synchronous or asynchronous values.