2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-03-24 17:45:33 -04:00
|
|
|
import {InjectionToken, ɵisObservable as isObservable, ɵisPromise as isPromise} from '@angular/core';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Observable, forkJoin, from} from 'rxjs';
|
|
|
|
import {map} from 'rxjs/operators';
|
2017-02-23 12:53:29 -05:00
|
|
|
import {AsyncValidatorFn, ValidationErrors, Validator, ValidatorFn} from './directives/validators';
|
|
|
|
import {AbstractControl, FormControl} from './model';
|
2016-09-18 18:55:08 -04:00
|
|
|
|
2017-01-05 12:25:20 -05:00
|
|
|
function isEmptyInputValue(value: any): boolean {
|
|
|
|
// we don't check for string here so it also works with arrays
|
|
|
|
return value == null || value.length === 0;
|
2016-10-10 12:17:45 -04:00
|
|
|
}
|
2016-08-02 18:53:34 -04:00
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
2018-04-05 05:58:12 -04:00
|
|
|
* Providers for validators to be used for `FormControl`s in a form.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
|
|
|
* Provide this using `multi: true` to add validators.
|
|
|
|
*
|
2017-12-01 05:08:46 -05:00
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```typescript
|
|
|
|
* @Directive({
|
|
|
|
* selector: '[custom-validator]',
|
|
|
|
* providers: [{provide: NG_VALIDATORS, useExisting: CustomValidatorDirective, multi: true}]
|
|
|
|
* })
|
|
|
|
* class CustomValidatorDirective implements Validator {
|
|
|
|
* validate(control: AbstractControl): ValidationErrors | null {
|
|
|
|
* return {"custom": true};
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2017-01-03 19:54:46 -05:00
|
|
|
export const NG_VALIDATORS = new InjectionToken<Array<Validator|Function>>('NgValidators');
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/**
|
2018-04-05 05:58:12 -04:00
|
|
|
* Providers for asynchronous validators to be used for `FormControl`s
|
2016-06-08 18:36:24 -04:00
|
|
|
* in a form.
|
|
|
|
*
|
|
|
|
* Provide this using `multi: true` to add validators.
|
|
|
|
*
|
2018-04-05 05:58:12 -04:00
|
|
|
* See `NG_VALIDATORS` for more details.
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
2017-01-03 19:54:46 -05:00
|
|
|
export const NG_ASYNC_VALIDATORS =
|
|
|
|
new InjectionToken<Array<Validator|Function>>('NgAsyncValidators');
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-12-29 12:07:02 -05:00
|
|
|
const EMAIL_REGEXP =
|
|
|
|
/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* Provides a set of validators used by form controls.
|
|
|
|
*
|
2018-04-05 05:58:12 -04:00
|
|
|
* A validator is a function that processes a `FormControl` or collection of
|
2016-06-08 18:36:24 -04:00
|
|
|
* controls and returns a map of errors. A null map means that validation has passed.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```typescript
|
2016-06-10 14:15:59 -04:00
|
|
|
* var loginControl = new FormControl("", Validators.required)
|
2016-06-08 18:36:24 -04:00
|
|
|
* ```
|
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
|
|
|
export class Validators {
|
2017-04-06 11:41:10 -04:00
|
|
|
/**
|
|
|
|
* Validator that requires controls to have a value greater than a number.
|
2017-12-20 09:49:57 -05:00
|
|
|
*`min()` exists only as a function, not as a directive. For example,
|
|
|
|
* `control = new FormControl('', Validators.min(3));`.
|
2017-04-06 11:41:10 -04:00
|
|
|
*/
|
|
|
|
static min(min: number): ValidatorFn {
|
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
2017-06-07 23:22:23 -04:00
|
|
|
if (isEmptyInputValue(control.value) || isEmptyInputValue(min)) {
|
2017-04-06 11:41:10 -04:00
|
|
|
return null; // don't validate empty values to allow optional controls
|
|
|
|
}
|
|
|
|
const value = parseFloat(control.value);
|
2017-06-07 23:22:23 -04:00
|
|
|
// Controls with NaN values after parsing should be treated as not having a
|
|
|
|
// minimum, per the HTML forms spec: https://www.w3.org/TR/html5/forms.html#attr-input-min
|
|
|
|
return !isNaN(value) && value < min ? {'min': {'min': min, 'actual': control.value}} : null;
|
2017-04-06 11:41:10 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validator that requires controls to have a value less than a number.
|
2017-12-20 09:49:57 -05:00
|
|
|
* `max()` exists only as a function, not as a directive. For example,
|
|
|
|
* `control = new FormControl('', Validators.max(15));`.
|
2017-04-06 11:41:10 -04:00
|
|
|
*/
|
|
|
|
static max(max: number): ValidatorFn {
|
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
2017-06-07 23:22:23 -04:00
|
|
|
if (isEmptyInputValue(control.value) || isEmptyInputValue(max)) {
|
2017-04-06 11:41:10 -04:00
|
|
|
return null; // don't validate empty values to allow optional controls
|
|
|
|
}
|
|
|
|
const value = parseFloat(control.value);
|
2017-06-07 23:22:23 -04:00
|
|
|
// Controls with NaN values after parsing should be treated as not having a
|
|
|
|
// maximum, per the HTML forms spec: https://www.w3.org/TR/html5/forms.html#attr-input-max
|
|
|
|
return !isNaN(value) && value > max ? {'max': {'max': max, 'actual': control.value}} : null;
|
2017-04-06 11:41:10 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* Validator that requires controls to have a non-empty value.
|
|
|
|
*/
|
2017-02-23 12:53:29 -05:00
|
|
|
static required(control: AbstractControl): ValidationErrors|null {
|
2016-10-10 12:17:45 -04:00
|
|
|
return isEmptyInputValue(control.value) ? {'required': true} : null;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
2016-12-10 05:44:04 -05:00
|
|
|
/**
|
|
|
|
* Validator that requires control value to be true.
|
|
|
|
*/
|
2017-02-23 12:53:29 -05:00
|
|
|
static requiredTrue(control: AbstractControl): ValidationErrors|null {
|
2016-12-10 05:44:04 -05:00
|
|
|
return control.value === true ? null : {'required': true};
|
|
|
|
}
|
|
|
|
|
2016-12-29 12:07:02 -05:00
|
|
|
/**
|
|
|
|
* Validator that performs email validation.
|
|
|
|
*/
|
2017-02-23 12:53:29 -05:00
|
|
|
static email(control: AbstractControl): ValidationErrors|null {
|
2017-12-07 01:24:49 -05:00
|
|
|
if (isEmptyInputValue(control.value)) {
|
|
|
|
return null; // don't validate empty values to allow optional controls
|
|
|
|
}
|
2016-12-29 12:07:02 -05:00
|
|
|
return EMAIL_REGEXP.test(control.value) ? null : {'email': true};
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* Validator that requires controls to have a value of a minimum length.
|
|
|
|
*/
|
|
|
|
static minLength(minLength: number): ValidatorFn {
|
2017-02-23 12:53:29 -05:00
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
2016-10-10 12:17:45 -04:00
|
|
|
if (isEmptyInputValue(control.value)) {
|
|
|
|
return null; // don't validate empty values to allow optional controls
|
|
|
|
}
|
2016-12-12 14:17:12 -05:00
|
|
|
const length: number = control.value ? control.value.length : 0;
|
2016-10-06 18:12:09 -04:00
|
|
|
return length < minLength ?
|
|
|
|
{'minlength': {'requiredLength': minLength, 'actualLength': length}} :
|
2016-06-08 19:38:52 -04:00
|
|
|
null;
|
2016-06-08 18:36:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validator that requires controls to have a value of a maximum length.
|
|
|
|
*/
|
|
|
|
static maxLength(maxLength: number): ValidatorFn {
|
2017-02-23 12:53:29 -05:00
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
2016-12-12 14:17:12 -05:00
|
|
|
const length: number = control.value ? control.value.length : 0;
|
2016-10-06 18:12:09 -04:00
|
|
|
return length > maxLength ?
|
|
|
|
{'maxlength': {'requiredLength': maxLength, 'actualLength': length}} :
|
2016-06-08 19:38:52 -04:00
|
|
|
null;
|
2016-06-08 18:36:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validator that requires a control to match a regex to its value.
|
|
|
|
*/
|
2016-10-19 12:37:54 -04:00
|
|
|
static pattern(pattern: string|RegExp): ValidatorFn {
|
|
|
|
if (!pattern) return Validators.nullValidator;
|
|
|
|
let regex: RegExp;
|
|
|
|
let regexStr: string;
|
|
|
|
if (typeof pattern === 'string') {
|
2017-09-18 16:28:36 -04:00
|
|
|
regexStr = '';
|
|
|
|
|
|
|
|
if (pattern.charAt(0) !== '^') regexStr += '^';
|
|
|
|
|
|
|
|
regexStr += pattern;
|
|
|
|
|
|
|
|
if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
|
|
|
|
|
2016-10-19 12:37:54 -04:00
|
|
|
regex = new RegExp(regexStr);
|
|
|
|
} else {
|
|
|
|
regexStr = pattern.toString();
|
|
|
|
regex = pattern;
|
|
|
|
}
|
2017-02-23 12:53:29 -05:00
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
2016-10-10 12:17:45 -04:00
|
|
|
if (isEmptyInputValue(control.value)) {
|
|
|
|
return null; // don't validate empty values to allow optional controls
|
|
|
|
}
|
|
|
|
const value: string = control.value;
|
2016-10-19 12:37:54 -04:00
|
|
|
return regex.test(value) ? null :
|
|
|
|
{'pattern': {'requiredPattern': regexStr, 'actualValue': value}};
|
2016-06-08 18:36:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No-op validator.
|
|
|
|
*/
|
2017-02-23 12:53:29 -05:00
|
|
|
static nullValidator(c: AbstractControl): ValidationErrors|null { return null; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose multiple validators into a single function that returns the union
|
|
|
|
* of the individual error maps.
|
|
|
|
*/
|
2017-04-17 14:13:30 -04:00
|
|
|
static compose(validators: null): null;
|
|
|
|
static compose(validators: (ValidatorFn|null|undefined)[]): ValidatorFn|null;
|
|
|
|
static compose(validators: (ValidatorFn|null|undefined)[]|null): ValidatorFn|null {
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!validators) return null;
|
2017-04-17 14:13:30 -04:00
|
|
|
const presentValidators: ValidatorFn[] = validators.filter(isPresent) as any;
|
2016-06-08 18:36:24 -04:00
|
|
|
if (presentValidators.length == 0) return null;
|
|
|
|
|
2016-06-22 17:56:10 -04:00
|
|
|
return function(control: AbstractControl) {
|
2016-06-08 18:36:24 -04:00
|
|
|
return _mergeErrors(_executeValidators(control, presentValidators));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
static composeAsync(validators: (AsyncValidatorFn|null)[]): AsyncValidatorFn|null {
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!validators) return null;
|
2017-04-17 14:13:30 -04:00
|
|
|
const presentValidators: AsyncValidatorFn[] = validators.filter(isPresent) as any;
|
2016-06-08 18:36:24 -04:00
|
|
|
if (presentValidators.length == 0) return null;
|
|
|
|
|
2016-06-22 17:56:10 -04:00
|
|
|
return function(control: AbstractControl) {
|
2017-03-16 13:15:17 -04:00
|
|
|
const observables = _executeAsyncValidators(control, presentValidators).map(toObservable);
|
2018-02-27 17:06:06 -05:00
|
|
|
return forkJoin(observables).pipe(map(_mergeErrors));
|
2016-06-08 18:36:24 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:37:01 -05:00
|
|
|
function isPresent(o: any): boolean {
|
|
|
|
return o != null;
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
export function toObservable(r: any): Observable<any> {
|
2018-02-27 17:06:06 -05:00
|
|
|
const obs = isPromise(r) ? from(r) : r;
|
2017-03-16 13:15:17 -04:00
|
|
|
if (!(isObservable(obs))) {
|
|
|
|
throw new Error(`Expected validator to return Promise or Observable.`);
|
|
|
|
}
|
|
|
|
return obs;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
2016-06-22 17:56:10 -04:00
|
|
|
function _executeValidators(control: AbstractControl, validators: ValidatorFn[]): any[] {
|
2016-06-08 18:36:24 -04:00
|
|
|
return validators.map(v => v(control));
|
|
|
|
}
|
|
|
|
|
2016-06-22 17:56:10 -04:00
|
|
|
function _executeAsyncValidators(control: AbstractControl, validators: AsyncValidatorFn[]): any[] {
|
2016-06-08 18:36:24 -04:00
|
|
|
return validators.map(v => v(control));
|
|
|
|
}
|
|
|
|
|
2017-02-23 12:53:29 -05:00
|
|
|
function _mergeErrors(arrayOfErrors: ValidationErrors[]): ValidationErrors|null {
|
2016-10-19 12:37:54 -04:00
|
|
|
const res: {[key: string]: any} =
|
2017-02-23 12:53:29 -05:00
|
|
|
arrayOfErrors.reduce((res: ValidationErrors | null, errors: ValidationErrors | null) => {
|
2017-04-17 14:13:30 -04:00
|
|
|
return errors != null ? {...res !, ...errors} : res !;
|
2016-06-08 18:36:24 -04:00
|
|
|
}, {});
|
2016-10-03 19:46:05 -04:00
|
|
|
return Object.keys(res).length === 0 ? null : res;
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|