npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dismissible/nestjs-validation

v3.0.0

Published

Validation service module for NestJS applications using class-validator and class-transformer

Readme

Dismissible manages the state of your UI elements across sessions, so your users see what matters, once! No more onboarding messages reappearing on every tab, no more notifications haunting users across devices. Dismissible syncs dismissal state everywhere, so every message is intentional, never repetitive.

@dismissible/nestjs-validation

A validation service for NestJS applications using class-validator and class-transformer.

Part of the Dismissible API - This library is part of the Dismissible API ecosystem. Visit dismissible.io for more information and documentation.

Overview

This library provides a ValidationService that wraps class-validator and class-transformer to provide a consistent validation API for DTOs and class instances in NestJS applications.

Installation

npm install @dismissible/nestjs-validation

You'll also need to install the peer dependencies:

npm install class-validator class-transformer

Getting Started

Basic Setup

import { Module } from '@nestjs/common';
import { ValidationModule } from '@dismissible/nestjs-validation';

@Module({
  imports: [ValidationModule],
})
export class AppModule {}

Validating DTOs

The ValidationService can validate plain objects against DTO classes:

import { Injectable } from '@nestjs/common';
import { ValidationService } from '@dismissible/nestjs-validation';
import { IsString, IsEmail, IsOptional } from 'class-validator';

class CreateUserDto {
  @IsString()
  name!: string;

  @IsEmail()
  email!: string;

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

@Injectable()
export class UserService {
  constructor(private readonly validationService: ValidationService) {}

  async createUser(data: unknown) {
    // Validates and transforms the data, throws BadRequestException if invalid
    const dto = await this.validationService.validateDto(CreateUserDto, data);

    // dto is now a validated instance of CreateUserDto
    // Use dto.name, dto.email, etc.
  }
}

Validating Existing Instances

You can also validate class instances that have already been created:

import { Injectable } from '@nestjs/common';
import { ValidationService } from '@dismissible/nestjs-validation';
import { IsString, IsDate } from 'class-validator';

class MyDto {
  @IsString()
  name!: string;

  @IsDate()
  createdAt!: Date;
}

@Injectable()
export class MyService {
  constructor(private readonly validationService: ValidationService) {}

  async validateInstance(dto: MyDto) {
    // Validates the instance, throws BadRequestException if invalid
    await this.validationService.validateInstance(dto);
  }
}

Error Handling

The validation service throws BadRequestException with formatted error messages when validation fails:

import { BadRequestException } from '@nestjs/common';
import { ValidationService } from '@dismissible/nestjs-validation';

@Injectable()
export class MyService {
  constructor(private readonly validationService: ValidationService) {}

  async handleRequest(data: unknown) {
    try {
      const dto = await this.validationService.validateDto(MyDto, data);
      // Process valid data
    } catch (error) {
      if (error instanceof BadRequestException) {
        // Handle validation errors
        console.error('Validation failed:', error.message);
      }
      throw error;
    }
  }
}

API Reference

ValidationService

validateDto<T>(dtoClass, data): Promise<T>

Validates and transforms plain data into a DTO instance.

Parameters:

  • dtoClass: ClassConstructor<T> - The DTO class to validate against
  • data: unknown - The data to validate

Returns: Promise<T> - A validated instance of the DTO class

Throws: BadRequestException if validation fails

validateInstance<T>(instance): Promise<void>

Validates an existing class instance.

Parameters:

  • instance: T - The instance to validate

Throws: BadRequestException if validation fails

ValidationModule

A NestJS module that provides ValidationService as a singleton.

Exports:

  • ValidationService - The validation service

Features

  • Automatic transformation using class-transformer
  • Validation using class-validator decorators
  • Nested validation error extraction
  • Formatted error messages
  • Type-safe DTOs with TypeScript generics

Example: Using in a Controller

import { Controller, Post, Body } from '@nestjs/common';
import { ValidationService } from '@dismissible/nestjs-validation';
import { IsString, IsEmail } from 'class-validator';

class CreateUserDto {
  @IsString()
  name!: string;

  @IsEmail()
  email!: string;
}

@Controller('users')
export class UserController {
  constructor(private readonly validationService: ValidationService) {}

  @Post()
  async create(@Body() body: unknown) {
    const dto = await this.validationService.validateDto(CreateUserDto, body);
    // Use validated dto
    return { message: `Creating user: ${dto.name}` };
  }
}

Related Packages

This validation service is used by:

  • @dismissible/nestjs-core - Validates dismissible items

License

MIT