Skip to content

Commit

Permalink
feat(routing): create a validateOptions method that validates other r…
Browse files Browse the repository at this point in the history
…oute's additional requirements

Route's additional options can include list of rest method verbs it listens for
  • Loading branch information
teclone committed Jul 20, 2018
1 parent e8fc2eb commit 4c0aa6d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/modules/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ export default class Router {
return 'Router';
}

/**
*@param {Object} [options] - optional configuration options
*@param {Array} [options.methods] - array of methods allowed
*@returns {boolean}
*/
validateOptions(options) {
options = Util.isPlainObject(options)? options : {};
let result = true;

//validate request method. the request method should be among the options.methods item.
if (Util.isArray(options.methods)) {
let method = this.method;
result = options.methods.some((testMethod) => {
return typeof testMethod === 'string' && testMethod.toUpperCase() === method;
});
}
return result;
}

/**
*@description - checks if the request method is ok and that callback is a function
*@param {Function} callback - the callback function
Expand Down Expand Up @@ -61,7 +80,7 @@ export default class Router {
this.params = []; //reset the params tuple
routeUrl = routeUrl.toLowerCase().replace(/^\/+/, '').replace(/\/+$/, '');

if (!this.validateRoute(callback, overrideMethod))
if (!this.validateRoute(callback, overrideMethod) || !this.validateOptions(options))
return;
}

Expand Down

0 comments on commit 4c0aa6d

Please sign in to comment.