From a183e6555fbe2636634c142bb37da6056e46c14c Mon Sep 17 00:00:00 2001 From: Mike Schaeffer Date: Tue, 4 Dec 2018 16:05:20 -0500 Subject: [PATCH 1/2] Correct a minor bug in which a 'basePath' would potentially be stripped from the middle of a URL. --- index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7f3f7b5..75738e5 100644 --- a/index.js +++ b/index.js @@ -17,11 +17,19 @@ const buildPathObjects = paths => _.map(paths, (pathDef, path) => ({ pathDef, })); +const stripBasePath = (url) => { + const basePath = options.schema.basePath; + + if (basePath && (url.indexOf(basePath) == 0)) { + return url.slice(basePath.length); + } else { + return url; + } +}; + const matchUrlWithSchema = (reqUrl) => { - let url = parseUrl(reqUrl).pathname; - if (options.schema.basePath) { - url = url.replace(options.schema.basePath, ''); - } + let url = stripBasePath(parseUrl(reqUrl).pathname); + const pathObj = pathObjects.filter(obj => url.match(obj.regexp)); let match = null; if (pathObj[0]) { From 8be7c368a17185d641f3ad9b66a415ce4a01052a Mon Sep 17 00:00:00 2001 From: Mike Schaeffer Date: Tue, 4 Dec 2018 16:04:30 -0500 Subject: [PATCH 2/2] Include more details in debug messages that describe validation errors/skips/etc. --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 75738e5..85b0697 100644 --- a/index.js +++ b/index.js @@ -144,7 +144,7 @@ const validateResponse = (req, res, next) => { const responseSchema = resolveResponseModelSchema(req, res); if (!responseSchema) { - debug('Response validation skipped: no matching response schema'); + debug(`Response validation skipped: no matching response schema for ${req.method} ${req.originalUrl}`); sendData(res, data, encoding); } else { let val; @@ -191,7 +191,7 @@ const validateResponse = (req, res, next) => { sendData(res, val, encoding); } else { const err = { - message: `Response schema validation failed for ${req.method}${req.originalUrl}`, + message: `Response schema validation failed for ${req.method} ${req.originalUrl}`, }; next(err); } @@ -218,7 +218,7 @@ const validateRequest = (req, res, next) => { const requestSchema = resolveRequestModelSchema(req); if (!requestSchema) { - debug('Request validation skipped: no matching request schema'); + debug(`Request validation skipped: no matching request schema for ${req.method} ${req.originalUrl}`); if (options.validateResponse) { validateResponse(req, res, next); } else { @@ -228,19 +228,19 @@ const validateRequest = (req, res, next) => { const validator = ajv.compile(requestSchema); const validation = validator(_.cloneDeep(req.body)); if (!validation) { - debug(` Request validation errors: \n${util.inspect(validator.errors)}`); + debug(` Request validation errors for ${req.method} ${req.originalUrl}: \n${util.inspect(validator.errors)}`); if (options.requestValidationFn) { options.requestValidationFn(req, req.body, validator.errors); next(); } else { const err = { - message: `Request schema validation failed for ${req.method}${req.originalUrl}`, + message: `Request schema validation failed for ${req.method} ${req.originalUrl}`, }; res.status(400); res.json(err); } } else { - debug('Request validation success'); + debug(`Request validation success for ${req.method} ${req.originalUrl}`); if (options.validateResponse) { validateResponse(req, res, next); } else {