Skip to content

Do not skip custom validators if value is null and allowNull is true #9143

Closed

Description

@papb

In 2013, the pull request #601 was created to resolve some issues such as #270.

@tremby said (source)

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.

Activity

  1. awdnf commented on Mar 20, 2018

    @awdnf

    +1

  2. damianijr commented on Mar 20, 2018

    @damianijr

    +1

  3. added
    status: in discussion
    on Apr 7, 2018
  4. sushantdhiman commented on Apr 7, 2018

    @sushantdhiman
    Contributor

    I think atleast no validate state should not be tied to allowNull, validators defined on attribute must run

  5. dylandechant commented on Oct 31, 2018

    @dylandechant

    this also fits a use case i have

  6. papb commented on Jan 2, 2019

    @papb
    MemberAuthor

    After further thought on this issue and further investigations on sequelize's code:

    Current sequelize behavior is as follows

    If the value of an attribute is null, no validators defined on that attribute run at all. Also, in that case, if allowNull is false, a ValidationError is thrown.

    How I think it should be

    If the value of an attribute is null and allowNull is false, the behavior shouldn't change, i.e., no validators defined on that attribute should run at all and a ValidationError should be thrown. To me this makes sense because running any other validators would be a waste anyway, because the validation is already going to fail.

    But if the value is null and allowNull is true:

    • No built-in validators defined on that attribute should run, because none of the build-in validators make sense with a null value (so for this part, the behavior won't change).
    • Custom validators defined on that attribute should run.

    Because of the last bullet point, this change would consist of a breaking change, because a few people's code could start throwing ValidationErrors where they previously didn't. Regardless, in my humble opinion is this change is deserved, because the current behavior of unexpectedly ignoring user defined validators is very confusing and restrictive (as I can't define my conditional validation as I wanted in the original post).

    I will open a PR implementing the above changes soon (including documentation changes, of course).

  7. changed the title [-]Feature request: change feature made in PR #601 from mantadory to optional (opt-out, i.e. active by default)[/-] [+]Do not skip custom validators if value is null and allowNull is true[/+] on Jan 2, 2019
  8. dalepo commented on Jul 9, 2019

    @dalepo

    I know I'm late for this, but I think this PR does not make any sense. If you have a special case to validate your own model, then treat it as a special case with a model validation.
    This broke most of the models from our project, we have around ~150 models. Because of this, we probably did a mistake on upgrading to v5.

  9. papb commented on Jul 10, 2019

    @papb
    MemberAuthor

    Hi @dalepo, I'm sorry to hear that...

    Would you be willing to update your validators or will you end up rolling back to v4?

    As I said in the upgrade guide:

    To avoid problems when upgrading, please check all your custom validators defined per attribute, where allowNull is true, and make sure all these validators behave correctly when the value is null.

    In other words, you can upgrade by adding a if (value === null) return; in the first line of each of your validators... 150 is a lot though, I feel your pain... Good luck.

  10. dalepo commented on Jul 10, 2019

    @dalepo

    Hi @papb we probably are gonna roll back.

    Adding if (value === null) return; would be a good solution, but sadly these functions are used in other modules and tests.
    That or probably do a wrapper for each function.

  11. papb commented on Jul 10, 2019

    @papb
    MemberAuthor

    Of course you know your situation more than I do, but why would the if (value === null) return; break anything? Do your other modules and tests pass null explicitly to the validator? And return something other than undefined?

  12. ephys commented on May 13, 2022

    @ephys
    Member

    This has been merged a long time ago now but I must say ideologically I agree with @dalepo. Attribute validators should only rely on themselves, and should not run if the attribute is null.
    If a check depends on multiple attributes, imo it belongs in model validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: in discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions