Skip to content

fix: skip validation for undefined values while updating - #9587

Merged
sushantdhiman merged 9 commits into
sequelize:masterfrom
jony89:master
Jun 24, 2018
Merged

sushantdhiman merged 9 commits into
sequelize:masterfrom
jony89:master

Conversation

@jony89

@jony89 jony89 commented Jun 23, 2018 •

Copy link
Copy Markdown

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 #7056, after #7167 has failed since it affects the .create method (and should have not).

As stated in #7056, while assigning undefined value into a non-nullable prop and calling .update a validation Error is raised, yet while assigning undefined to nullable prop is completely ignored and not shown in the update query, due to the mapValueFieldNames function which removes undefined props as desired.
This also aligning the behavior with #9548 .

BWC checks for non-nullable props :

  • assigning null : no implication
  • assigning value : no implicaation
  • assigning undefined : no implication in final query yet no exception is raised now.

@codecov

codecov Bot commented Jun 23, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9587 into master will increase coverage by <.01%.
The diff coverage is 100%.

@sushantdhiman sushantdhiman left a comment

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.

Why don't we just filter out undefined from values input? Please add a test to verify instance.update also ignores any undefined values

@jony89

jony89 commented Jun 24, 2018

Copy link
Copy Markdown
Author

Why don't we just filter out undefined from values input?

@sushantdhiman since there is valuesUse and values I guessed they might be used for something else.

I can do even better and move valuesUse to upper scope and remove entirely the values, but again I think it was done for a reason. what do u think ?

@sushantdhiman

Copy link
Copy Markdown
Contributor

@jony89

jony89 commented Jun 24, 2018 •

Copy link
Copy Markdown
Author

@sushantdhiman yeah that what I thought at start, but there is no need for that because that's exactly what mapValueFieldNames does later (along with virtual attribtues). so we can just do it sooner ?

@sushantdhiman

Copy link
Copy Markdown
Contributor

mapValueFieldNames will convert attributes to field name which may fail in next segment as it is expected that fields to contain attributes (bad name :D)

So safest option is to filter out undefined at the top, probably use omitBy which will filter for undefined keys and create new object in one go, removing need for clone

@sushantdhiman sushantdhiman left a comment

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.

Need some work in tests and its good to go

Comment thread test/integration/model/update.test.js Outdated

it('should not check for notNull Violation for undefined values', function() {
const ownerId = 2;
let accountRowId;

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.

Get rid of accountRowId, if you do account.findOne you will directly get our target instance, as there is sync in before each.

Comment thread test/integration/model/update.test.js Outdated
name: Math.random().toString()
}).then(account => {
accountRowId = account.get('id');
const accountVal = {

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.

values from accountVal can be passed directly to this.Account.update, no need to keep values as constant.

Comment thread test/integration/model/update.test.js Outdated
});
});

it('should ignore undefined values', function() {

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.

Looks like duplicate of test above, Please add a test here https://github.com/sequelize/sequelize/blob/master/test/integration/instance/update.test.js so we can be sure instance method update also ignores undefined values

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It was one for not null props and one for the nullable props (as it might behave different) but I guess the first one is enough

Comment thread test/integration/model/update.test.js Outdated
}));
});

it('should not check for notNull Violation for undefined values', function() {

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.

should ignore undefined values without throwing not null validation

@jony89

jony89 commented Jun 24, 2018 •

Copy link
Copy Markdown
Author

Please add a test here https://github.com/sequelize/sequelize/blob/master/test/integration/instance/update.test.js so we can be sure instance method update also ignores undefined values

So I had to edit the .update non-static method as well. otherwise the test in test/integration/instance/update.test.js would fail, the value in the DB was not updated with undefined but the instance in the RAM did. I'm not sure about this case.

See if it's fine by you. and whether u like to refactor that .omitBy line.

@sushantdhiman sushantdhiman left a comment

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.

Thanks

@rpesk

rpesk commented Jun 18, 2019

Copy link
Copy Markdown

@sushantdhiman Can this be merged in v4?

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.

update method tries to insert null when receiving undefined

3 participants