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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@haorama/nestjs-fv

v0.4.0

Published

Fastest validator for nestjs

Downloads

8

Readme

nestjs-fv

Fastest validator for nestjs

Features

  • Class Validation (similar to class-validator)
  • built-in validation pipe

Installation

npm install @haorama/nestjs-fv

or

yarn add @haorama/nestjs-fv

Usage

Create your class and put some validation decorators on the properties you want to validate:

import { IsString, IsNumber, IsEmail } from "@haorama/nestjs-fv"

export class CreateUserDTO {
  @IsString()
  name: string

  @IsEmail()
  email: string;

  @IsNumber()
  age: number;
}

and somewhere in your controller

import { ValidationPipe } from "@haorama/nestjs-fv";
import { CreateUserDTO } from "./create-user.dto";
import { Controller, UsePipes, Post, Body } from "@nestjs/common";

@Controller("users")
@UsePipes(ValidationPipe)
export class UserController {
  @Post()
  async create(@Body() data: CreateUserDTO) {
    return data;
  }
}

Notice that you'll received unhandled exceptions, next step is to create exception filter to sends an appropriate user-friendly response.

Also noted that validation pipe by default add $$strict = "remove" if there is no $$strict prop defined in your class schema. you can overwrite or use your own validation pipe to modify the behavior.

import { ValidationError } from "@haorama/nestjs-fv";
import { ArgumentsHost, Catch, ExceptionFilter } from "@nestjs/common";
import { Response } from "express"; // change this if you're using fastify

@Catch(ValidationError)
export class ValidationExceptionFilter
  implements ExceptionFilter<ValidationError>
{
  catch(exception: ValidationError, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const res = ctx.getResponse<Response>();

    const errors = exception.errors.map((error) => ({
      field: error.field,
      message: error.message,
    }));

    return res.status(422).json({
      message: "Unprocessable Entity",
      statusCode: 422,
      errors,
    });
  }
}

and add exception filters into our UserController:

import { ValidationPipe } from "@haorama/nestjs-fv";
import { CreateUserDTO } from "./create-user.dto";
import { ValidationExceptionFilter } from "./validation-exception.filter";
import { Controller, UsePipes, Post, Body, UseFilters } from "@nestjs/common";

@Controller("users")
@UsePipes(ValidationPipe)
@UseFilters(ValidationExceptionFilter)
export class UserController {
  @Post()
  async create(@Body() data: CreateUserDTO) {
    return data;
  }
}

@IsArray

This is an array validator.

import { IsArray } from "@haorama/nestjs-fv";

export class CreateUserDTO {
  @IsArray({ items: "string" })
  roles: string[];

  // OR
  @IsArray({ items: { type: "string", enum: ["editor", "viewer"]} })
  roles: string[];
}

@IsBoolean

This is an boolean validator, by default value will be converted to boolean (1,0, on, off, "true", "false")

import { IsBoolean } from "@haorama/nestjs-fv";

export class CreateUserDTO {
  @IsBoolean()
  subscribed: boolean;
}

@IsDate

This is an date validator, by default value will be convert to Date

import { IsDate } from "@haorama/nestjs-fv";

export class CreatePostDTO {
  @IsDate()
  created: Date;
}

@IsEmail

This is an email validator

import { IsEmail } from "@haorama/nestjs-fv";

export class CreateUserDTO {
  @IsEmail()
  email: string;
}

@IsEqual

This is an equal validator.

import { IsString, IsEqual } from "@haorama/nestjs-fv";

export class CreateUserDTO {
  @IsString()
  password: string;

  @IsEqual("password")
  confirm_password: string;

  @IsEqual({ value: true, strict: true }) // this also worked
  agreeTerms: boolean;
}

@IsNumber

This is an number validator, by default value will be converted to number.

import { IsNumber } from "@haoarama/nestjs-fv";

export class CreateUserDTO {
  @IsNumber()
  age: number;
}

@IsObject

This is an object validator

import { IsObject } from "@haorama/nestjs-fv";

export class CreateUserDTO {
  @IsObject({
    strict: "remove", //true, false, remove
    props: {
      street: "string", //string short-hand definition
      city: { type: "string" },
      zip: { type: "number" }
    }
  })
  address: Record<string, any>; //or you can replace with class or interface ( because we dont support nested schema yet)
}

@IsString

This is an string validator.

import { IsString } from "@haorama/nestjs-fv";
export class CreateUserDTO {
  @IsString({convert: false, empty: true})
  name: string;
}

@IsUrl

This is an url validator

import { IsUrl } from "@haorama/nestjs-fv";

export class CreateProfileDTO {
  @IsUrl()
  github_url: string;
}

Unsupported Rule

For now we dont add all rules from fastest-validator, but you can still use it by using IsRule decorator:

import {IsRule, IsString} from "@haorama/nestjs-fv";

export class LoginDTO {
  @IsString({ min: 8 })
  password: string;

  @IsRule({ type: "equal", field: "password" })
  password_confirmation: string;
}

Error / Validation Messages

by default we overwrite some of the fastest-validator default messages. removing 'field' inside field placeholder. you can check the overwrite messages here https://github.com/haorama/nestjs-fv/blob/main/src/validator.ts

TODO

  • Add more decorators provided by fastest-validator
  • Support Nested Schema
  • Unit and e2e testing?