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

Name middleware #583

Merged
merged 5 commits into from
Apr 23, 2021
Merged
Changes from 3 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
63 changes: 32 additions & 31 deletions src/openapi.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ export class OpenApiValidator {
};
});

const that = this; // using named functions instead of anonymous functions to allow traces to be more useful
benblack86 marked this conversation as resolved.
Show resolved Hide resolved
let inited = false;
// install path params
middlewares.push((req, res, next) =>
pContext
middlewares.push(function OpenApiValidatorPathParamsMiddleware(req, res, next) {
benblack86 marked this conversation as resolved.
Show resolved Hide resolved
return pContext
.then(({ context, error }) => {
// Throw if any error occurred during spec load.
if (error) throw error;
Expand All @@ -129,61 +130,61 @@ export class OpenApiValidator {
// Doing so would enable path params to be type coerced when provided to
// the final middleware.
// Unfortunately, it is not possible to get the current Router from a handler function
this.installPathParams(req.app, context);
that.installPathParams(req.app, context);
inited = true;
}
next();
})
.catch(next),
);
.catch(next);
});

// metadata middleware
let metamw;
middlewares.push((req, res, next) =>
pContext
middlewares.push(function OpenApiValidatorMetadataMiddleware(req, res, next) {
return pContext
.then(({ context, responseApiDoc }) => {
metamw = metamw || this.metadataMiddleware(context, responseApiDoc);
metamw = metamw || that.metadataMiddleware(context, responseApiDoc);
return metamw(req, res, next);
})
.catch(next),
);
.catch(next);
});

if (this.options.fileUploader) {
// multipart middleware
let fumw;
middlewares.push((req, res, next) =>
pContext
middlewares.push(function OpenApiValidatorMultipartMiddleware(req, res, next) {
return pContext
.then(({ context: { apiDoc } }) => {
fumw = fumw || this.multipartMiddleware(apiDoc);
fumw = fumw || that.multipartMiddleware(apiDoc);
return fumw(req, res, next);
})
.catch(next),
);
.catch(next);
});
}

// security middlware
let scmw;
middlewares.push((req, res, next) =>
pContext
middlewares.push(function OpenApiValidatorSecurityMiddleware(req, res, next) {
return pContext
.then(({ context: { apiDoc } }) => {
const components = apiDoc.components;
if (this.options.validateSecurity && components?.securitySchemes) {
scmw = scmw || this.securityMiddleware(apiDoc);
if (that.options.validateSecurity && components?.securitySchemes) {
scmw = scmw || that.securityMiddleware(apiDoc);
return scmw(req, res, next);
} else {
next();
}
})
.catch(next),
);
.catch(next);
});

// request middlweare
if (this.options.validateRequests) {
let reqmw;
middlewares.push((req, res, next) => {
middlewares.push(function OpenApiValidatorRequestMiddleware(req, res, next) {
return pContext
.then(({ context: { apiDoc } }) => {
reqmw = reqmw || this.requestValidationMiddleware(apiDoc);
reqmw = reqmw || that.requestValidationMiddleware(apiDoc);
return reqmw(req, res, next);
})
.catch(next);
Expand All @@ -193,25 +194,25 @@ export class OpenApiValidator {
// response middleware
if (this.options.validateResponses) {
let resmw;
middlewares.push((req, res, next) =>
pContext
middlewares.push(function OpenApiValidatorResponseMiddleware(req, res, next) {
return pContext
.then(({ responseApiDoc }) => {
resmw = resmw || this.responseValidationMiddleware(responseApiDoc);
resmw = resmw || that.responseValidationMiddleware(responseApiDoc);
return resmw(req, res, next);
})
.catch(next),
);
.catch(next);
})
}

// op handler middleware
if (this.options.operationHandlers) {
let router: Router = null;
middlewares.push((req, res, next) => {
middlewares.push(function OpenApiValidatorOperationHandlersMiddleware(req, res, next) {
if (router) return router(req, res, next);
pContext
return pContext
.then(
({ context }) =>
(router = this.installOperationHandlers(req.baseUrl, context)),
(router = that.installOperationHandlers(req.baseUrl, context)),
)
.then((router) => router(req, res, next))
.catch(next);
Expand Down