Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix basePath bug and add details to various validation debug messages. #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code will replace occurrences of the basepath that occur in the middle of the input URL. (As long as they are the first such instance.)

> const basePath = '/b';
undefined
> '/a/b/c/d'.replace(basePath, '')
'/a/c/d'

}
let url = stripBasePath(parseUrl(reqUrl).pathname);

const pathObj = pathObjects.filter(obj => url.match(obj.regexp));
let match = null;
if (pathObj[0]) {
Expand Down Expand Up @@ -136,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}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent behind these changes is to make it easier to see which requests are not covered by a given schema.

sendData(res, data, encoding);
} else {
let val;
Expand Down Expand Up @@ -183,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);
}
Expand All @@ -210,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 {
Expand All @@ -220,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 {
Expand Down