Skip to content

Commit

Permalink
perf(forms): avoid direct references to the Validators class (angul…
Browse files Browse the repository at this point in the history
…ar#41189)

Currently the `Validators` class contains a number of static methods that represent different validators as well as some helper methods. Since class methods are not tree-shakable, any reference to the `Validator` class retains all of its methods (even if you've used just one).

This commit refactors the code to extract the logic into standalone functions and use these functions in the code instead of referencing them via `Validators` class. That should make the code more tree-shakable. The `Validators` class still retains its structure and calls these standalone methods internally to keep this change backwards-compatible.

PR Close angular#41189
  • Loading branch information
AndrewKushnir committed Mar 15, 2021
1 parent da5f5a4 commit da2ec28
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,12 @@
{
"name": "collectStylingFromTAttrs"
},
{
"name": "compose"
},
{
"name": "composeAsync"
},
{
"name": "composeAsyncValidators"
},
Expand Down Expand Up @@ -1346,6 +1352,9 @@
{
"name": "notFoundValueOrThrow"
},
{
"name": "nullValidator"
},
{
"name": "observable"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@
{
"name": "DomSharedStylesHost"
},
{
"name": "EMAIL_REGEXP"
},
{
"name": "EMPTY_ARRAY"
},
Expand Down Expand Up @@ -569,9 +566,6 @@
{
"name": "VE_ViewContainerRef"
},
{
"name": "Validators"
},
{
"name": "Version"
},
Expand Down Expand Up @@ -1052,9 +1046,6 @@
{
"name": "hasTagAndTypeMatch"
},
{
"name": "hasValidLength"
},
{
"name": "hostReportError"
},
Expand Down Expand Up @@ -1136,9 +1127,6 @@
{
"name": "isDirectiveHost"
},
{
"name": "isEmptyInputValue"
},
{
"name": "isForwardRef"
},
Expand Down
20 changes: 10 additions & 10 deletions packages/forms/src/directives/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Directive, forwardRef, Input, OnChanges, SimpleChanges, StaticProvider}
import {Observable} from 'rxjs';

import {AbstractControl} from '../model';
import {NG_VALIDATORS, Validators} from '../validators';
import {emailValidator, maxLengthValidator, minLengthValidator, NG_VALIDATORS, nullValidator, patternValidator, requiredTrueValidator, requiredValidator} from '../validators';


/**
Expand Down Expand Up @@ -181,7 +181,7 @@ export class RequiredValidator implements Validator {
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this.required ? Validators.required(control) : null;
return this.required ? requiredValidator(control) : null;
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ export class CheckboxRequiredValidator extends RequiredValidator {
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this.required ? Validators.requiredTrue(control) : null;
return this.required ? requiredTrueValidator(control) : null;
}
}

Expand Down Expand Up @@ -289,7 +289,7 @@ export class EmailValidator implements Validator {
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this._enabled ? Validators.email(control) : null;
return this._enabled ? emailValidator(control) : null;
}

/**
Expand Down Expand Up @@ -360,7 +360,7 @@ export const MIN_LENGTH_VALIDATOR: any = {
host: {'[attr.minlength]': 'minlength ? minlength : null'}
})
export class MinLengthValidator implements Validator, OnChanges {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;

/**
Expand Down Expand Up @@ -396,7 +396,7 @@ export class MinLengthValidator implements Validator, OnChanges {
}

private _createValidator(): void {
this._validator = Validators.minLength(
this._validator = minLengthValidator(
typeof this.minlength === 'number' ? this.minlength : parseInt(this.minlength, 10));
}
}
Expand Down Expand Up @@ -438,7 +438,7 @@ export const MAX_LENGTH_VALIDATOR: any = {
host: {'[attr.maxlength]': 'maxlength ? maxlength : null'}
})
export class MaxLengthValidator implements Validator, OnChanges {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;

/**
Expand Down Expand Up @@ -473,7 +473,7 @@ export class MaxLengthValidator implements Validator, OnChanges {
}

private _createValidator(): void {
this._validator = Validators.maxLength(
this._validator = maxLengthValidator(
typeof this.maxlength === 'number' ? this.maxlength : parseInt(this.maxlength, 10));
}
}
Expand Down Expand Up @@ -518,7 +518,7 @@ export const PATTERN_VALIDATOR: any = {
host: {'[attr.pattern]': 'pattern ? pattern : null'}
})
export class PatternValidator implements Validator, OnChanges {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;

/**
Expand Down Expand Up @@ -553,6 +553,6 @@ export class PatternValidator implements Validator, OnChanges {
}

private _createValidator(): void {
this._validator = Validators.pattern(this.pattern);
this._validator = patternValidator(this.pattern);
}
}

0 comments on commit da2ec28

Please sign in to comment.