File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { Module } from '@nestjs/common' ;
2
2
import { APP_PIPE } from '@nestjs/core' ;
3
+ import { Validator } from 'class-validator' ;
3
4
import { ValidationPipe } from './validation.pipe' ;
4
5
5
6
@Module ( {
6
7
providers : [
8
+ Validator ,
7
9
ValidationPipe ,
8
10
{ provide : APP_PIPE , useExisting : ValidationPipe } ,
9
11
] ,
Original file line number Diff line number Diff line change @@ -2,15 +2,34 @@ import {
2
2
ValidationPipe as BaseValidationPipe ,
3
3
Injectable ,
4
4
} from '@nestjs/common' ;
5
+ import { ModuleRef } from '@nestjs/core' ;
6
+ import { useContainer , ValidatorOptions } from 'class-validator' ;
5
7
import { ValidationException } from './validation.exception' ;
6
8
7
9
@Injectable ( )
8
10
export class ValidationPipe extends BaseValidationPipe {
9
- constructor ( ) {
11
+ constructor ( private readonly moduleRef : ModuleRef ) {
10
12
super ( {
11
13
transform : true ,
12
14
skipMissingProperties : true ,
13
15
exceptionFactory : ( es ) => new ValidationException ( es ) ,
14
16
} ) ;
15
17
}
18
+ private readonly containerForLib = {
19
+ get : ( type : any ) => {
20
+ if ( type . name === 'CustomConstraint' ) {
21
+ // Prototype-less constraints. Null to fall back to default, which just calls constructor once.
22
+ return null ;
23
+ }
24
+ return this . moduleRef . get ( type ) ;
25
+ } ,
26
+ } ;
27
+
28
+ protected async validate (
29
+ object : object ,
30
+ validatorOptions ?: ValidatorOptions ,
31
+ ) {
32
+ useContainer ( this . containerForLib , { fallback : true } ) ;
33
+ return await super . validate ( object , validatorOptions ) ;
34
+ }
16
35
}
You can’t perform that action at this time.
0 commit comments