Skip to content

Commit 4250213

Browse files
committed
employeeNumber
1 parent 2b9aa5f commit 4250213

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Injectable } from '@nestjs/common'
2+
import { InjectConnection } from '@nestjs/mongoose'
3+
import {
4+
ValidationArguments,
5+
ValidationOptions,
6+
ValidatorConstraint,
7+
ValidatorConstraintInterface,
8+
registerDecorator,
9+
} from 'class-validator'
10+
import { Connection } from 'mongoose'
11+
import { get } from 'radash'
12+
13+
@ValidatorConstraint({ name: 'UniquenessMailValidator', async: true })
14+
@Injectable()
15+
export class UniquenessMailValidator implements ValidatorConstraintInterface {
16+
public constructor(@InjectConnection() private connection: Connection) { }
17+
public async validate(
18+
value: any,
19+
args?: ValidationArguments
20+
): Promise<boolean> {
21+
for (const contraint of args.constraints) {
22+
const $ne = get(args.object, contraint.primaryKey)
23+
const val = get(value, contraint.key)
24+
const count = await this.connection.collection(contraint.collection).countDocuments({
25+
[contraint.primaryKey]: { $ne },
26+
[contraint.key]: val,
27+
})
28+
29+
console.log('count', count)
30+
console.log('debug', {
31+
[contraint.primaryKey]: { $ne },
32+
[contraint.key]: val,
33+
})
34+
35+
if (count > 0) {
36+
return false
37+
}
38+
}
39+
40+
return true
41+
}
42+
43+
defaultMessage(validationArguments?: ValidationArguments): string {
44+
// return custom field message
45+
const field: string = validationArguments.property
46+
return `${field} is already exist`
47+
}
48+
}
49+
50+
export type UniquenessMailInterface = {
51+
collection: string
52+
primaryKey: string
53+
key: string
54+
}
55+
56+
export function isUnique(options: UniquenessMailInterface, validationOptions?: ValidationOptions) {
57+
return function (object: any, propertyName: string) {
58+
registerDecorator({
59+
name: 'isUnique',
60+
target: object.constructor,
61+
propertyName: propertyName,
62+
options: validationOptions,
63+
constraints: [options],
64+
validator: UniquenessMailValidator,
65+
})
66+
}
67+
}

src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { readFileSync } from 'fs';
1212
import * as http from 'http';
1313
import * as https from 'https';
1414
import { ShutdownObserver } from './_common/observers/shutdown.observer';
15+
import { useContainer } from 'class-validator';
1516

1617
declare const module: any;
1718
(async (): Promise<void> => {
@@ -22,8 +23,6 @@ declare const module: any;
2223
});
2324
await logger.initialize();
2425

25-
console.log('cfg', cfg.application);
26-
2726
let extraOptions = <any>{};
2827
if (cfg.application?.https?.enabled) {
2928
try {
@@ -56,6 +55,8 @@ declare const module: any;
5655
await (await import('./swagger')).default(app);
5756
}
5857

58+
useContainer(app.select(AppModule), { fallbackOnErrors: true });
59+
5960
await app.init();
6061

6162
const shutdownObserver = app.get(ShutdownObserver);

0 commit comments

Comments
 (0)