forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add installOpenapiValidationMiddleware
This method allows us to re-use the API server's internal mechanisms to configure the OpenAPI spec validation in test cases where we cannot depend on the API server itself due to circular dependencies. So this method is designed to be used both by the API server and the test cases at the same time. Closes: hyperledger-cacti#847 Signed-off-by: Elena Izaguirre <e.izaguirre.equiza@accenture.com>
- Loading branch information
1 parent
e7b60a0
commit 186cd1b
Showing
29 changed files
with
6,334 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ges/cactus-core/src/main/typescript/web-services/install-open-api-validator-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { Application, NextFunction, Request, Response } from "express"; | ||
import * as OpenApiValidator from "express-openapi-validator"; | ||
import { OpenAPIV3 } from "express-openapi-validator/dist/framework/types"; | ||
|
||
import { | ||
Checks, | ||
LoggerProvider, | ||
LogLevelDesc, | ||
} from "@hyperledger/cactus-common"; | ||
|
||
export interface IInstallOpenapiValidationMiddlewareRequest { | ||
readonly logLevel: LogLevelDesc; | ||
readonly app: Application; | ||
readonly apiSpec: unknown; | ||
} | ||
|
||
/** | ||
* Installs the middleware that validates openapi specifications | ||
* @param app | ||
* @param pluginOAS | ||
*/ | ||
export async function installOpenapiValidationMiddleware( | ||
req: IInstallOpenapiValidationMiddlewareRequest, | ||
): Promise<void> { | ||
const fnTag = "installOpenapiValidationMiddleware"; | ||
Checks.truthy(req, `${fnTag} req`); | ||
Checks.truthy(req.apiSpec, `${fnTag} req.apiSpec`); | ||
Checks.truthy(req.app, `${fnTag} req.app`); | ||
const { app, apiSpec, logLevel } = req; | ||
const log = LoggerProvider.getOrCreate({ | ||
label: fnTag, | ||
level: logLevel || "INFO", | ||
}); | ||
log.debug(`Installing validation for OpenAPI specs: `, apiSpec); | ||
|
||
app.use( | ||
OpenApiValidator.middleware({ | ||
apiSpec: apiSpec as OpenAPIV3.Document, | ||
validateApiSpec: false, | ||
}), | ||
); | ||
app.use( | ||
( | ||
err: { | ||
status?: number; | ||
errors: [ | ||
{ | ||
path: string; | ||
message: string; | ||
errorCode: string; | ||
}, | ||
]; | ||
}, | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
if (err) { | ||
res.status(err.status || 500); | ||
res.send(err.errors); | ||
} else { | ||
next(); | ||
} | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.