0.0.2 • Published 1 year ago

openapi-backend-strict-mode v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

openapi-backend-strict-mode

A package that implements strict response schema validation in openapi-backend.

Strips out fields from the payload that don't exactly match the schema.

Usage

Example usage with AWS Lambda API Gateway responses

import { OpenAPIBackend } from 'openapi-backend';
import { transformToStrictMode } from 'openapi-backend-strict-mode';

const api = new OpenAPIBackend({
  definition: 'openapi.yml',
});

api.register('postResponseHandler', (c) => {
  const schema =
    c.operation?.responses[c.response.statusCode]?.content?.['application/json']
      ?.schema;
  const payload = c.response.body ? JSON.parse(c.response.body) : undefined;

  if (responseBody && responseSchema) {
    const { result, additional } = transformToStrictMode({ payload, schema })

    c.response.body = JSON.stringify({
      ...result,
      // include invalid fields in a special __additional property
      __additional: additional,
    });
  }

  return c.response;
});
0.0.2

1 year ago

0.0.1

1 year ago