NestJS Profiler
Tutorials

DTO validation profiling

Inspect successful and failed DTO validations - class-validator or zod - in the profiler's Validator panel.

This tutorial shows how to add the validator collector to a NestJS application. The collector is validator-agnostic; this walkthrough uses class-validator (the default), and a nestjs-zod variant is shown below.

Prerequisites

  • @eleven-labs/nest-profiler installed and configured
  • A validation library. This tutorial uses class-validator + class-transformer, which are required only for the default class-validator pipe - they are not dependencies of @eleven-labs/nest-profiler-validator itself. For the nestjs-zod variant, install nestjs-zod + zod instead.

Validation stays app-owned

Production keeps its own validation pipe; the dev entry main-dev.ts swaps in createProfilerValidationPipe(...) through the shared bootstrap() hook, and ValidatorCollectorModule.forRoot() contributes only the panel. Validation always runs, with or without the profiler.

Step 1 - Install the package

pnpm add -D @eleven-labs/nest-profiler-validator
# this tutorial uses the default class-validator pipe (a regular dependency of your app):
pnpm add class-validator class-transformer

Step 2 - Install the pipe and register the panel

Both entrypoints share a bootstrap(rootModule, options) function (see Enabling and disabling the profiler). Production calls bootstrap(AppModule) and gets a plain new ValidationPipe(options). The dev entry swaps in the profiler's pipe with the same options — createClassValidatorPipe (rather than a bare new ValidationPipe()) attaches the raw ValidationError[] the panel reads, so violations show per property:

main-dev.ts
import { createProfilerLogger } from '@eleven-labs/nest-profiler';
import {
  createClassValidatorPipe,
  createProfilerValidationPipe,
} from '@eleven-labs/nest-profiler-validator';
import { AppDevModule } from './app.dev.module';
import { bootstrap } from './bootstrap';

void bootstrap(AppDevModule, {
  wrapLogger: (logger) => createProfilerLogger(logger),
  validationPipe: (options) => createProfilerValidationPipe(createClassValidatorPipe(options)),
});

Then register the Validator panel — forRoot() adds only the collector (no APP_PIPE), so it goes in the dev-only bundle like every other collector:

profiling/profiling.module.ts
import { Module } from '@nestjs/common';
import { ProfilerModule } from '@eleven-labs/nest-profiler';
import { ValidatorCollectorModule } from '@eleven-labs/nest-profiler-validator';

@Module({
  imports: [ProfilerModule.forRoot({ isGlobal: true }), ValidatorCollectorModule.forRoot()],
})
export class ProfilingModule {}

ProfilingModule is the dev-only bundle loaded by main-dev.ts — see Enabling and disabling the profiler. If the profiler is installed as a production dependency behind the runtime gate, wrap the same call in ConditionalModule.registerWhen(..., isProfilerEnabled).

Step 3 - Create a DTO with validation constraints

Use value imports, not type imports

Always import decorator functions from class-validator as value imports (not import type). The decorators must be executed at runtime to register metadata. Using import type strips them at compile time.

dto/create-article.dto.ts
import { IsString, IsNotEmpty, MinLength, MaxLength, IsOptional, IsUrl } from 'class-validator';

export class CreateArticleDto {
  @IsString()
  @IsNotEmpty()
  @MinLength(5)
  @MaxLength(120)
  title: string;

  @IsString()
  @IsNotEmpty()
  @MinLength(20)
  body: string;

  @IsOptional()
  @IsUrl()
  coverImageUrl?: string;
}

Step 4 - Use the DTO in a controller

articles.controller.ts
import { Body, Controller, Post } from '@nestjs/common';
import { CreateArticleDto } from './dto/create-article.dto';

@Controller('articles')
export class ArticlesController {
  @Post()
  create(@Body() dto: CreateArticleDto) {
    return { message: 'Article created', title: dto.title };
  }
}

Step 5 - Test it

Valid request:

curl -i -X POST http://localhost:3000/api/v1/articles \
  -H "Content-Type: application/json" \
  -d '{"title": "My first article", "body": "This is the body of the article with enough content."}'

Invalid request:

curl -i -X POST http://localhost:3000/api/v1/articles \
  -H "Content-Type: application/json" \
  -d '{"title": "Hi", "body": "Too short"}'

Open /_profiler/{token} and click the Validator tab.

For the valid request, you will see:

  • Source - ArticlesController.create
  • DTO class - CreateArticleDto
  • Status - valid
  • Violations - none

For the invalid request, you will see:

  • Source - ArticlesController.create
  • DTO class - CreateArticleDto
  • Status - invalid
  • Violations - one entry per failing property, listing the constraint names and messages

The toolbar badge shows the total count of validated DTOs, or N violations when at least one validation failed (e.g., 2 violations).

Capturing both valid and invalid validations

The collector records every validation that passes through the pipe - both successful and failed. This lets you:

  • Confirm that valid payloads pass all constraints
  • See exactly which constraints failed and on which properties
  • Debug validation logic without adding temporary logging

Even when validation fails and NestJS returns a 400 Bad Request, the profile is still created and the failed validation is recorded. Navigate to /_profiler to find the profile by timestamp.

Using a different validator (nestjs-zod)

The collector is validator-agnostic. To profile a nestjs-zod app instead, production uses its pipe directly and the dev entry wraps it — class-validator is never loaded:

main.ts
import { ZodValidationPipe } from 'nestjs-zod';

void bootstrap(AppModule, { validationPipe: () => new ZodValidationPipe() });
main-dev.ts
import { ZodValidationPipe } from 'nestjs-zod';
import { createProfilerValidationPipe } from '@eleven-labs/nest-profiler-validator';

void bootstrap(AppDevModule, {
  wrapLogger: (logger) => createProfilerLogger(logger),
  validationPipe: () => createProfilerValidationPipe(new ZodValidationPipe()),
});
dto/create-article.dto.ts
import { createZodDto } from 'nestjs-zod';
import { z } from 'zod';

const ArticleSchema = z.object({
  title: z.string().min(5).max(120),
  body: z.string().min(20),
  coverImageUrl: z.string().url().optional(),
});

export class CreateArticleDto extends createZodDto(ArticleSchema) {}

The Validator panel renders zod violations exactly the same way - one entry per failing property with its messages. A NestJS app uses a single global validation strategy, so use one validator at a time. createProfilerValidationPipe(inner, extractors?) also takes a custom extractor chain as its second argument.

Testing the app manually

Since the pipe is set in the bootstrap rather than a module, mirror the dev entry's useGlobalPipes(...) call when you boot the app yourself in e2e tests (Test.createTestingModule(...).createNestApplication()).

How it works

ProfilerValidationPipe wraps the validation pipe you configure: it records a valid/invalid entry into the active request profile and normalizes violations through duck-typed extractors (class-validator, then zod, then a generic HttpException fallback) before re-throwing the original exception. It resolves CLS statically, so createProfilerValidationPipe(...) needs no DI container. See How it works on the package page.

Powered & maintained by

On this page