1.6.0 • Published 10 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
10 months ago
1.5.3
10 months ago
1.5.2
11 months ago
1.5.1
11 months ago
1.5.0
11 months ago
1.4.1
12 months ago
1.4.0
1 year ago
1.3.5
1 year ago
1.3.4
1 year ago
1.3.3
1 year ago
1.3.2
1 year ago
1.3.0
1 year ago
1.1.25
1 year ago
1.1.24
1 year ago
1.1.23
1 year ago
1.1.22
1 year ago
1.1.21
1 year ago
1.1.20
1 year ago
1.1.19
1 year ago
1.1.18
1 year ago
1.1.17
1 year ago
1.1.16
1 year ago
1.1.15
1 year ago
1.1.14
1 year ago
1.1.13
1 year ago
1.1.12
1 year ago
1.1.11
1 year ago
1.1.10
1 year ago
1.1.9
1 year ago
1.1.8
1 year ago
1.1.7
1 year ago
1.1.6
1 year ago
1.1.5
1 year ago
1.1.4
1 year ago
1.1.3
1 year ago
1.1.2
1 year ago
1.0.0
1 year ago