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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With