Permalink
Browse files

[api] Respect resource schema validation when performing updates.

  • Loading branch information...
1 parent 0b37c43 commit d43ebf8ee6e39d2b71ccd5f3358d8e5ff182942d @Marak Marak committed Jun 14, 2012
Showing with 10 additions and 0 deletions.
  1. +10 −0 lib/resourceful/resource.js
@@ -262,6 +262,16 @@ Resource.update = function (id, obj, callback) {
obj.mtime = Date.now();
}
+ var validate = this.prototype.validate({ _properties: obj }, this.schema);
+
+ if (!validate.valid) {
+ this.emit('error', validate.errors);
+ if (callback) {
+ callback(validate.errors);
+ }
+ return;
+ }
+
return id
? this._request('update', id, obj, callback)
: callback && callback(new Error('key is undefined'));

5 comments on commit d43ebf8

@mmalecki
Contributor

This is wrong. We often perform partial updates (where obj parameter has just few - not all - keys). Example:

user.update({ password: hash }, function (err, res) {
  // ...
});

And since user model expects email property, this update fails, even tho it's completely valid.

@Marak
Contributor
Marak commented on d43ebf8 Jun 18, 2012

Ok. When you call:

user.update({ email: INVALID_EMAIL }, function (err, res) {
  // ...
});

This will cause the save to happen, despite the fact the email is invalid.

Please read this issue: #80

@mmalecki
Contributor

OK, let's make it run validations only on supplied properties. What do you think?

@Marak
Contributor
Marak commented on d43ebf8 Jun 19, 2012

Sounds like the right idea.

I'd be super appreciative if you could start writing the tests mentioned in #80.

@indexzero
Member

@Marak @mmalecki I've implemented this functionality correctly by creating a partial schema containing only a subset of properties being updated and added associated tests in b75964c

Please sign in to comment.