0
0
NestJSframework~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply a built-in validation pipe to the controller method.

NestJS
import { Controller, Post, Body, [1] } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body(new [1]()) userData: any) {
    return userData;
  }
}
Drag options to blanks, or click blank then click option'
AUseGuards
BValidationPipe
CParseIntPipe
DHttpException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a pipe that only parses numbers instead of validating the whole object.
Forgetting to instantiate the pipe with parentheses.
2fill in blank
medium

Complete the code to transform the input string to a number using a pipe.

NestJS
import { Controller, Get, Param, [1] } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get(':id')
  getItem(@Param('id', new [1]()) id: number) {
    return { id };
  }
}
Drag options to blanks, or click blank then click option'
AParseIntPipe
BDefaultValuePipe
CParseBoolPipe
DValidationPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValidationPipe which does not transform types by default.
Not instantiating the pipe with parentheses.
3fill in blank
hard

Fix the error in the pipe usage to enable automatic transformation of input data.

NestJS
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';

@Controller('products')
export class ProductsController {
  @Post()
  addProduct(@Body(new ValidationPipe({ [1]: true })) productData: any) {
    return productData;
  }
}
Drag options to blanks, or click blank then click option'
Atransform
Bvalidate
CautoTransform
DtransformInput
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect option names that do not exist in ValidationPipe.
Not enabling transformation causes type mismatches.
4fill in blank
hard

Fill both blanks to create a custom pipe that validates and transforms input.

NestJS
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class [1] implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    if (typeof value !== '[2]') {
      throw new BadRequestException('Validation failed');
    }
    return value;
  }
}
Drag options to blanks, or click blank then click option'
AStringValidationPipe
BNumberValidationPipe
Cstring
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between class name and type checked.
Using wrong type string in typeof comparison.
5fill in blank
hard

Fill all three blanks to apply a global validation pipe with transformation enabled.

NestJS
import { ValidationPipe, [1] } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new [2]({ [3]: true }));
  await app.listen(3000);
}
bootstrap();
Drag options to blanks, or click blank then click option'
AINestApplication
BValidationPipe
Ctransform
DParseIntPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseIntPipe globally instead of ValidationPipe.
Forgetting to enable transform option causes no type conversion.