ParseAgePipePipeTransform interfaceBadRequestException if notParseAgePipePipeTransform interfaceBadRequestException if notParseAgePipe that implements the PipeTransform interface from @nestjs/common.Start by importing PipeTransform and create a class with a transform method.
transform method of ParseAgePipe, add code to check if value is a number string. If not, throw a BadRequestException from @nestjs/common with the message 'Invalid age'.Use Number(value) to convert and isNaN() to check validity.
createUser that takes a parameter age decorated with @Body('age', new ParseAgePipe()). The method should return the age value.Use @Body('age', new ParseAgePipe()) to apply the pipe to the age parameter.
ParseAgePipe class is exported and imported correctly in the controller file. Confirm the controller uses the pipe in the createUser method parameter.Make sure the pipe class is exported and imported correctly, and used in the controller method parameter.
What is the main purpose of a custom pipe in NestJS?
Which of the following is the correct way to define a custom pipe class in NestJS?
export class MyPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata) {
// logic here
}
}Given this custom pipe code, what will be the output if the input value is "123"?
import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
@Injectable()
export class ParseIntPipe implements PipeTransform {
transform(value: any) {
const val = parseInt(value, 10);
if (isNaN(val)) {
throw new BadRequestException('Validation failed');
}
return val;
}
}Input: "123"
Identify the error in this custom pipe code:
import { PipeTransform, Injectable } from '@nestjs/common';
@Injectable()
export class UppercasePipe implements PipeTransform {
transform(value: string) {
return value.toUpperCase;
}
}You want to create a custom pipe that trims whitespace and converts input to lowercase. Which of these implementations correctly achieves this?
import { PipeTransform, Injectable } from '@nestjs/common';
@Injectable()
export class NormalizePipe implements PipeTransform {
transform(value: any) {
// Which option is correct?
}
}Pass the test and this pattern moves unknown