0
0
NestJSframework~5 mins

Why pipes transform and validate input in NestJS

Choose your learning style9 modes available
Introduction

Pipes in NestJS help check if the data coming into your app is correct and change it into the right format. This keeps your app safe and working well.

When you want to make sure user input is the right type before using it.
When you need to change input data into a format your app expects.
When you want to stop bad data from causing errors in your app.
When you want to give clear error messages if input is wrong.
When you want to keep your code clean by handling input checks in one place.
Syntax
NestJS
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class ExamplePipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    // Validate or transform the value
    if (typeof value !== 'string') {
      throw new BadRequestException('Invalid input');
    }
    return value.toUpperCase();
  }
}

The transform method runs automatically on input data.

Throwing an exception stops the request and sends an error response.

Examples
This pipe checks if the input can be turned into a number. If not, it throws an error.
NestJS
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';

@Injectable()
export class ParseIntPipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    const val = parseInt(value, 10);
    if (isNaN(val)) {
      throw new BadRequestException('Validation failed');
    }
    return val;
  }
}
This pipe removes spaces from the start and end of a string input.
NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common';

@Injectable()
export class TrimPipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    if (typeof value === 'string') {
      return value.trim();
    }
    return value;
  }
}
Sample Program

This example shows a controller that uses a pipe to check if the query parameter 'id' is a number. If not, it sends an error. If yes, it returns a message with the id.

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

@Injectable()
class ParseIntPipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    const val = parseInt(value, 10);
    if (isNaN(val)) {
      throw new BadRequestException('Validation failed: Not a number');
    }
    return val;
  }
}

@Controller('items')
export class ItemsController {
  @Get()
  @UsePipes(new ParseIntPipe())
  getItem(@Query('id') id: string) {
    return `Item id is ${id}`;
  }
}
OutputSuccess
Important Notes

Pipes run before your route handler, so they catch bad data early.

You can reuse pipes in many places to keep your code DRY (Don't Repeat Yourself).

Always provide clear error messages to help users fix their input.

Summary

Pipes check and change input data before your app uses it.

They help keep your app safe and error-free.

Use pipes to make your code cleaner and easier to maintain.