Skip to content

Commit

Permalink
Merge pull request #11 from tinacious/upgrade-joi
Browse files Browse the repository at this point in the history
Upgrade Joi
  • Loading branch information
tinacious authored Apr 17, 2020
2 parents b2229ea + bc2edc9 commit 5839040
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
53 changes: 31 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-joi-validate",
"version": "2.0.1",
"version": "2.1.0",
"description": "Simple route validation middleware for Express using Joi",
"main": "validate.js",
"scripts": {
Expand Down Expand Up @@ -33,6 +33,6 @@
"handlebars": "v4.1.2"
},
"dependencies": {
"@hapi/joi": "^v15.0.3"
"@hapi/joi": "^v17.1.1"
}
}
16 changes: 8 additions & 8 deletions tests/validate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,51 @@ describe('validate() middleware', () => {
.get('/posts')
.expect(400, done)
});

it('returns 200 for successful query validation', (done) => {
request(server)
.get('/posts?limit=10')
.expect(200, 'ok', done)
});
});

describe('params validation', () => {
it('returns 400 for failed params validation', (done) => {
request(server)
.get('/contacts/tina')
.expect(400, done)
});

it('returns 200 for successful params validation', (done) => {
request(server)
.get('/contacts/7')
.expect(200, 'ok', done)
});
});

describe('body validation', () => {
it('returns 400 for failed body validation', (done) => {
request(server)
.post('/contacts')
.send({ firstName: 'Tina' })
.expect(400)
.end((err, res) => {
assert.equal(res.body.field, 'email');
assert.equal(res.body.field, 'body.email');
done();
})
});

it('returns 200 for successful body validation', (done) => {
request(server)
.post('/contacts')
.send({ firstName: 'Tina', email: '[email protected]' })
.expect(200, 'ok', done)
});
});

it('skips validation if no schema provided', (done) => {
request(server)
.get('/health')
.expect(200, 'ok', done)
});
});
});
19 changes: 10 additions & 9 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ const validate = (schema) => (
}
});

return Joi.validate(obj, schema, (err) => {
if (err) {
const message = `${err.details[0].message.replace(/"/g, "'")} at ${err.details[0].path}`;
const field = err.details[0].context.key;
res.status(400).json({ message, field }).end();
return;
}
const joiSchema = Joi.object(schema);
const { error } = joiSchema.validate(obj);

return next();
});
if (error) {
const field = error.details[0].path.join('.');
const message = error.details[0].message.replace(/"/g, "'");

return res.status(400).json({ message, field }).end();
}

return next();
}
);

Expand Down

0 comments on commit 5839040

Please sign in to comment.