koa-x-oapi-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Koa port of the popular express-openapi-validator library. See 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 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 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
npm i koa-x-oapi-validator
  1. Initialize the middleware
const validator = new KoaOpenAPIValidator({
  apiSpec: OPENAPI_SPEC,
  validateRequests: true,
  validateResponses: true,
});
const validatorMw = validator.use();
  1. Use the middleware
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).

Package Sidebar

Install

npm i koa-x-oapi-validator

Weekly Downloads

18

Version

1.0.1

License

MIT

Unpacked Size

97.3 kB

Total Files

30

Last publish

Collaborators

  • ron96