1.6.0 • Published 7 months ago
@russ-b/nestjs-common-tools v1.6.0
nestjs-common-tools
NestJS Common Tools
Entity Validator
A custom validator for NestJS that validates if an entity exists in the database using TypeORM.
Setup
- Register the validator and setup class-validator container in your application:
// main.ts
import { useContainer } from 'class-validator';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
useContainer(app.select(AppModule), {
fallbackOnErrors: true
});
await app.listen(3000);
}- Register the validator constraint in your
AppModule:
// app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { EntityConstraint } from './common/validators/entity/entity.constraint';
@Module({
providers: [
EntityConstraint,
// other providers
]
})
export class AppModule {}Usage
import { IsEntity } from '@russ-b/nestjs-common-tools/common/validators/entity';
export class UserDto {
// Validate single entity
@IsEntity(User)
userId: string;
// Validate array of entities
@IsEntity(Role, { each: true })
roleIds: string[];
// Custom property validation
@IsEntity(User, { property: 'customId' })
userCustomId: string;
// Disable UUID validation
@IsEntity(User, { isUuid: false })
numericId: number;
}Options
| Option | Type | Default | Description |
|---|---|---|---|
| isUuid | boolean | false | Validate if the value is a valid UUID |
| each | boolean | false | Apply validation to each item in array |
| property | string | 'id' | Database property to check against |
Example
// user.dto.ts
export class AssignRolesDto {
@IsArray()
@IsEntity(Role, {
each: true,
isUuid: true,
property: 'id'
})
roleIds: string[];
}1.6.0
7 months ago
1.5.3
7 months ago
1.5.2
7 months ago
1.5.1
7 months ago
1.5.0
7 months ago
1.4.1
9 months ago
1.4.0
9 months ago
1.3.5
9 months ago
1.3.4
9 months ago
1.3.3
9 months ago
1.3.2
9 months ago
1.3.0
9 months ago
1.1.25
10 months ago
1.1.24
10 months ago
1.1.23
10 months ago
1.1.22
10 months ago
1.1.21
10 months ago
1.1.20
10 months ago
1.1.19
10 months ago
1.1.18
10 months ago
1.1.17
10 months ago
1.1.16
10 months ago
1.1.15
10 months ago
1.1.14
10 months ago
1.1.13
10 months ago
1.1.12
10 months ago
1.1.11
10 months ago
1.1.10
10 months ago
1.1.9
10 months ago
1.1.8
10 months ago
1.1.7
10 months ago
1.1.6
10 months ago
1.1.5
10 months ago
1.1.4
10 months ago
1.1.3
10 months ago
1.1.2
10 months ago
1.0.0
10 months ago