Skip to content

Commit

Permalink
add missing/problematic field to the response body
Browse files Browse the repository at this point in the history
  • Loading branch information
tinacious committed Jun 6, 2017
1 parent 3157f55 commit cfccae4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion tests/validate-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const request = require('supertest');
const assert = require('assert');


describe('validate() middleware', () => {
Expand Down Expand Up @@ -37,7 +38,11 @@ describe('validate() middleware', () => {
request(server)
.post('/contacts')
.send({ firstName: 'Tina' })
.expect(400, done)
.expect(400)
.end((err, res) => {
assert.equal(res.body.field, 'email');
done();
})
});

it('returns 200 for successful body validation', (done) => {
Expand Down
3 changes: 2 additions & 1 deletion validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const validate = (schema) => (
return Joi.validate(obj, schema, (err) => {
if (err) {
const message = `${err.details[0].message.replace(/"/g, "'")} at ${err.details[0].path}`;
res.status(400).json({ message }).end();
const field = err.details[0].context.key;
res.status(400).json({ message, field }).end();
return;
}

Expand Down

0 comments on commit cfccae4

Please sign in to comment.