Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of Validators.nullValidator(control: AbstractControl)?

I'm working on form validation in Angular and while reading the API I came across nullValidator. It says it's a "Validator that performs no operation.", which sounds to me like it is a validator that doesn't actually do anything. When I checked the code on github, this turned out to be correct. It just returns null.

So my question is, why does nullValidator exist? What purpose does it serve?

like image 252
Friso Avatar asked Oct 25 '18 07:10

Friso


1 Answers

One use case comes to mind. Imagine that you have function that should return ValidatorFn. You might be in situation that in some cases it is reasonable to return validator that does nothing. E.g.:

getValidator(): ValidatorFn {
    if(someCondition) {
        return realValidator;
    }
    return Validators.nullValidator;
}

It also represents null object pattern which prevents null pointers/null guards in your code as you can have always some validator even when it does nothing.

like image 119
Ludevik Avatar answered Sep 23 '22 00:09

Ludevik