diff --git a/lib/model.js b/lib/model.js index 8d618d0..ba85dba 100644 --- a/lib/model.js +++ b/lib/model.js @@ -409,12 +409,13 @@ function modify (obj, updateQuery) { if (!modifierFunctions[m]) { throw "Unknown modifier " + m; } - try { - keys = Object.keys(updateQuery[m]); - } catch (e) { + // Can't rely on Object.keys throwing on non objects since ES6{ + // Not 100% satisfying as non objects can be interpreted as objects but no false negatives so we can live with it + if (typeof updateQuery[m] !== 'object') { throw "Modifier " + m + "'s argument must be an object"; } + keys = Object.keys(updateQuery[m]); keys.forEach(function (k) { modifierFunctions[m](newDoc, k, updateQuery[m][k]); }); diff --git a/package.json b/package.json index 5563164..efd65c3 100644 --- a/package.json +++ b/package.json @@ -21,12 +21,12 @@ }, "dependencies": { "async": "0.2.10", - "underscore": "~1.4.4", "binary-search-tree": "0.2.4", - "mkdirp": "~0.5.1" + "mkdirp": "~0.5.1", + "underscore": "~1.4.4" }, "devDependencies": { - "chai": "1.0.x", + "chai": "^3.2.0", "mocha": "1.4.x", "request": "2.9.x", "sinon": "1.3.x", diff --git a/test/model.test.js b/test/model.test.js index 0ab78c1..7e6a3cd 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -1,6 +1,7 @@ var model = require('../lib/model') , should = require('chai').should() , assert = require('chai').assert + , expect = require('chai').expect , _ = require('underscore') , async = require('async') , util = require('util') @@ -309,9 +310,9 @@ describe('Model', function () { , updateQuery = { replace: 'done', bloup: [ 1, 8], _id: 'donttryit' } ; - (function () { + expect(function () { model.modify(obj, updateQuery); - }).should.throw(); + }).to.throw("You cannot change a document's _id"); updateQuery._id = 'keepit'; model.modify(obj, updateQuery); // No error thrown @@ -321,27 +322,27 @@ describe('Model', function () { var obj = { some: 'thing' } , updateQuery = { replace: 'me', $modify: 'metoo' }; - (function () { + expect(function () { model.modify(obj, updateQuery); - }).should.throw(); + }).to.throw("You cannot mix modifiers and normal fields"); }); it('Throw an error if trying to use an inexistent modifier', function () { var obj = { some: 'thing' } - , updateQuery = { $set: 'this exists', $modify: 'not this one' }; + , updateQuery = { $set: { it: 'exists' }, $modify: 'not this one' }; - (function () { + expect(function () { model.modify(obj, updateQuery); - }).should.throw(); + }).to.throw(/^Unknown modifier .modify/); }); it('Throw an error if a modifier is used with a non-object argument', function () { var obj = { some: 'thing' } , updateQuery = { $set: 'this exists' }; - (function () { + expect(function () { model.modify(obj, updateQuery); - }).should.throw(); + }).to.throw(/Modifier .set's argument must be an object/); }); describe('$set modifier', function () {