1- import { PipeTransform } from '../interfaces/features/pipe-transform.interface' ;
21import { ArgumentMetadata , BadRequestException } from '../index' ;
2+ import { ValidatorOptions } from '../interfaces/external/validator-options.interface' ;
3+ import { PipeTransform } from '../interfaces/features/pipe-transform.interface' ;
4+ import { loadPackage } from '../utils/load-package.util' ;
35import { isNil } from '../utils/shared.utils' ;
46import { Injectable } from './../decorators/core/component.decorator' ;
5- import { loadPackage } from '../utils/load-package.util' ;
6- import { ValidatorOptions } from '../interfaces/external/validator-options.interface' ;
77
88export interface ValidationPipeOptions extends ValidatorOptions {
99 transform ?: boolean ;
@@ -33,7 +33,10 @@ export class ValidationPipe implements PipeTransform<any> {
3333 if ( ! metatype || ! this . toValidate ( metadata ) ) {
3434 return value ;
3535 }
36- const entity = classTransformer . plainToClass ( metatype , value ) ;
36+ const entity = classTransformer . plainToClass (
37+ metatype ,
38+ this . toEmptyIfNil ( value ) ,
39+ ) ;
3740 const errors = await classValidator . validate ( entity , this . validatorOptions ) ;
3841 if ( errors . length > 0 ) {
3942 throw new BadRequestException ( errors ) ;
@@ -53,4 +56,8 @@ export class ValidationPipe implements PipeTransform<any> {
5356 const types = [ String , Boolean , Number , Array , Object ] ;
5457 return ! types . find ( t => metatype === t ) && ! isNil ( metatype ) ;
5558 }
59+
60+ toEmptyIfNil < T = any , R = any > ( value : T ) : R | { } {
61+ return isNil ( value ) ? { } : value ;
62+ }
5663}
0 commit comments