Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation schema to shot #71

Merged
merged 3 commits into from Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/index.js
Expand Up @@ -2,12 +2,19 @@

// Load modules

const Joi = require('joi');
const Request = require('./request');
const Response = require('./response');

const Schema = require('./schema');

exports.inject = function (dispatchFunc, options, callback) {

Joi.assert({
dispatchFunc: dispatchFunc,
options: options,
callback: callback
}, Schema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ options, callback } looks nicer


const req = new Request(options);
const res = new Response(req, callback);

Expand Down
29 changes: 29 additions & 0 deletions lib/schema.js
@@ -0,0 +1,29 @@
'use strict';

const Joi = require('joi');

const internals = {};

internals.url = Joi.alternatives(Joi.string().required(), Joi.object().keys({
protocol: Joi.string(),
hostname: Joi.string(),
port: Joi.any(),
pathname: Joi.string().required(),
query: Joi.any()
}).required());

internals.options = Joi.object().keys({
url: internals.url.required(),
headers: Joi.object(),
payload: Joi.any(),
simulate: Joi.object(), // add these
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Add these... :-)

authority: Joi.string(),
remoteAddress: Joi.string(),
method: Joi.string().regex(/^[a-zA-Z0-9!#\$%&'\*\+\-\.^_`\|~]+$/)
}).min(1);

module.exports = Joi.object().keys({
dispatchFunc: Joi.func().required(),
options: Joi.alternatives(internals.options, internals.url).required(),
callback: Joi.func()
});
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -13,6 +13,9 @@
"engines": {
"node": ">=4.0.0"
},
"dependencies": {
"joi": "9.x.x"
},
"devDependencies": {
"code": "3.x.x",
"lab": "10.x.x"
Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Expand Up @@ -750,6 +750,31 @@ describe('_read()', () => {
done();
});
});

it('errors for invalid input options', (done) => {

try {
Shot.inject({}, {}, (res) => {});
}
catch (err) {

expect(err).to.exist();
expect(err.isJoi).to.be.true();
done();
}
});

it('errors for missing url', (done) => {

try {
Shot.inject((req, res) => {}, {}, (res) => {});
}
catch (err) {
expect(err).to.exist();
expect(err.isJoi).to.be.true();
done();
}
});
});


Expand Down