import 'reflect-metadata' ; import { ClassTransformOptions , instanceToPlain , plainToInstance , } from 'class-transformer' ; import { validate , ValidationError , ValidatorOptions } from 'class-validator' ; import { Type } from '@nestjs/common' ; todo see https export function getTransformed < T = any > ( plain , dto , options ? ) { return plainToInstance ( dto , plain , options ) ; } export async function validateDto ( plain , dto , validatorOptions , transformOptions ? ) { const object = getTransformed < typeof dto > ( plain , dto , transformOptions ) ; todo return validate ( object , validatorOptions ) ; } param plain param dto param validatorOptions param transformOptions returns export async function filteredByDto < T = any > ( plain , dto , validatorOptions , transformOptions ? ) { const data = getTransformed < typeof dto > ( plain , dto , transformOptions ) ; const errors = await validate ( data , validatorOptions ) ; if ( errors . length ) { return { errors , result : null } ; } const result = instanceToPlain ( data ) as T ; return { errors , result } ; } param error param prefix returns function formatErrorMessages ( error , prefix ) { const constraints = Object . keys ( error . constraints ) ; return constraints . map ( ( constraint ) => { return `${ prefix }${ error . property }: ${ constraint }` ; } ) ; } param validationErrors param prefix returns function getAllPropertiesErrors ( validationErrors , prefix = '' ) { let messages = [ ] ; for ( const error of validationErrors ) { if ( error . constraints ) { messages = messages . concat ( formatErrorMessages ( error , prefix ) ) ; } /**
         * Required default value on children because validationError.toString required it
         */ if ( ! error . children ) { error . children = [ ] ; } messages = messages . concat ( getAllPropertiesErrors ( error . children , `${ prefix }${ error . property }.` ) ) ; } return messages ; } param validationErrors returns export function getDtoErrors ( validationErrors ) { const errors = getAllPropertiesErrors ( validationErrors ) ; if ( errors . length === 0 ) { return null ; } return new Error ( errors . join ( '\n' ) ) ; }