diff --git a/packages/openapi-response-validator/index.ts b/packages/openapi-response-validator/index.ts index e306013d..f8daa97e 100644 --- a/packages/openapi-response-validator/index.ts +++ b/packages/openapi-response-validator/index.ts @@ -3,6 +3,7 @@ import Ajv, { Format, ErrorObject, ValidateFunction, + Options, } from 'ajv'; import { IJsonSchema, OpenAPIV2, OpenAPIV3 } from 'openapi-types'; @@ -37,6 +38,7 @@ export interface OpenAPIResponseValidatorArgs { openAPIResponseValidatorValidationError: OpenAPIResponseValidatorError, ajvError: ErrorObject ): any; + ajvOptions?: Options; } export interface OpenAPIResponseValidatorError { @@ -82,6 +84,7 @@ export default class OpenAPIResponseValidator strict: false, // @ts-ignore TODO get Ajv updated to account for logger logger: false, + ...(args.ajvOptions || {}), }); this.errorMapper = errorTransformer diff --git a/packages/openapi-response-validator/test/data-driven/fail-with-discriminating-errors-on-oneof.js b/packages/openapi-response-validator/test/data-driven/fail-with-discriminating-errors-on-oneof.js new file mode 100644 index 00000000..6f09520c --- /dev/null +++ b/packages/openapi-response-validator/test/data-driven/fail-with-discriminating-errors-on-oneof.js @@ -0,0 +1,59 @@ +module.exports = { + constructorArgs: { + ajvOptions: { + discriminator: true + }, + responses: { + 200: { + schema: { + oneOf: [ + { + type: 'object', + properties: { + foo: { + type: 'string', + }, + my_type: { + type: 'string', + enum: ['foo'] + } + }, + required: ['foo', 'my_type'], + }, + { + + type: 'object', + properties: { + bar: { + type: 'string', + }, + my_type: { + type: 'string', + enum: ['bar'] + } + }, + required: ['bar', 'my_type'], + } + ], + discriminator: { + propertyName: 'my_type' + } + } + } + }, + }, + inputStatusCode: 200, + inputResponseBody: { + my_type: 'foo', + }, + expectedValidationError: { + message: 'The response was not valid.', + errors:[ + { + errorCode: 'required.openapi.responseValidation', + message: "must have required property 'foo'", + path: 'response' + } + ] + } +} diff --git a/packages/openapi-response-validator/test/data-driven/fail-with-indisriminating-errors-on-oneof.js b/packages/openapi-response-validator/test/data-driven/fail-with-indisriminating-errors-on-oneof.js new file mode 100644 index 00000000..292bdbea --- /dev/null +++ b/packages/openapi-response-validator/test/data-driven/fail-with-indisriminating-errors-on-oneof.js @@ -0,0 +1,71 @@ +module.exports = { + constructorArgs: { + responses: { + 200: { + schema: { + oneOf: [ + { + type: 'object', + properties: { + foo: { + type: 'string', + }, + my_type: { + type: 'string', + enum: ['foo'] + } + }, + required: ['foo', 'my_type'], + }, + { + + type: 'object', + properties: { + bar: { + type: 'string', + }, + my_type: { + type: 'string', + enum: ['bar'] + } + }, + required: ['bar', 'my_type'], + } + ], + discriminator: { + propertyName: 'my_type' + } + } + } + } + }, + inputStatusCode: 200, + inputResponseBody: { + my_type: 'foo', + }, + expectedValidationError: { + message: 'The response was not valid.', + errors:[ + { + errorCode: 'required.openapi.responseValidation', + message: "must have required property 'foo'", + path: 'response' + }, + { + errorCode: 'required.openapi.responseValidation', + message: "must have required property 'bar'", + path: 'response' + }, + { + errorCode: 'enum.openapi.responseValidation', + message: 'must be equal to one of the allowed values', + path: 'my_type' + }, + { + errorCode: 'oneOf.openapi.responseValidation', + message: 'must match exactly one schema in oneOf', + path: 'response' + } + ] + } +}