Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
m9dfukc opened this issue Sep 19, 2012 · 7 comments
Closed

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

m9dfukc opened this issue Sep 19, 2012 · 7 comments
Labels
type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@m9dfukc
Copy link

m9dfukc commented Sep 19, 2012

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).

@tremby
Copy link
Contributor

tremby commented Feb 26, 2013

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.

@janmeier
Copy link
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.

@tremby
Copy link
Contributor

tremby commented Feb 27, 2013

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.

@janmeier
Copy link
Member

janmeier commented May 9, 2013

Implemented in #601

@janmeier janmeier closed this as completed May 9, 2013
@papb
Copy link
Member

papb commented Feb 28, 2018

@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!

@tremby
Copy link
Contributor

tremby commented Mar 5, 2018

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.

@papb
Copy link
Member

papb commented Mar 6, 2018

@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
Labels
type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
4 participants