Skip to content

option for ignore validation on fields with 'allowNull=true' #270

Closed

Description

@m9dfukc

Would be nice to have an option to ignore the validation settings if the field value is null with the allowNull parameter set.

Another way would be come up with custom validation methods (for example 'allowNullOrIsEmail') but that would mean that we need access to the other validation functions (to reuse em).

Activity

  1. tremby commented on Feb 26, 2013

    @tremby
    Contributor

    This is the same as #118, and it's not clear why that one is closed. 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. As for the other issue I commented on tonight, I'll offer a small donation as bounty if this is addressed soon.

  2. janmeier commented on Feb 26, 2013

    @janmeier
    Member

    Agree, this should be implemented. I think we can safely infer that if a field allows null it should not be validated if it contains a null value.

  3. tremby commented on Feb 27, 2013

    @tremby
    Contributor

    So, for instance, if a string field has allowNull: true, but also validate: len: 1, an empty string "" should fail, a string "string" should pass, and null should pass.

  4. janmeier commented on May 9, 2013

    @janmeier
    Member

    Implemented in #601

  5. papb commented on Feb 28, 2018

    @papb
    Member

    @tremby and @janmeier Hello and sorry to bring this up after so many years, but I would like to point out that this should be an opt-out option instead of a forced behaviour.

    @tremby said: "I don't think it should even be an option. If allowNull is true, clearly you want to allow null."

    This is being a problem for me, because I want to allow null depending on other fields:

    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");
          }
        }
      }
    }

    But with the changes made in #601, if I do allowNull: true, it is always allowed. If I do allowNull: false, it is never allowed, and I can't escape this... (if I omit it, it defaults to true)

    This should be an option (that comes enabled, of course), so I can disable it for myField in my case... It's very specific but maybe others will encounter the same problem.

    What do you think?

    Thanks a lot!

  6. tremby commented on Mar 5, 2018

    @tremby
    Contributor

    I hadn't considered a use case like that. But I am going to bow out of discussion -- I haven't used Sequelize in years.

  7. papb commented on Mar 6, 2018

    @papb
    Member

    @tremby Thank you for responding! No problem.

    I opened a new issue about this: #9143

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

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions