Skip to content

Commit

Permalink
skip body check on GET and HEAD.
Browse files Browse the repository at this point in the history
Closes #1.
  • Loading branch information
arb committed Jun 2, 2016
1 parent 65b139f commit e9d658b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ module.exports = (schema) => {
Insync.apply(validateSource, 'headers'),
Insync.apply(validateSource, 'params'),
Insync.apply(validateSource, 'query'),
Insync.apply(validateSource, 'body')
(next) => {
const method = req.method.toLowerCase();
if (method === 'get' || method === 'head') {
return next();
}
validateSource('body', next);
}
], next);
};
};
29 changes: 26 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Code = require('code');
const Lab = require('lab');
const Joi = require('joi');
const Lab = require('lab');
const Celebrate = require('../lib');

const lab = exports.lab = Lab.script();
Expand Down Expand Up @@ -84,7 +84,8 @@ describe('Celebrate Middleware', () => {
body: {
first: 'john',
last: 123
}
},
method: 'POST'
}, null, (err) => {
expect(err).to.exist();
expect(err.isJoi).to.be.true();
Expand Down Expand Up @@ -133,7 +134,8 @@ describe('Celebrate Middleware', () => {
first: 'john',
last: 'doe'
},
query: undefined
query: undefined,
method: 'POST'
};
const middleware = Celebrate({
body: {
Expand All @@ -155,4 +157,25 @@ describe('Celebrate Middleware', () => {
done();
});
});

it('does not validate req.body if the method is "GET" or "HEAD"', (done) => {
const middleware = Celebrate({
body: {
first: Joi.string().required(),
last: Joi.string(),
role: Joi.number().integer()
}
});

middleware({
body: {
first: 'john',
last: 123
},
method: 'GET'
}, null, (err) => {
expect(err).to.be.undefined();
done();
});
});
});

0 comments on commit e9d658b

Please sign in to comment.