diff --git a/lib/list.js b/lib/list.js index 93bcb755..6a639f09 100644 --- a/lib/list.js +++ b/lib/list.js @@ -13,7 +13,9 @@ function List(items, itemType, parent) { try { items = JSON.parse(items); } catch (e) { - throw new Error(util.format('could not create List from JSON string: %j', items)); + var err = new Error(util.format('could not create List from JSON string: %j', items)); + err.statusCode = 400; + throw err; } } @@ -22,7 +24,9 @@ function List(items, itemType, parent) { items = items || []; if (!Array.isArray(items)) { - throw new Error(util.format('Items must be an array: %j', items)); + var err = new Error(util.format('Items must be an array: %j', items)); + err.statusCode = 400; + throw err; } if(!itemType) { diff --git a/test/datatype.test.js b/test/datatype.test.js index 3c542e2e..278f368d 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -23,6 +23,32 @@ describe('datatypes', function () { }); }); + it('should return 400 when property of type array is set to string value', + function (done) { + var myModel = db.define('myModel', { + list: { type: ['object'] } + }); + + (function(){ + myModel.create({ list: 'This string will crash the server' }); + }).should.throw({ statusCode: 400 }); + + done(); + }); + + it('should return 400 when property of type array is set to object value', + function (done) { + var myModel = db.define('myModel', { + list: { type: ['object'] } + }); + + (function(){ + myModel.create({ list: { key: 'This string will crash the server' } }); + }).should.throw({ statusCode: 400 }); + + done(); + }); + it('should keep types when get read data from db', function (done) { var d = new Date, id;