Skip to content

Commit

Permalink
Only add valid routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cole Panike committed Dec 16, 2019
1 parent 71cebfa commit 495ce3c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/auto-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ module.exports.getFiles = (baseDir, pattern) => {
return glob(absolutePattern, {});
};

module.exports.getRoutes = (files) => {
module.exports.getRoutes = (filepaths) => {

return files.map((file) => {
const routeSchema = Joi.object().keys({
method: Joi.string().required(),
path: Joi.string().required(),
// Technically handler is required as well, either in the method or in its options, but that gets sticky with plugins.
}).xor('handler', 'options').unknown(true);

return Hoek.clone(require(Path.resolve(file)));
});
const multipleRoutesAllowed = Joi.alternatives(routeSchema, Joi.array().items(routeSchema));

const files = filepaths.map(p => require(Path.resolve(p)));

const routes = files.filter(file => {
const { error } = Joi.validate(file, multipleRoutesAllowed);
return error === null;
})

return routes.map((file) => Hoek.clone(file));
};

module.exports.getPrefixes = (files, baseDir) => {
Expand Down

0 comments on commit 495ce3c

Please sign in to comment.