Async Validator Throw Expected validator to return Promise or Observable

Kevin - Dhinesh babu picture Kevin - Dhinesh babu · Jul 8, 2017 · Viewed 17.7k times · Source

I have tried to confirm the password with password value. I have done as per Async validator standard. But I am wondering it's not working and throw me the following error. Please tell anyone how to solve this error.

Expected validator to return Promise or Observable.

Here is my code.

Call Validators:

cPass: ['', Validators.compose([
  Validators.required, 
  Validators.maxLength(32),
  Validators.minLength(10)
]),
  this.validPassword.bind(this)
]

Custom Validation funciton:

validPassword(control: AbstractControl) {            
  const isEqual = Observable.of(this.password == control.value);
  return isEqual ? { valid : true } : null;         
}

Answer

developer033 picture developer033 · Jul 9, 2017

The error speaks for itself:

Expected validator to return Promise or Observable.

You're returning object|null in your function.

Just change it to:

validPassword(control: AbstractControl) {
  return observableOf('12345678910' === control.value).pipe(
    map(result => result ? { invalid: true } : null)
  );
}

STABKBLITZ DEMO