Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ron96g/koa-x-oapi-validator
Koa port of the popular express-openapi-validator library.
https://github.com/ron96g/koa-x-oapi-validator
koa middleware nodejs openapi openapi-3 request-validation response-validation swagger
Last synced: about 1 month ago
JSON representation
Koa port of the popular express-openapi-validator library.
- Host: GitHub
- URL: https://github.com/ron96g/koa-x-oapi-validator
- Owner: ron96G
- License: mit
- Created: 2023-02-25T21:55:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-29T19:31:41.000Z (about 1 year ago)
- Last Synced: 2024-10-01T02:04:54.907Z (about 2 months ago)
- Topics: koa, middleware, nodejs, openapi, openapi-3, request-validation, response-validation, swagger
- Language: TypeScript
- Homepage:
- Size: 337 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Koa port of the popular [express-openapi-validator](https://www.npmjs.com/package/express-openapi-validator) library.
See [Intention](#intention)
# Intention
I was looking for a OpenAPI validation middleware solution for Koa. However, I was unable to find anything that (a) had the required functionality and (b) was actively maintained.
Therefore, I wrote this library as a port to the popular [express-openapi-validator](https://www.npmjs.com/package/express-openapi-validator) which satisfies the requirements and with which I have worked before.This does come with some drawbacks as the solution used in express-openapi-validator has been tailored for express and thus is not useable as [koa-openapi-validator](https://www.npmjs.com/package/koa-openapi-validator) shows.
If there is any solution for Koa that is sufficient, I would recommend using this instead of this.
# Usage
This library should be integrated as a normal Koa middleware.
Error-Handling and everything else should be done the normal "Koa way".1. Install the library
```bash
npm i koa-x-oapi-validator
```2. Initialize the middleware
```ts
const validator = new KoaOpenAPIValidator({
apiSpec: OPENAPI_SPEC,
validateRequests: true,
validateResponses: true,
});
const validatorMw = validator.use();
```3. Use the middleware
```ts
router.post('/hello', validatorMw, async (ctx: Koa.Context) => {
ctx.status = 200;
ctx.body = {
message: 'Hello World!',
};
});
```# Debugging
To enable debug logging set the environment variable `DEBUG=validator.*` (see [debug](https://www.npmjs.com/package/debug)).