|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {InjectionToken, ɵisObservable as isObservable, ɵisPromise as isPromise} from '@angular/core'; |
| 9 | +import {InjectionToken, ɵisObservable as isObservable, ɵisPromise as isPromise, ɵRuntimeError as RuntimeError} from '@angular/core'; |
10 | 10 | import {forkJoin, from, Observable} from 'rxjs'; |
11 | 11 | import {map} from 'rxjs/operators'; |
12 | 12 |
|
13 | 13 | import {AsyncValidator, AsyncValidatorFn, ValidationErrors, Validator, ValidatorFn} from './directives/validators'; |
| 14 | +import {RuntimeErrorCode} from './errors'; |
14 | 15 | import {AbstractControl} from './model/abstract_model'; |
15 | 16 |
|
| 17 | +const NG_DEV_MODE = typeof ngDevMode === 'undefined' || !!ngDevMode; |
| 18 | + |
16 | 19 | function isEmptyInputValue(value: any): boolean { |
17 | 20 | /** |
18 | 21 | * Check if the object is a string or array before evaluating the length attribute. |
@@ -567,10 +570,16 @@ function isPresent(o: any): boolean { |
567 | 570 | return o != null; |
568 | 571 | } |
569 | 572 |
|
570 | | -export function toObservable(r: any): Observable<any> { |
571 | | - const obs = isPromise(r) ? from(r) : r; |
572 | | - if (!(isObservable(obs)) && (typeof ngDevMode === 'undefined' || ngDevMode)) { |
573 | | - throw new Error(`Expected validator to return Promise or Observable.`); |
| 573 | +export function toObservable(value: any): Observable<any> { |
| 574 | + const obs = isPromise(value) ? from(value) : value; |
| 575 | + if (NG_DEV_MODE && !(isObservable(obs))) { |
| 576 | + let errorMessage = `Expected async validator to return Promise or Observable.`; |
| 577 | + // A synchronous validator will return object or null. |
| 578 | + if (typeof value === 'object') { |
| 579 | + errorMessage += |
| 580 | + ' Are you using a synchronous validator where an async validator is expected?'; |
| 581 | + } |
| 582 | + throw new RuntimeError(RuntimeErrorCode.WRONG_VALIDATOR_RETURN_TYPE, errorMessage); |
574 | 583 | } |
575 | 584 | return obs; |
576 | 585 | } |
|
0 commit comments