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

Add validation on update #1445

Merged
merged 1 commit into from
Aug 2, 2017
Merged

Add validation on update #1445

merged 1 commit into from
Aug 2, 2017

Conversation

ssh24
Copy link
Contributor

@ssh24 ssh24 commented Jul 31, 2017

Description

Currently, update/updateAll function does not perform any validation if a validation is specified on the model. This fix adds the validation check on dao for update/updateAll function and tests with all the validatable functions.

fixes #771

Copy link
Contributor

@b-admike b-admike left a comment

Choose a reason for hiding this comment

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

I suggest you check error messages when the validations fail and one property of the Employee instance that is updated when they pass on top of checking the existance of the error or the instance.

lib/dao.js Outdated
update = inst.toObject(false);

Model.applyProperties(update, inst);
Model = Model.lookupModel(update);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this line needed to do the validation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it is not. Good catch.

Copy link
Contributor

@b-admike b-admike left a comment

Choose a reason for hiding this comment

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

🚢🇮🇹

@ssh24 ssh24 force-pushed the fix/validate-update branch from 198ae85 to 8e3865a Compare July 31, 2017 20:18
@ssh24
Copy link
Contributor Author

ssh24 commented Jul 31, 2017

@slnode test please

1 similar comment
@ssh24
Copy link
Contributor Author

ssh24 commented Aug 1, 2017

@slnode test please

id: 3,
name: 'Baz',
age: 3,
}];
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. It does not have that red arrow thing.

@ssh24
Copy link
Contributor Author

ssh24 commented Aug 1, 2017

@slnode test please

lib/dao.js Outdated
doUpdate(ctx.where, ctx.data);

data = ctx.data;
var update = data;
Copy link
Contributor

Choose a reason for hiding this comment

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

why do you need to declare update here?
I mean if you continue the code while removing the update variable, it will work just fine !
Also, I see it as a redundant use of data, which is not needed

Copy link
Contributor

Choose a reason for hiding this comment

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

@loay update variable is used on line 2708

Copy link
Contributor

Choose a reason for hiding this comment

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

true and 2699 as well .. any use for adding this variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The whole point is to keep the data variable clean. If you scroll down along the lines, after making a copy of data into update, we modify update so that the data returned from the before save hook is not changed.

        data = ctx.data;
        var update = data;
        var inst = data;
        if (!(data instanceof Model)) {
          inst = new Model(data, {applyDefaultValues: false});
        }
        update = inst.toObject(false);

        // validation required
        inst.isValid(function(valid) {
          if (valid) {
            doUpdate(ctx.where, ctx.data);
          } else {
            cb(new ValidationError(inst), inst);
          }
        }, update, options);
      });

Copy link
Contributor

@loay loay Aug 2, 2017

Choose a reason for hiding this comment

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

Try with this:

        data = ctx.data;
        var inst = data;
        if (!(data instanceof Model)) {
          inst = new Model(data, {applyDefaultValues: false});
        }

        // validation required
        inst.isValid(function(valid) {
          if (valid) {
            doUpdate(ctx.where, ctx.data);
          } else {
            cb(new ValidationError(inst), inst);
          }
        }, options);
      });

You will see that it is not affected and works fine, because inst variable is assigned to be equal to data as well .. so it is either not needed at all, or the test coverage is not covering this part .. I think it is not needed, because inst variable is doing the job.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Fixed.

@ssh24 ssh24 force-pushed the fix/validate-update branch from 8e3865a to 3313cc4 Compare August 1, 2017 21:23
@ssh24
Copy link
Contributor Author

ssh24 commented Aug 2, 2017

@slnode test please

@ssh24 ssh24 force-pushed the fix/validate-update branch 2 times, most recently from 1c22e8a to 7737ab4 Compare August 2, 2017 15:15
@ssh24
Copy link
Contributor Author

ssh24 commented Aug 2, 2017

@slnode test please

Copy link
Contributor

@loay loay left a comment

Choose a reason for hiding this comment

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

LGTM
Don't forget to squash :)

@ssh24 ssh24 force-pushed the fix/validate-update branch from 7737ab4 to b1a0cb8 Compare August 2, 2017 17:24
@ssh24 ssh24 merged commit 2f7dd90 into master Aug 2, 2017
@ssh24 ssh24 deleted the fix/validate-update branch August 2, 2017 18:12
@ssh24 ssh24 removed the #review label Aug 2, 2017
@ssh24 ssh24 added this to the Sprint 41 - Apex milestone Aug 2, 2017
@bajtos
Copy link
Member

bajtos commented Aug 3, 2017

@ssh24 IMO, the tests you have modified in LoopBack (strongloop/loopback#3541) are a valid usage of update/updateAll API. If they are failing after your change here in #1445, then it's very likely that the change in juggler is actually a breaking change.

In order to support validations of partial updates (either via patchAttributes or updateAll), we need to implement partial validations that will validate only properties that are being changed.

If you think it still makes sense to run full validation in updateAll, then please add a feature flag to enable it. For example, many DAO options have options.validate that can be used to control whether validation should be run or skipped. You can also introduce model-level setting, similar to validateUpsert.

Regardless of the approach you take, all existing tests here in juggler and in loopback must keep passing without any changes. (The feature "validate updateAll" must be disabled by default for backwards compatibility.)

cc @kjdelisle

@mrfelton
Copy link
Contributor

mrfelton commented Aug 3, 2017

This change breaks https://github.com/fullcube/loopback-ds-cascade-update-mixin which uses updateAll to make partial updates to multiple models.

+1 on @bajtos comments - this is a breaking change.

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

Successfully merging this pull request may close these issues.

PersistedModel.updateAll() ignores validation
6 participants