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

@rotyro-tools/nestjs-typeorm-validator

v1.0.9

Published

Lightweight NestJS decorators to validate data against TypeORM entities.

Downloads

368

Readme

NestJS TypeORM Validator

Publish to npm Coverage Commitizen friendly

Lightweight NestJS decorators for validating values against TypeORM entities.

This package provides several decorators (more to come):

| Decorator | Description | | ----------- | ------------------------------------------------------------- | | @ExistsIn | Ensures a value exists in a specified entity/table column. | | @UniqueIn | Ensures a value is unique in a specified entity/table column. |

It integrates with TypeORM DataSource instances, enabling database-aware validation in DTOs.


Key features

  • Works with TypeORM entity classes or table name strings.
  • Supports multiple (named) TypeORM DataSource registrations.
  • Supports class-validator options (for example each for array validation, or message for custom message).
  • Minimal runtime dependency surface — relies on TypeORM and class-validator as peer dependencies.

Installation

Install the package:

npm

npm install @rotyro-tools/nestjs-typeorm-validator

yarn

yarn add @rotyro-tools/nestjs-typeorm-validator

pnpm

pnpm add @rotyro-tools/nestjs-typeorm-validator

bun

bun add @rotyro-tools/nestjs-typeorm-validator

Peer dependencies

This package declares the following peer dependencies that consumers must install at compatible versions:

Install them:

npm

npm install --save class-validator typeorm

yarn

yarn add class-validator typeorm

pnpm

pnpm add class-validator typeorm

bun

bun add class-validator typeorm

Getting started

  1. Register one or more TypeORM DataSources to be used by the validators:
import { registerDataSourceForValidation } from '@rotyro-tools/nestjs-typeorm-validator';
import { DataSource } from 'typeorm';

const dataSource = new DataSource({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'user',
  password: 'pass',
  database: 'db',
  entities: [
    /* ... */
  ],
  synchronize: false,
});

// If you initialize the DataSource yourself, that's fine. If you don't, registerDataSourceForValidation will attempt to initialize it for you.
await dataSource.initialize();

// Register your DataSource
registerDataSourceForValidation(dataSource); // Registers as 'default'

// Second 'replica' DataSource
const replicaDataSource = new DataSource({
  type: 'mysql',
  host: 'replica-host',
  port: 3306,
  username: 'replica_user',
  password: 'replica_pass',
  database: 'db_replica',
  entities: [
    /* ... */
  ],
  synchronize: false,
});

// Optionally initialize the replica DataSource as well
await replicaDataSource.initialize();

// Register your DataSource
registerDataSourceForValidation(replicaDataSource, 'replica');
  1. Use decorators in your DTOs:
import { ExistsIn, UniqueIn } from '@rotyro-tools/nestjs-typeorm-validator';

import { User } from '@/modules/users/entities/user.entity';

class CreatePostDto {
  // Ensure the given authorId exists in the User entity's id column
  // in the 'default' DataSource
  @ExistsIn(User, 'id', null, { message: 'Author not found' })
  authorId: number;

  // Ensure the title is unique in the posts table (using a table name)
  // in the 'replica' DataSource
  @UniqueIn('post', 'title', 'replica', { message: 'Title already taken' })
  title: string;
}

Examples

Checking existence (minimal setup):

import { ExistsIn } from '@rotyro-tools/nestjs-typeorm-validator';

class CreateCommentDto {
  @ExistsIn('user', 'id')
  userId: number;
}

Unique constraint (entity usage and custom message):

import { UniqueIn } from '@rotyro-tools/nestjs-typeorm-validator';

import { User } from '@/modules/users/entities/user.entity';

class RegisterUserDto {
  @UniqueIn(User, 'email', null, { message: 'Email already in use' })
  email: string;
}

Validating arrays:

import { ExistsIn } from '@rotyro-tools/nestjs-typeorm-validator';

class BulkCreateDto {
  @ExistsIn('tag', 'name', 'replica', {
    each: true,
    message: 'Tag does not exist',
  })
  tagNames: string[];
}

Development

Install dependencies:

npm install

Build:

npm run build

Lint:

npm run lint

Format:

npm run format

Run tests:

npm run test

Commit (with Commitizen):

npm run cm