Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NestJS pipes validate or transform request values immediately before a route handler receives them. Use a built-in Parse* pipe for a single value, ValidationPipe for DTO rules, or StandardSchemaValidationPipe to validate against a compatible schema. A failed pipe throws an exception, so the handler does not run.

What a NestJS pipe does

A pipe is an injectable class that implements PipeTransform. Nest runs it at the boundary between incoming data and a handler argument. A pipe can check that a value is acceptable and return it, or transform it and return a replacement value. If it throws an exception, Nest handles that through its exception layer and skips the route handler.

That makes pipes a useful place to reject invalid external input before it reaches application logic. For example, a route parameter arrives as a string; a parsing pipe can validate it and provide a number to the handler. See the NestJS pipes guide.

Parse one parameter with a built-in pipe

For an individual path or query value, a built-in parsing pipe is usually the clearest choice:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
  return this.catsService.findOne(id);
}

Nest applies ParseIntPipe to the id parameter before calling findOne. If the input is not a valid integer, the documented default behavior is an HTTP 400 Bad Request; the handler is not called. Query parameters can be bound in the same way.

Passing the pipe class lets Nest instantiate it, including through dependency injection. Pass an instance when you need to configure options, such as a different HTTP status. For UUID input, ParseUUIDPipe validates the value as a UUID. By default it accepts any UUID version; set its version option when you need to restrict that. The pipes guide documents these built-ins and options.

Validate request bodies with a DTO

Use ValidationPipe when your validation rules belong on a DTO class. Nest’s documented DTO approach uses class-validator decorators for rules and class-transformer for transformation. Install those packages for this approach. TypeScript types alone do not validate incoming data at runtime: the decorators on the DTO supply validation metadata.

import { IsEmail, IsString } from 'class-validator';

export class CreateUserDto {
  @IsEmail()
  email: string;

  @IsString()
  name: string;
}

@Post()
@UsePipes(new ValidationPipe())
create(@Body() body: CreateUserDto) {
  return this.usersService.create(body);
}

Here, @UsePipes() makes the pipe method-scoped, so it applies to the handler’s parameters rather than only to body. For an application-wide validation policy, Nest documents app.useGlobalPipes(new ValidationPipe()); a pipe can also be registered application-wide through an APP_PIPE provider. See the NestJS validation guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Control unexpected properties

With whitelist: true, ValidationPipe removes properties that have no validation decorators. Add forbidNonWhitelisted: true alongside the whitelist option to reject a request containing such properties instead of silently removing them.

app.useGlobalPipes(new ValidationPipe({
  whitelist: true,
  forbidNonWhitelisted: true,
}));

Choose when input should be transformed

Path and query parameters arrive as strings. With transform: true, ValidationPipe can convert primitive path or query values according to the handler’s declared type and turn plain request bodies into DTO instances.

app.useGlobalPipes(new ValidationPipe({ transform: true }));

@Get(':id')
findOne(@Param('id') id: number) {
  return this.catsService.findOne(id);
}

This example relies on the global pipe’s transformation setting. Without transform: true, do not assume a parameter declared as number has been converted from its incoming string. Bind an explicit pipe instead when you want local, visible conversion:

@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
  return this.catsService.findOne(id);
}

The validation guide describes DTO and primitive transformation as well as explicit parsing.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Validate against a schema

If your rules are defined in a compatible schema rather than DTO decorators, the current NestJS pipes guide recommends its built-in StandardSchemaValidationPipe for schema-based production validation. The guide names Zod, Valibot, and ArkType as examples of compatible libraries. A schema can be supplied through the schema option of a parameter decorator. Unlike DTO decorator validation, the schema defines both the checks and the parsed output.

The same guide also demonstrates a custom Zod pipe: implement PipeTransform, call schema.parse(value), return the parsed result, and convert a parse failure into BadRequestException. That example shows how a pipe works; for production schema validation, prefer the built-in option recommended by the current guide. See the pipes guide and validation guide.

Write a custom pipe when built-ins do not fit

A custom pipe’s transform() method receives the input and returns the value that Nest passes to the handler. It can throw an exception when the input is unacceptable. This simple integer example illustrates the contract:

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

export class SimpleIntPipe implements PipeTransform<string, number> {
  transform(value: string): number {
    const parsed = parseInt(value, 10);

    if (Number.isNaN(parsed)) {
      throw new BadRequestException('Validation failed');
    }

    return parsed;
  }
}

This intentionally simple parser is for understanding transform(), not a recommendation to replace ParseIntPipe. Nest’s documentation notes that the built-in pipe is more sophisticated.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a binding scope

Bind a pipe narrowly when only one argument needs it, and use a broader scope when the same behavior should apply across handlers. A broader scope can affect multiple handler parameters, so check what each binding covers.

Binding scope How to bind What it applies to
Parameter @Param('id', ParseIntPipe), or a pipe on a body or query parameter decorator One bound value.
Method @UsePipes(...) on a handler Parameters of that handler.
Controller @UsePipes(...) on a controller Handlers and parameters covered by that controller.
Application app.useGlobalPipes(...) or an APP_PIPE provider Application-wide handlers and parameters.

For WebSocket gateways, method-, gateway-, and global-scoped pipes apply to every message-handler parameter; parameter binding can target only the message payload. Gateway-specific examples are in the NestJS gateway pipes guide.

What happens when a pipe rejects input

Pipes run in Nest’s exceptions zone. When a pipe throws, the applicable global exception filter and any context-specific filter handle the exception, and the handler does not run. For a straightforward invalid integer, ParseIntPipe returns HTTP 400 by default; an instance can be configured when a different status or error behavior is needed. See the NestJS pipes guide.

Which pipe should you use?

  • One route or query value: Use the relevant built-in Parse* pipe when you need to parse or validate that value explicitly.
  • A DTO with decorator-based rules: Use ValidationPipe; enable transformation deliberately if you want plain input converted to DTO instances or primitive parameters converted from strings.
  • Rules already expressed as a compatible schema: Use StandardSchemaValidationPipe and let the schema define validation and parsed output.
  • Behavior the built-ins do not cover: Implement PipeTransform, return the value the handler should receive, and throw an appropriate Nest exception for rejected input.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.