diff --git a/lib/auto-route.js b/lib/auto-route.js index 47a981b..34949f9 100644 --- a/lib/auto-route.js +++ b/lib/auto-route.js @@ -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) => {