export function countryExists()

in src/app/common/validator/country-exists.validator.ts [23:43]


export function countryExists(countryService: CountryService): AsyncValidatorFn {
  return (control: AbstractControl): Observable<any> => {
    if (!control.dirty || !control.value || control.value.length === 0) {
      return Observable.of(null);
    }

    const country = control.value;
    const displayName: string = country && typeof country === 'object' ? country.displayName : country;

    return Observable.of(displayName)
      .map(searchTerm => countryService.fetchCountries(displayName))
      .map(countries => {
        if (countries.length === 1 && countries[0].displayName === displayName) {
          return null;
        }
        return {
          invalidCountry: true
        };
      });
  };
}