Skip to content

Commit 629ec61

Browse files
kanongilMarsup
authored andcommitted
Improve input validation and error messaging
1 parent 2dca411 commit 629ec61

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

lib/index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,13 @@ internals.options = Validate.object().keys({
3838
});
3939

4040

41-
exports.inject = function (dispatchFunc, options) {
41+
exports.inject = async function (dispatchFunc, options) { // eslint-disable-line require-await
4242

4343
options = (typeof options === 'string' ? { url: options } : options);
4444

45-
if (options.validate !== false) { // Defaults to true
46-
try {
47-
Hoek.assert(typeof dispatchFunc === 'function', 'Invalid dispatch function');
48-
Validate.assert(options, internals.options);
49-
}
50-
catch (err) {
51-
return Promise.reject(err);
52-
}
45+
if (options?.validate !== false) { // Defaults to true
46+
Hoek.assert(typeof dispatchFunc === 'function', 'Invalid or missing dispatch function');
47+
Validate.assert(options ?? null, internals.options, 'Invalid options:');
5348
}
5449

5550
return new Promise((resolve) => {

test/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,15 @@ describe('_read()', () => {
794794

795795
it('errors for invalid input options', async () => {
796796

797-
await expect(Shot.inject({}, {})).to.reject('Invalid dispatch function');
797+
await expect(Shot.inject()).to.reject('Invalid or missing dispatch function');
798+
await expect(Shot.inject({}, {})).to.reject('Invalid or missing dispatch function');
799+
await expect(Shot.inject(Hoek.ignore)).to.reject(/^Invalid options:/);
800+
await expect(Shot.inject(Hoek.ignore, true)).to.reject(/^Invalid options:/);
798801
});
799802

800803
it('errors for missing url', async () => {
801804

802-
const err = await expect(Shot.inject((req, res) => { }, {})).to.reject();
805+
const err = await expect(Shot.inject((req, res) => { }, {})).to.reject(/^Invalid options:/);
803806
expect(err.isJoi).to.be.true();
804807
});
805808

0 commit comments

Comments
 (0)