Description
In 2013, the pull request #601 was created to resolve some issues such as #270.
I'd like to see this implemented, but I don't think it should even be an option. If allowNull is true, clearly you want to allow null. If it's not null, only then should the other validations happen.
Five years later, I would like to respectfully disagree. It should be an (opt-out, of course) option. Below I will present a situation where the mandatory aspect of this change is preventing me from doing what I want.
My use case is that I want to allow null only in some cases. Therefore, neither "always" (accomplished with allowNull: true
) nor "never" (accomplished by allowNull: false
) suit me, and since PR #601 made this a definitive check, any attempt of providing custom validators don't work.
myField: {
validate: {
custom(value) {
if (value === null && !foobar(this.otherField1, this.otherField2)) {
throw new Error("myField can't be null when otherField1 and otherField2 satisfy property X");
}
}
}
}
When I attempt to create the instance giving null to that field, my custom validator above does not execute at all, no matter what value allowNull
is set to.
Therefore this should be an option instead of a forced behaviour.