import {AbstractControl, AsyncValidatorFn, ValidationErrors} from '@angular/forms'; import {Observable, timer} from 'rxjs'; import {map, switchMap} from 'rxjs/operators'; import {MessageService} from 'ui-message-angular'; import {JobService} from './job.service'; export function existingJobNameValidator(jobService: JobService, messageService: MessageService): AsyncValidatorFn { return (control: AbstractControl): Promise | Observable => { return timer(500).pipe( switchMap( () => jobService.getJob(control.value).pipe( map(data => { // @ts-ignore if (data?.name === control.value) { return {message: messageService.generateMessage('JOB', 'JOB_EXISTS', 'E', control.value).msgShortText}; } else { return null; } }) ))); }; }