Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow force-save of non-valid instances, add skip fields for update/v…

…alidate
  • Loading branch information...
commit 9ea8300e6e0c270cf37460bafa9b98e246729e23 1 parent f4e407d
mde authored
View
6 lib/adapters/mongo/index.js
@@ -218,7 +218,6 @@ utils.mixin(Adapter.prototype, new (function () {
}
};
-
this.update = function (data, query, callback) {
var collectionName = _collectionizeModelName(query.model.modelName)
, collection = this.client.collection(collectionName)
@@ -226,11 +225,6 @@ utils.mixin(Adapter.prototype, new (function () {
, item = data;
// Single instance-lookup by id
if (id) {
- // Bail out if instance isn't valid
- if (!item.isValid()) {
- return callback(data.errors, null);
- }
-
item = item.toData({whitelist: ['_id', 'id', 'createdAt']});
collection.update({id: id}, item, function (err, res) {
View
5 lib/adapters/riak/index.js
@@ -197,11 +197,6 @@ utils.mixin(Adapter.prototype, new (function () {
, item = data;
// Single instance-lookup by id
if (id) {
- // Bail out if instance isn't valid
- if (!item.isValid()) {
- return callback(data.errors, null);
- }
-
item = item.toData({whitelist: ['id', 'createdAt']});
item = JSON.stringify(item);
View
22 lib/index.js
@@ -390,8 +390,8 @@ utils.mixin(model, new (function () {
return callback(new Error('A bulk-save can only have new ' +
'items in it.'), null);
}
- // Bail out if any instance isn't valid
- if (!item.isValid()) {
+ // Bail out if any instance isn't valid and no force flag
+ if (!(item.isValid() || opts.force)) {
return callback(item.errors, null);
}
}
@@ -401,7 +401,7 @@ utils.mixin(model, new (function () {
saved = data._saved;
// Bail out if instance isn't valid
- if (!data.isValid()) {
+ if (!(data.isValid() || opts.force)) {
return callback(data.errors, null);
}
// Already existing instance, use update
@@ -453,6 +453,10 @@ utils.mixin(model, new (function () {
// Data may by just a bag or params, or an actual instance
if (data instanceof model.ModelBase) {
+ // Bail out if instance isn't valid
+ if (!(data.isValid() || opts.force)) {
+ return callback(data.errors, null);
+ }
data.emit('beforeUpdate');
}
@@ -609,6 +613,8 @@ utils.mixin(model, new (function () {
, validated = null
, errs = null
, camelizedKey
+ , skip = opts.skip
+ , skipKeys = {}
, val;
item.emit('beforeValidate')
@@ -639,7 +645,17 @@ utils.mixin(model, new (function () {
params.updatedAt = item.updatedAt;
}
+ if (skip) {
+ for (var i in skip) {
+ skipKeys[skip[i]] = true;
+ }
+ }
+
for (var p in properties) {
+ if (skipKeys[p]) {
+ continue;
+ }
+
validated = this.validateProperty(properties[p], params);
// If there are any failed validations, the errs param
// contains an Object literal keyed by field name, and the
Please sign in to comment.
Something went wrong with that request. Please try again.