diff --git a/lib/router.js b/lib/router.js index a07ae87..048c16f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -20,6 +20,16 @@ var router = module.exports = function (req, res, next) { , method = req.method , path = '/' ; + + // query sugar for JSON based query strings + // eg ?q={"foo": {"bar": true}} + if(req.query && req.query.q && req.query.q[0] === '{') { + req.query = JSON.parse(req.query.q); + if (req.query.$orderby) { + req.sort = req.query.$orderby; + delete req.query.$orderby; + } + } // sanitize parts if(!parts[0]) parts.shift(); diff --git a/lib/server.js b/lib/server.js index 1107628..ba65c1c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -38,11 +38,6 @@ middleware.listen = function (callback) { // by default pause the request req.pause(); - // query sugar for JSON based query strings - // eg ?q={"foo": {"bar": true}} - if(req.query && req.query.q && req.query.q[0] === '{') { - req.query = JSON.parse(req.query.q); - } if(req.method === 'GET' || req.method === 'DELETE') return next(); @@ -146,10 +141,10 @@ middleware.listen = function (callback) { if(err instanceof Error) { res.statusCode = 500; res.send({message: err.message}); + } else { + res.statusCode = err.status || 400; + res.send(err); } - - res.statusCode = err.status || 400; - res.send(err); }); // start the server diff --git a/lib/validation.js b/lib/validation.js index fd79c30..7f2fc60 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -54,9 +54,10 @@ module.exports = function (req, res, next) { try { sanitized[key] = new Date(req.body[key]).toISOString(); } catch(e) { - sanitized[key] = false; + sanitized[key] = 'invalid date'; } - + } else if (resource.properties[key].type === 'boolean' && !req.body[key]) { + sanitized[key] = false; } else { sanitized[key] = req.body[key]; } @@ -134,7 +135,8 @@ function transform(validation) { } // rename and add human readable errors - validation.validation = validation.errors; + // validation.validation = validation.errors; + delete validation.valid; validation.errors = err; return validation; diff --git a/test/support.js b/test/support.js index b0319d3..3664c9f 100644 --- a/test/support.js +++ b/test/support.js @@ -37,8 +37,7 @@ data = { optional: false }, completed: { - type: "boolean", - optional: true + type: "boolean" }, order: { type: "number", diff --git a/test/validation.test.js b/test/validation.test.js index 5dd1ca1..ad49fcf 100644 --- a/test/validation.test.js +++ b/test/validation.test.js @@ -8,14 +8,35 @@ describe('Resource Actions', function(){ }) }) }) + + it('should support the $orderby flag', function(done) { + var query = { + $orderby: {title: 1} + }; + var queryJson = encodeURI(JSON.stringify(query)); + + todos.post({title: 'c'}, function(e1) { + todos.post({title: 'b'}, function(e2) { + todos.post({title: 'a'}, function(e3) { + todos.use('?q=' + queryJson).get(function(err, body, req, res) { + expect(body).to.exist; + expect(body.length).to.equal(3); + expect(body[0].title).to.equal('a'); + expect(body[1].title).to.equal('b'); + expect(body[2].title).to.equal('c'); + done(e1, e2, e3, err); + }); + }); + }); + }); + + }); }) describe('POST /todos', function(){ it('should return an error when provided invalid data', function(done) { todos.post({foo: 123, completed: 'flarg'}, function (err, todo, req, res) { expect(err).to.exist; - expect(err.valid).to.equal(false); - expect(err.validation).to.have.length(2); expect(err.errors).to.be.a('object'); expect(todo).to.not.exist; done();