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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@code-net/json-schema-class-nestjs-validation-pipe

v1.0.1

Published

This library provides a NestJS validation pipe that uses classes transformed to JSON Schema for validation.

Readme

json-schema-class-nestjs-validation-pipe

This library provides a NestJS validation pipe that uses classes transformed to JSON Schema for validation.

Installation

npm install json-schema-class json-schema-class-nestjs-validation-pipe ajv ajv-formats

Usage globally

In your app module:

import { Module } from '@nestjs/common';
import { APP_PIPE } from '@nestjs/core';
import { JsonSchemaValidationPipe } from '@code-net/json-schema-class-nestjs-validation-pipe';
import { Enum, Items, JsonSchema, JsonSchemaResolver, Optional, Required, Type } from '@code-net/json-schema-class';

@JsonSchema()
class UserIdentityDto {
  @Required()
  id: string;

  @Optional()
  @Enum(['google', 'github', 'email'])
  provider: string;
}

@JsonSchema({
  title: 'User',
  description: 'Just a user example',
})
class RegisterUserDto {
  @Required()
  @Type('number')
  id: number;

  @Optional()
  name: string;

  @Required()
  @Items(UserIdentityDto)
  identities: UserIdentityDto[];
}

@Module({
  providers: [
    {
      provide: APP_PIPE,
      useClass: JsonSchemaValidationPipe,
    },
  ],
})
export class AppModule {}

Then you can use the RegisterUserDto class in your controllers:

class UserController {
  @Post('register')
  async register(@Body() user: RegisterUserDto) {
    // user was validated against the JSON Schema
  }
}

Usage in a specific controller

You can also use the JsonSchemaValidationPipe in a specific controller or route handler:

import { Controller, Post, Body, UsePipes } from '@nestjs/common';
import { JsonSchemaValidationPipe } from '@code-net/json-schema-class-nestjs-validation-pipe';
import { RegisterUserDto } from './register-user.dto';

@Controller('user')
export class UserController {
  @Post('register')
  @UsePipes(new JsonSchemaValidationPipe())
  async register(@Body() user: RegisterUserDto) {
    // user was validated with AJV against the JSON Schema
  }
}

Customizing the Pipe

You can customize the JsonSchemaValidationPipe by passing options to its constructor:

import { JsonSchemaResolver } from '@code-net/json-schema-class';
import { JsonSchemaValidationPipe } from '@code-net/json-schema-class-nestjs-validation-pipe';
import Ajv from 'ajv';

const resolver = new JsonSchemaResolver(
  (name) => `/definitions/${name}.json#`
)

const ajv = new Ajv({
  // Custom AJV options 
  allErrors: true,
  coerceTypes: true,
  // Pass all the JSON schemas (mandatory)
  schemas: resolver.all(),
});

new JsonSchemaValidationPipe({
  ajv, // Custom AJV instance
  resolver, // Custom resolver
  createError: ({ errors }) => {
    // Custom error handling
    return new BadRequestException(ajv.errorsText(errors));
  },
});