Skip to content

change: remove deprecation from notNull for custom messages / enforce use of allowNull with this validator - #9549

Merged
sushantdhiman merged 3 commits into
masterfrom
fix-1500
Jun 19, 2018
Merged

sushantdhiman merged 3 commits into
masterfrom
fix-1500

Conversation

@sushantdhiman

Copy link
Copy Markdown
Contributor

…lowNull usage with it

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Fixes #1500

Sequelize can customize allowNull error messages with notNull validator, but validator throw error when value is not null due to deprecation for notNull.

I have removed that deprecation, it was only introduced to enforce users to use allowNull over notNull, I have added some checks that will only allow use of notNull with allowNull: false.

So overall if users want to prevent null values they can as usual use allowNull: false, but if they want to customize that error message, they can use notNull.msg

@codecov

codecov Bot commented Jun 17, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9549 into master will increase coverage by 0.01%.
The diff coverage is 87.5%.

Comment thread lib/utils/validator-extras.js Outdated
validator.notNull = function() {
throw new Error('Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"');
validator.notNull = function(val) {
return [null, undefined].indexOf(val) === -1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since v5 targets NodeJS 6+ it's now possible to return [null, undefined].includes(val)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I just copied this code from old PR, will change

@sushantdhiman
sushantdhiman merged commit 7525ef0 into master Jun 19, 2018
@sushantdhiman
sushantdhiman deleted the fix-1500 branch June 19, 2018 03:00
@dydome

dydome commented Jun 22, 2018

Copy link
Copy Markdown

@sushantdhiman when those changes will be online with the latest npm varsion?

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

Next beta release 5.0.0-beta.9 this weekend

Comment thread lib/model.js
}

if (attribute.allowNull !== false && _.get(attribute, 'validate.notNull')) {
throw new Error(`Invalid definition for "${this.name}.${name}", "notNull" validator is only allowed with "allowNull:false"`);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great change for developer UX 👍 @sushantdhiman

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants