import { ValidationError } from 'class-validator-multi-lang' ; import { ShortValidationErrors } from '../types/short-validation-errors' ; const cloneDeep = require ( 'lodash.clonedeep' ) ; const mergeWith = require ( 'lodash.mergewith' ) ; export function transformValidationErrors ( errors ) { const customErrors = { } ; errors . forEach ( ( error ) => { if ( error && error . constraints !== undefined ) { Object . keys ( error . constraints ) . forEach ( ( key ) => { if ( ! customErrors [ error . property ] ) { customErrors [ error . property ] = [ ] ; } if ( error . constraints && ( customErrors [ error . property ] as string [ ] ) . indexOf ( error . constraints [ key ] ) === - 1 ) { ( customErrors [ error . property ] as string [ ] ) . push ( error . constraints [ key ] ) ; } } ) ; } if ( error . children !== undefined && error . children . length ) { customErrors [ error . property ] = transformValidationErrors ( error . children ) ; } } ) ; return customErrors ; } export function mergeErrors ( externalErrors ? , validationErrors ? ) { const clonedExternalErrors = cloneDeep ( externalErrors ) ; return mergeWith ( clonedExternalErrors , validationErrors , ( objValue , srcValue ) => { if ( canMerge ( ) ) { return objValue . concat ( srcValue ) ; } function canMerge ( ) { return ( Array . isArray ( objValue ) && Array . isArray ( srcValue ) && ! objValue . find ( ( objItem ) => srcValue . includes ( objItem ) ) ) ; } } ) ; }