angular-docs-cn/packages/forms/src/directives/normalize_validator.ts

28 lines
874 B
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
2016-06-08 15:36:24 -07:00
import {AbstractControl} from '../model';
import {AsyncValidator, AsyncValidatorFn, Validator, ValidatorFn} from './validators';
2016-06-08 15:36:24 -07:00
export function normalizeValidator(validator: ValidatorFn | Validator): ValidatorFn {
2016-11-11 10:47:34 -08:00
if ((<Validator>validator).validate) {
2016-06-08 15:36:24 -07:00
return (c: AbstractControl) => (<Validator>validator).validate(c);
} else {
return <ValidatorFn>validator;
}
}
export function normalizeAsyncValidator(validator: AsyncValidatorFn | AsyncValidator):
AsyncValidatorFn {
if ((<AsyncValidator>validator).validate) {
return (c: AbstractControl) => (<AsyncValidator>validator).validate(c);
2016-06-08 15:36:24 -07:00
} else {
return <AsyncValidatorFn>validator;
}
}