import {ArgumentError} from '../argument-error'; import {BasePredicate, testSymbol} from './base-predicate'; import {Main} from '../..'; import {PredicateOptions} from './predicate'; /** * @hidden */ export class AnyPredicate implements BasePredicate { constructor( private readonly predicates: BasePredicate[], private readonly options: PredicateOptions = {} ) {} // tslint:disable completed-docs [testSymbol](value: T, main: Main, label: string | Function) { const errors = [ 'Any predicate failed with the following errors:' ]; for (const predicate of this.predicates) { try { main(value, label, predicate); return; } catch (err) { if (value === undefined && this.options.optional === true) { return; } errors.push(`- ${err.message}`); } } throw new ArgumentError(errors.join('\n'), main); } }