import { AbstractControl, ValidatorFn } from '@angular/forms'; import { find, get } from 'lodash'; import { BehaviorSubject } from 'rxjs'; import { ReferenceData } from '../models/reference-data'; export function autocompleteValidator(selections: BehaviorSubject): ValidatorFn { return (control: AbstractControl): { [key: string]: boolean } => { const isValid: boolean = !control.value || !!find(selections.value, control.value) // tslint:disable-next-line:no-any || !!find(selections.value, (item: any) => { return item.value === get(control, 'value.value', get(control, 'value')); }); return isValid ? undefined : {invalidSelection: true}; }; }