-
Notifications
You must be signed in to change notification settings - Fork 362
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
Add validation on update #1445
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚢🇮🇹
198ae85
to
8e3865a
Compare
@slnode test please |
1 similar comment
@slnode test please |
id: 3, | ||
name: 'Baz', | ||
age: 3, | ||
}]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: newline.
There was a problem hiding this comment.
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.
@slnode test please |
lib/dao.js
Outdated
doUpdate(ctx.where, ctx.data); | ||
|
||
data = ctx.data; | ||
var update = data; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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);
});
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Fixed.
8e3865a
to
3313cc4
Compare
@slnode test please |
1c22e8a
to
7737ab4
Compare
@slnode test please |
There was a problem hiding this 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 :)
7737ab4
to
b1a0cb8
Compare
@ssh24 IMO, the tests you have modified in LoopBack (strongloop/loopback#3541) are a valid usage of In order to support validations of partial updates (either via If you think it still makes sense to run full validation in 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 |
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. |
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 forupdate
/updateAll
function and tests with all the validatable functions.fixes #771