11import { Optional } from '../decorators' ;
2- import { ArgumentMetadata , BadRequestException } from '../index' ;
2+ import { Injectable } from '../decorators/core/component.decorator' ;
3+ import {
4+ ArgumentMetadata ,
5+ BadRequestException ,
6+ ValidationError ,
7+ } from '../index' ;
38import { ValidatorOptions } from '../interfaces/external/validator-options.interface' ;
49import { PipeTransform } from '../interfaces/features/pipe-transform.interface' ;
510import { loadPackage } from '../utils/load-package.util' ;
611import { isNil } from '../utils/shared.utils' ;
7- import { Injectable } from '../decorators/core/component.decorator' ;
812
913export interface ValidationPipeOptions extends ValidatorOptions {
1014 transform ?: boolean ;
1115 disableErrorMessages ?: boolean ;
16+ exceptionFactory ?: ( errors : ValidationError [ ] ) => any ;
1217}
1318
1419let classValidator : any = { } ;
@@ -19,13 +24,20 @@ export class ValidationPipe implements PipeTransform<any> {
1924 protected isTransformEnabled : boolean ;
2025 protected isDetailedOutputDisabled ?: boolean ;
2126 protected validatorOptions : ValidatorOptions ;
27+ protected exceptionFactory : ( errors : ValidationError [ ] ) => any ;
2228
2329 constructor ( @Optional ( ) options ?: ValidationPipeOptions ) {
2430 options = options || { } ;
2531 const { transform, disableErrorMessages, ...validatorOptions } = options ;
2632 this . isTransformEnabled = ! ! transform ;
2733 this . validatorOptions = validatorOptions ;
2834 this . isDetailedOutputDisabled = disableErrorMessages ;
35+ this . exceptionFactory =
36+ options . exceptionFactory ||
37+ ( errors =>
38+ new BadRequestException (
39+ this . isDetailedOutputDisabled ? undefined : errors ,
40+ ) ) ;
2941
3042 const loadPkg = pkg => loadPackage ( pkg , 'ValidationPipe' ) ;
3143 classValidator = loadPkg ( 'class-validator' ) ;
@@ -43,15 +55,13 @@ export class ValidationPipe implements PipeTransform<any> {
4355 ) ;
4456 const errors = await classValidator . validate ( entity , this . validatorOptions ) ;
4557 if ( errors . length > 0 ) {
46- throw new BadRequestException (
47- this . isDetailedOutputDisabled ? undefined : errors ,
48- ) ;
58+ throw this . exceptionFactory ( errors ) ;
4959 }
5060 return this . isTransformEnabled
5161 ? entity
5262 : Object . keys ( this . validatorOptions ) . length > 0
53- ? classTransformer . classToPlain ( entity )
54- : value ;
63+ ? classTransformer . classToPlain ( entity )
64+ : value ;
5565 }
5666
5767 private toValidate ( metadata : ArgumentMetadata ) : boolean {
0 commit comments