import handleMultipleValidations from './handle-multiple-validations' ; import isPromise from './is-promise' ; import isObject from './is-object' ; import { ValidatorAction , ValidatorMapFunc , ValidatorClass , ValidationResult , ValidatorMap } from '../types' ; import get from './get-deep' ; method param validationMap export default function lookupValidator ( validationMap ) { return ( { key , newValue , oldValue , changes , content } ) => { const validations = validationMap || { } ; let validator = get ( validations , key ) ; const isValidatorClass = ( maybeClass ) : maybeClass is ValidatorClass => ! ! ( maybeClass as Record < string , any > ) . validate ; if ( validator && isValidatorClass ( validator ) ) { validator = validator . validate . bind ( validator ) ; } if ( ! validator || isObject ( validator ) ) { return true ; } let validation ; if ( Array . isArray ( validator ) ) { validation = handleMultipleValidations ( validator , { key , newValue , oldValue , changes , content } ) ; } else { validation = ( validator as ValidatorMapFunc ) ( key , newValue , oldValue , changes , content ) ; } return isPromise ( validation ) ? ( validation as Promise < ValidationResult > ) . then ( result => { return result ; } ) : validation ; } ; }