Skip to main content


The ValidationPipe is a built-in pipe that can be used to validate data coming from the client. It uses class-validator package under the hood. The ValidationPipe provides a convenient approach to enforce validation rules for all incoming client payloads, where the specific rules are declared with simple annotations in DTO declarations in each module.


$ npm install class-validator class-transformer


Now we can add a few validation rules in our CreateUserDto. We do this using decorators provided by the class-validator package, described in detail here. In this fashion, any route that uses the CreateUserDto will automatically enforce these validation rules.

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

export class CreateUserDto {
name: 'name',
description: 'Your name',
required: true
name: string;

name: 'text',
description: 'Your email',
required: false
email: string;

Also you can use ValidationPipe for transforming and validating the payload of a request.

Read more about transforming

Now, we can use the ValidationPipe in our UsersCommands to enforce the validation rules we just defined.

import { Injectable } from '@nestjs/common';
import { Context, SlashCommand, SlashCommandContext } from 'necord';

export class UsersCommands {
name: 'create',
description: 'Create a new user'
public async onCreateUser(
@Context() [interaction]: SlashCommandContext,
@Options(new ValidationPipe()) createUserDto: CreateUserDto
) {
return interaction.reply({ content: `User created: ${}` });

Now, if we try to use the create command without providing a name or email, we will get an error message.

{"statusCode":400,"message":["email should not be empty"],"error":"Bad Request"}

You can create snippet for validated options decorator:

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

export const ValidatedOptions = (...dataOrPipes: PipeTransform[] | string) => {
return Options(...dataOrPipes, new ValidationPipe());

You can create filters to handle and response validation errors.

See Exception Filters for more information