11import { Optional } from '../decorators' ;
22import { Injectable } from '../decorators/core' ;
3- import { ArgumentMetadata , BadRequestException , ValidationError } from '../index' ;
3+ import {
4+ ArgumentMetadata ,
5+ BadRequestException ,
6+ ValidationError ,
7+ } from '../index' ;
48import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface' ;
59import { ValidatorOptions } from '../interfaces/external/validator-options.interface' ;
610import { PipeTransform } from '../interfaces/features/pipe-transform.interface' ;
@@ -57,10 +61,13 @@ export class ValidationPipe implements PipeTransform<any> {
5761 if ( ! metatype || ! this . toValidate ( metadata ) ) {
5862 return value ;
5963 }
64+ value = this . toEmptyIfNil ( value ) ;
65+
66+ this . stripProtoKeys ( value ) ;
6067 const entity = classTransformer . plainToClass (
6168 metatype ,
62- this . toEmptyIfNil ( value ) ,
63- this . transformOptions
69+ value ,
70+ this . transformOptions ,
6471 ) ;
6572 const errors = await classValidator . validate ( entity , this . validatorOptions ) ;
6673 if ( errors . length > 0 ) {
@@ -69,8 +76,8 @@ export class ValidationPipe implements PipeTransform<any> {
6976 return this . isTransformEnabled
7077 ? entity
7178 : Object . keys ( this . validatorOptions ) . length > 0
72- ? classTransformer . classToPlain ( entity , this . transformOptions )
73- : value ;
79+ ? classTransformer . classToPlain ( entity , this . transformOptions )
80+ : value ;
7481 }
7582
7683 private toValidate ( metadata : ArgumentMetadata ) : boolean {
@@ -82,7 +89,15 @@ export class ValidationPipe implements PipeTransform<any> {
8289 return ! types . some ( t => metatype === t ) && ! isNil ( metatype ) ;
8390 }
8491
85- toEmptyIfNil < T = any , R = any > ( value : T ) : R | { } {
92+ private toEmptyIfNil < T = any , R = any > ( value : T ) : R | { } {
8693 return isNil ( value ) ? { } : value ;
8794 }
95+
96+ private stripProtoKeys ( value : Record < string , any > ) {
97+ delete value . __proto__ ;
98+ const keys = Object . keys ( value ) ;
99+ keys
100+ . filter ( key => typeof value [ key ] === 'object' )
101+ . forEach ( key => this . stripProtoKeys ( value [ key ] ) ) ;
102+ }
88103}
0 commit comments