Skip to main content

class-validator Validation

Use NestJS-style DTOs with class-validator and ValidationPipe instead of Zod.

Setup

npm i class-validator class-transformer

Define a DTO

import { IsString, MinLength, IsOptional } from 'class-validator';

export class CreateCatDto {
@IsString()
@MinLength(1)
name!: string;

@IsOptional()
@IsString()
breed?: string;
}

Use with @Input()

import { UsePipes, ValidationPipe } from '@nestjs/common';
import { Input, Mutation, Router } from 'nest-trpc-native';
import { CreateCatDto } from './create-cat.dto';

@Router('cats')
class CatsRouter {
@Mutation()
@UsePipes(new ValidationPipe({ whitelist: true }))
create(@Input() input: CreateCatDto) {
return { id: '1', ...input };
}
}

Global ValidationPipe

Apply validation globally to all procedures:

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

When to Use class-validator vs Zod

class-validatorZod
StyleDecorators on classesFunctional schemas
Type inferenceManual DTO typesAutomatic z.infer
tRPC schema genNot supportedFull support
Nest familiarityNative Nest patterntRPC-native pattern
tip

You can use both approaches in the same app. Use Zod for procedures that benefit from auto-generated types, and class-validator for procedures where you prefer the DTO pattern.