Modifiers arguments must be objects

pull/2/head
Louis Chatriot 12 years ago
parent 6586c7f81a
commit 56ee1227ab
  1. 26
      lib/model.js
  2. 9
      test/model.test.js

@ -125,8 +125,20 @@ function deepCopy (obj) {
}
/**
* Set field to value in a model
* Create it if it doesn't exist
* @param {Object} obj The model to set a field for
* @param {Object} mod mod is of the form { k: v }. k is the field, v its value
* k can contain dots, in that case that means we will set a subfield
*/
modifierFunctions.$set = function (obj, mod) {
var field, value;
};
modifierFunctions.$inc = function () {};
modifierFunctions.$set = function () {};
/**
@ -154,7 +166,19 @@ function modify (obj, updateQuery) {
// Apply modifiers
modifiers = _.uniq(keys);
modifiers.forEach(function (m) {
var keys;
if (!modifierFunctions[m]) { throw "Unknown modifier " + m; }
try {
keys = Object.keys(updateQuery[m]);
} catch (e) {
throw "Modifier " + m + "'s argument must be an object";
}
keys.forEach(function (k) {
modifierFunctions[m](obj, k, updateQuery[m][k]);
});
});
}

@ -240,6 +240,15 @@ describe('Model', function () {
}).should.throw();
});
it('Throw an error if a modifier is used with a non-object argument', function () {
var obj = { some: 'thing' }
, updateQuery = { $set: 'this exists' };
(function () {
model.modify(obj, updateQuery);
}).should.throw();
});
}); // ==== End of 'Modifying documents' ==== //
});

Loading…
Cancel
Save