Skip to content

Commit

Permalink
Pre schema validation, closes hapijs#1049
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Dec 8, 2013
1 parent 22c838c commit 9c4a09e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 11 additions & 19 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,17 @@ exports.prerequisites = function (config, server) {

/*
[
function (request, next) { },
{
method: function (request, next) { }
assign: key1
},
{
method: function (request, next) { },
assign: key2,
mode: parallel
},
[
function (request, next) { },
{
method: function (request, next) { }
assign: key1
},
{
method: function (request, next) { },
assign: key2
}
],
'user(params.id)'
]
*/
Expand All @@ -187,17 +188,8 @@ exports.prerequisites = function (config, server) {
pre = { method: pre };
}

Utils.assert(pre.method, 'Prerequisite config missing method');
Utils.assert(typeof pre.method === 'function' || typeof pre.method === 'string', 'Prerequisite method must be a function or helper name');

pre.mode = pre.mode || 'serial';
Utils.assert(pre.mode === 'serial' || pre.mode === 'parallel', 'Unknown prerequisite mode:', pre.mode);

pre.failAction = pre.failAction || 'error';
Utils.assert(['error', 'ignore', 'log'].indexOf(pre.failAction) !== -1, 'Unknown failAction:', pre.failAction);

pre.failAction = pre.failAction || 'error';
Utils.assert(['error', 'ignore', 'log'].indexOf(pre.failAction) !== -1, 'Unknown failAction:', pre.failAction);

if (typeof pre.method === 'string') {
var preMethodParts = pre.method.match(/^(\w+)(?:\s*)\((\s*\w+(?:\.\w+)*\s*(?:\,\s*\w+(?:\.\w+)*\s*)*)?\)$/);
Expand Down
13 changes: 12 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,18 @@ internals.routeConfigSchema = {
}),
cors: Joi.boolean(),
jsonp: Joi.string(),
pre: Joi.array(),
pre: Joi.array().includes([
Joi.string(),
Joi.func(),
Joi.object({
method: [Joi.string().required(), Joi.func().required()],
assign: Joi.string(),
mode: Joi.string().valid('serial', 'parallel'),
type: Joi.string().valid('pre', 'handler'),
output: Joi.string().valid('raw', 'response'),
failAction: Joi.string().valid('error', 'log', 'ignore')
})
]),
app: Joi.object().allow(null),
plugins: Joi.object(),
description: Joi.string(),
Expand Down

0 comments on commit 9c4a09e

Please sign in to comment.