diff --git a/paperwork.js b/paperwork.js index 30006dc..899a98f 100644 --- a/paperwork.js +++ b/paperwork.js @@ -131,16 +131,18 @@ module.exports = function (spec, blob, done) { done(null, validated); }; -module.exports.accept = function (spec) { +function middleware(spec, inspect) { return function (req, res, next) { - if (!req.body) - throw new Error('express.bodyParser() not enabled'); + var component = inspect || 'body'; + if (!req[component]) + throw new Error('parser middleware for request ' + component + ' not enabled'); - var visitor = new Visitor(), - validated = paperwork(spec, req.body, visitor); + var visitor = new Visitor(component), + validated = paperwork(spec, req[component], visitor); if (!visitor.hasErrors()) { - req.body = validated; + if (component === 'body') + req.body = validated; return next(); } @@ -151,10 +153,15 @@ module.exports.accept = function (spec) { status: 'bad_request', reason: 'Body did not satisfy requirements', errors: visitor.errors - } + }; res.end(JSON.stringify(response)); }; -}; +} + +module.exports.accept = middleware; +module.exports.json = function(spec) { return middleware(spec); }; +module.exports.query = function(spec) { return middleware(spec, 'query'); }; +module.exports.url = function(spec) { return middleware(spec, 'params'); }; module.exports.validator = function (spec) { return module.exports.bind(null, spec); diff --git a/test/test.js b/test/test.js index 7d63628..b27697f 100644 --- a/test/test.js +++ b/test/test.js @@ -355,6 +355,62 @@ describe('Paperwork', function () { fakeRes.end.called.should.equal(true, 'end() should have been called'); fakeRes._getData().should.match(/bad_request/); }); + + describe('custom validation middleware', function() { + var cookieSpec = { + 'connect.sid': String + }; + + it('should validate with custom middleware', function(done) { + var fakeReq = { + cookies: { + 'connect.sid': 'mellon', + } + }; + var fakeRes = httpMocks.createResponse(); + + paperwork.accept(cookieSpec, 'cookies')(fakeReq, fakeRes, function() { + should.exist(fakeReq.cookies); + fakeReq.cookies.should.have.property('connect.sid', 'mellon'); + done(); + }); + }); + + it('should invalidate with custom middleware', function(done) { + var fakeReq = { + cookies: {} + }; + var fakeRes = httpMocks.createResponse(); + sinon.spy(fakeRes, 'end'); + + + paperwork.accept(cookieSpec, 'cookies')(fakeReq, fakeRes, function() { + done(new Error('done() should not have been called')); + }); + + fakeRes.statusCode.should.equal(400, 'status code should be 400'); + fakeRes.end.called.should.equal(true, 'end() should have been called'); + fakeRes._getData().should.match(/bad_request/); + done(); + }); + + it('does not remove extra fields', function(done) { + var fakeReq = { + cookies: { + 'connect.sid': 'mellon', + extra: true + } + }; + var fakeRes = httpMocks.createResponse(); + + paperwork.accept(cookieSpec, 'cookies')(fakeReq, fakeRes, function() { + should.exist(fakeReq.cookies); + should.exist(fakeReq.cookies.extra); + done(); + }); + }); + + }); }); describe('.validator()', function () {