import isPromise from './is-promise' ; import { ValidatorMapFunc , ValidationResult , ValidatorClass } from '../types' ; private param { Array } validations return { Promise < boolean | Any > } async function handleValidationsAsync ( validations ) { try { const result = await Promise . all ( validations ) ; const maybeFailed = result . filter ( val => typeof val !== 'boolean' && val ) ; return maybeFailed . length === 0 || maybeFailed ; } catch ( e ) { return e ; } } private param { Array } validations return { boolean | Any } function handleValidationsSync ( validations ) { const maybeFailed = validations . filter ( val => typeof val !== 'boolean' && val ) ; return maybeFailed . length === 0 || maybeFailed ; } public param { Array } validators param { String } options . key param { Any } options . newValue param { Any } options . oldValue param { Object } options . changes param { Object } options . content return { Promise | boolean | Any } export default function handleMultipleValidations ( validators , { key , newValue , oldValue , changes , content } ) { let validations = Array . from ( validators . map ( ( validator ) : ValidationResult | Promise < ValidationResult > => { const isValidatorClass = ( maybeClass ) : maybeClass is ValidatorClass => ! ! ( maybeClass as Record < string , any > ) . validate ; if ( validator && isValidatorClass ( validator ) ) { validator = validator . validate . bind ( validator ) ; } return validator ( key , newValue , oldValue , changes , content ) ; } ) ) ; if ( validations . some ( isPromise ) ) { return Promise . all ( validations ) . then ( handleValidationsAsync ) ; } return handleValidationsSync ( validations as Array < ValidationResult > ) ; }