Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(forms): Update a Forms validator error to use RuntimeError (#46537)
Replace `new Error()` in a forms Validators function with `RuntimeError`, for better tree-shakability. Also, improve the error messages, and add documentation. PR Close #46537
- Loading branch information
Showing
7 changed files
with
54 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@name Wrong Async Validator Return Type | ||
@category forms | ||
@shortDescription Async validator must return a Promise or Observable | ||
|
||
@description | ||
Async validators must return a promise or an observable, and emit/resolve them whether the validation fails or succeeds. In particular, they must implement the [AsyncValidatorFn API](api/forms/AsyncValidator) | ||
|
||
```typescript | ||
export function isTenAsync(control: AbstractControl): | ||
Observable<ValidationErrors> | null { | ||
const v: number = control.value; | ||
if (v !== 10) { | ||
// Emit an object with a validation error. | ||
return of({ 'notTen': true, 'requiredValue': 10 }); | ||
} | ||
// Emit null, to indicate no error occurred. | ||
return of(null); | ||
} | ||
``` | ||
|
||
@debugging | ||
Did you mistakenly use a synchronous validator instead of an async validator? | ||
|
||
<!-- links --> | ||
|
||
<!-- external links --> | ||
|
||
<!-- end links --> | ||
|
||
@reviewed 2022-06-28 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1133,9 +1133,6 @@ | |
{ | ||
"name": "isObject" | ||
}, | ||
{ | ||
"name": "isObservable" | ||
}, | ||
{ | ||
"name": "isOptionsObj" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1094,9 +1094,6 @@ | |
{ | ||
"name": "isObject" | ||
}, | ||
{ | ||
"name": "isObservable" | ||
}, | ||
{ | ||
"name": "isOptionsObj" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters