0
0
NestJSframework~10 mins

Built-in pipes (ParseIntPipe, ParseBoolPipe) in NestJS - Interactive Code Practice

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

Complete the code to use ParseIntPipe to convert the route parameter to a number.

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

@Controller('items')
export class ItemsController {
  @Get(':id')
  getItem(@Param('id', [1]) id: number) {
    return `Item id is ${id}`;
  }
}
Drag options to blanks, or click blank then click option'
AParseIntPipe
BParseBoolPipe
CValidationPipe
DDefaultValuePipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseBoolPipe instead of ParseIntPipe causes type errors.
Not using any pipe leaves the parameter as a string.
2fill in blank
medium

Complete the code to use ParseBoolPipe to convert the query parameter to a boolean.

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

@Controller('flags')
export class FlagsController {
  @Get()
  getFlag(@Query('active', [1]) active: boolean) {
    return `Flag active is ${active}`;
  }
}
Drag options to blanks, or click blank then click option'
AParseIntPipe
BParseBoolPipe
CDefaultValuePipe
DValidationPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseIntPipe instead of ParseBoolPipe causes wrong type conversion.
Not using any pipe leaves the parameter as a string.
3fill in blank
hard

Fix the error in the code by choosing the correct pipe to parse the 'count' parameter as an integer.

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

@Controller('counts')
export class CountsController {
  @Get(':count')
  getCount(@Param('count', [1]) count: number) {
    return `Count is ${count}`;
  }
}
Drag options to blanks, or click blank then click option'
AValidationPipe
BParseBoolPipe
CDefaultValuePipe
DParseIntPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseBoolPipe causes wrong type conversion.
Not using any pipe causes the parameter to remain a string.
4fill in blank
hard

Fill both blanks to create a controller method that parses 'id' as an integer and 'enabled' as a boolean.

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

@Controller('settings')
export class SettingsController {
  @Get(':id')
  getSetting(
    @Param('id', [1]) id: number,
    @Query('enabled', [2]) enabled: boolean
  ) {
    return `Setting ${id} is ${enabled ? 'enabled' : 'disabled'}`;
  }
}
Drag options to blanks, or click blank then click option'
AParseIntPipe
BValidationPipe
CParseBoolPipe
DDefaultValuePipe
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the pipes causes wrong type conversions.
Using ValidationPipe does not convert types automatically.
5fill in blank
hard

Fill all three blanks to parse 'userId' as an integer, 'isAdmin' as a boolean, and 'page' as an integer.

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

@Controller('users')
export class UsersController {
  @Get(':userId')
  getUser(
    @Param('userId', [1]) userId: number,
    @Query('isAdmin', [2]) isAdmin: boolean,
    @Query('page', [3]) page: number
  ) {
    return `User ${userId}, Admin: ${isAdmin}, Page: ${page}`;
  }
}
Drag options to blanks, or click blank then click option'
AParseIntPipe
BParseBoolPipe
CDefaultValuePipe
DValidationPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseBoolPipe for number parameters causes wrong type conversion.
Not using pipes leaves parameters as strings.