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

db-class-validator

v1.1.2

Published

A class-validator extension package that is used to validate field in database

Downloads

24

Readme

db-class-validator

db-class-validator is a npm package for validating request parameters from database in nestjs using class-validator and typeorm

Table of Contents

Installation

To install it, using npm:

npm install db-class-validator

Usage

  1. To use this you first have to add following line in your main.ts file.
 useContainer(app.select(AppModule), { fallbackOnErrors: true });

useContainer is imported from class-validator it is used so that the validation function uses AppModule as the root module for dependency resolution.

  1. Add ExistsValidation class in your AppModule providers
import { Module } from '@nestjs/common';
import { ExistsValidation } from 'db-class-validator'; // Importing Validator Class

@Module({
  imports: [],
  controllers: [],
  providers: [
    ExistsValidation,  // Add This Here
  ],
})
export class AppModule { }
  1. Use the decorator on the property you want to validate in the Dto
import { Exists } from 'db-class-validator'; // Importing Decorator

export class YourDto { 
   @Exists(existsOptions,ValidatorOptions)  // Using Decorator
   property: string;
}

Features

The library has following decorators to validate your request data:

Exists

This decorator checks if the field under validation exists in the database and will throw error if it is not.

import { Exists } from 'db-class-validator'; // Importing Decorator

export class YourDto { 
  @Exists(
      {
          tableName: 'TABLE_NAME_IN_DATABASE',
          columnName?: 'COLUMN_NAME_IN_DATABASE'   // Column name is optional if it is not provided it will consider property name as column name
      },
      ValidatorOptions?
  )  // Using Decorator
  property: string;
}

This valdiation decorator uses ExistsValdiation Class for validation, which need to be imported in AppModule

import { Module } from '@nestjs/common';
import { ExistsValidation } from 'db-class-validator'; // Importing Validator Class

@Module({
 imports: [],
 controllers: [],
 providers: [
   ExistsValidation,  // Add This Here
 ],
})
export class AppModule { }

IsUnique

This decorator checks if the field under validation is unique and do not exists in the database.

import { IsUnique } from 'db-class-validator'; // Importing Decorator

export class YourDto { 
  @IsUnique(
      {
          tableName: 'TABLE_NAME_IN_DATABASE',
          columnName?: 'COLUMN_NAME_IN_DATABASE'   // Column name is optional if it is not provided it will consider property name as column name
      },
      ValidatorOptions?
  )  // Using Decorator
  property: string;
}

It can also be used to check if field under validation is unique but not against certain row, which is useful when validating during update operations

export class YourDto { 
  @IsUnique(
      {
          tableName: 'TABLE_NAME_IN_DATABASE',
          columnName?: 'COLUMN_NAME_IN_DATABASE',   // Column name is optional if it is not provided it will consider property name as column name
          valueToBeIgnored: VALUE_TO_BE_IGNORED, // It takes string | number | (ValidationArguments) => string | number
          valueToBeIgnoredColumn: 'COLUMN_NAME_TO_BE_IGNORED_IN_DATABASE'  // If `valueToBeIgnoredColumn` is not provided it checks in `id` column
      },
      ValidatorOptions?
  )  // Using Decorator
  property: string;
}

This valdiation decorator uses IsUniqueValdiation Class for validation, which need to be imported in AppModule

import { Module } from '@nestjs/common';
import { IsUniqueValidation } from 'db-class-validator'; // Importing Validator Class

@Module({
 imports: [],
 controllers: [],
 providers: [
   IsUniqueValidation,  // Add This Here
 ],
})
export class AppModule { }

Explanation

The package uses typeorm dataSource and queryRunner to check if value exists in the database or not

  const queryRunner = this.dataSource.createQueryRunner();
  const result = await queryRunner.query(query);

  queryRunner.release();