import { validateOrReject , ValidationError , ValidatorOptions , } from "class-validator" ; import { plainToClass } from "class-transformer" ; import AppError from "../common/AppError" ; export interface ValidationOptions extends ValidatorOptions { expectedType ? : any ; } export class Validation { protected expectedType : any ; protected validatorOptions : ValidationOptions | undefined ; constructor ( validatorOptions ? ) { this . expectedType = validatorOptions ? validatorOptions . expectedType : undefined ; this . validatorOptions = validatorOptions ; } async check ( type , params ) { const postData = this . transform ( this . expectedType || type , params ) ; await validateOrReject ( postData , this . validatorOptions ) . catch ( ( err ) => { if ( err . length > 0 && err [ 0 ] . constraints ) { const keys = Object . keys ( err [ 0 ] . constraints ) ; AppError . assert ( err [ 0 ] . constraints [ keys [ 0 ] ] ) ; } throw err ; } ) ; return postData ; } transform ( type , data ) { return plainToClass ( type , data , { enableImplicitConversion : true } ) ; } } exports . Validation = Validation ;