From 2e76dac2f961a1f20c591fb0a5d7ea6556d2ab70 Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Tue, 14 Jun 2016 12:31:02 -0500 Subject: [PATCH] Add assertions re https://twitter.com/mikermcneil/status/742769470028943361 --- lib/hooks/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/hooks/index.js b/lib/hooks/index.js index 585ef1c98f..d24a659aeb 100644 --- a/lib/hooks/index.js +++ b/lib/hooks/index.js @@ -19,6 +19,20 @@ module.exports = function(sails) { return function Hook(definition) { + // A few sanity checks to make sure te provided definition does not contain any reserved properties. + if (!_.isObject(definition)) { + // This particular behavior can be made a bit less genteel in future versions (it is currently + // forgiving for backwards compatibility) + definition = definition || {}; + } + + if (_.isFunction(definition.config)) { + throw new Error('Error defining hook: `config` is a reserved property and cannot be used as a custom hook method.'); + } + if (_.isFunction(definition.middleware)) { + throw new Error('Error defining hook: `middleware` is a reserved property and cannot be used as a custom hook method.'); + } + /** @@ -159,7 +173,9 @@ module.exports = function(sails) { /** - * Ensure that a hook definition has the required properties + * Ensure that a hook definition has the required properties. + * + * @returns {Dictionary} [coerced hook definition] * @api private */