fastify / fastify-response-validation

A simple plugin that enables response validation for Fastify.
MIT License
53 stars 17 forks source link

Supporting different content-type responses #79

Closed WoH closed 1 year ago

WoH commented 1 year ago

Prerequisites

🚀 Feature Proposal

This plugin does not support response validation in combination with different content types.

Motivation

While not supported by this plugin, both fastify and swagger support it already:

Example

The following setup is supported by @fastify/swagger(-ui), but does not work when used with the fastify response validation plugin:

    {
      schema: {
        body: Polygon,
        response: {
          200: {
            description: "Success",
            content: {
              "application/geo+json": {
                schema: FeatureCollection,
              },
            },
          },
      },
  }

Results are errors, but at the end of the day, it just does not seem to be intended.

``` ERROR (110): strict mode: unknown keyword: "application/geo+json" err: { "type": "Error", "message": "strict mode: unknown keyword: \"application/geo+json\"", "stack": Error: strict mode: unknown keyword: "application/geo+json" at checkStrictMode (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/util.js:174:15) at checkUnknownRules (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/util.js:32:13) at alwaysValidSchema (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/util.js:19:5) at (/node_modules/@fastify/response-validation/node_modules/ajv/dist/vocabularies/applicator/properties.js:23:63) at Array.filter () at Object.code (/node_modules/@fastify/response-validation/node_modules/ajv/dist/vocabularies/applicator/properties.js:23:37) at keywordCode (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/validate/index.js:454:13) at (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/validate/index.js:222:17) at CodeGen.code (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/codegen/index.js:439:13) at CodeGen.block (/node_modules/@fastify/response-validation/node_modules/ajv/dist/compile/codegen/index.js:568:18) } ```
``` ERROR (62): schema is invalid: data/properties/description must be object,boolean err: { "type": "Error", "message": "schema is invalid: data/properties/description must be object,boolean", "stack": Error: schema is invalid: data/properties/description must be object,boolean at Ajv.validateSchema (/node_modules/@fastify/response-validation/node_modules/ajv/dist/core.js:266:23) at Ajv._addSchema /node_modules/@fastify/response-validation/node_modules/ajv/dist/core.js:460:18) at Ajv.compile (/node_modules/@fastify/response-validation/node_modules/ajv/dist/core.js:158:26) at buildHook /node_modules/@fastify/response-validation/index.js:46:37) at Object.onRoute (/node_modules/@fastify/response-validation/index.js:38:34) at Object.addNewRoute (/node_modules/fastify/lib/route.js:243:16) at Object.route (/node_modules/fastify/lib/route.js:217:19) at Object.prepareRoute (/node_modules/fastify/lib/route.js:150:18) at Object._post (/node_modules/fastify/fastify.js:258:34) [...] ```
mcollina commented 1 year ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.