diff --git a/README.md b/README.md index e3eb2f3..061636d 100755 --- a/README.md +++ b/README.md @@ -110,6 +110,9 @@ This allows you to perform custom and complex queries and still make use of the ## Changelog +### 0.2.1 +* Fixed issue with plugin crashing with out of range start and limit values. + ### 0.2.0 * Added custom `find` object support that can be a direct mongoose query object rather than having one built automatically. diff --git a/lib/mongoose-list.js b/lib/mongoose-list.js index c9b725c..9fbf3e6 100755 --- a/lib/mongoose-list.js +++ b/lib/mongoose-list.js @@ -10,6 +10,10 @@ module.exports = exports = function list(schema,options){ search.start = search.start || 0 search.limit = search.limit || 10 search.sort = search.sort || options.sort || '' + //sanity checks + if(search.start < 0) search.start = 0 + if(search.limit < 0) search.limit = 10 + //setup searching if('string' === typeof search.find){ var searchText = new RegExp(search.find,'i') search.find = {$or: []} diff --git a/package.json b/package.json index 83436a1..ce26400 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongoose-list", - "version": "0.2.0", + "version": "0.2.1", "description": "List plugin for mongoose that allows pagination, filtering, and sorting.", "homepage": "https://github.com/snailjs/mongoose-list", "bugs": "https://github.com/snailjs/mongoose-list/issues", diff --git a/test/mongoose-list.test.js b/test/mongoose-list.test.js index 7333ce8..2215ecd 100755 --- a/test/mongoose-list.test.js +++ b/test/mongoose-list.test.js @@ -139,5 +139,29 @@ describe('MongooseList',function(){ done() }) }) + it('should not fail with a negative start value',function(done){ + Model.list({start: -100, limit: 10},function(err,count,results){ + if(err) throw err + expect(count).to.equal(100) + expect(results.length).to.equal(10) + done() + }) + }) + it('should not fail with a negative limit value',function(done){ + Model.list({start: 50, limit: -10},function(err,count,results){ + if(err) throw err + expect(count).to.equal(100) + expect(results.length).to.equal(10) + done() + }) + }) + it('should not fail with a negative limit and start value',function(done){ + Model.list({start: -50, limit: -10},function(err,count,results){ + if(err) throw err + expect(count).to.equal(100) + expect(results.length).to.equal(10) + done() + }) + }) }) }) \ No newline at end of file