From e98077baca70c770d7df5b7fc81d50fb70fc94f2 Mon Sep 17 00:00:00 2001 From: Kevin Van Lierde Date: Fri, 26 May 2023 14:57:18 +0200 Subject: [PATCH] Resolves #875: adds response arg to openapi-response-validator errorTransformer callback --- packages/openapi-response-validator/index.ts | 19 ++++++------ .../use-errorTransformer-to-format-errors.js | 4 +-- .../use-errorTransformer-with-response-arg.js | 30 +++++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 packages/openapi-response-validator/test/data-driven/use-errorTransformer-with-response-arg.js diff --git a/packages/openapi-response-validator/index.ts b/packages/openapi-response-validator/index.ts index e306013d..8de2af99 100644 --- a/packages/openapi-response-validator/index.ts +++ b/packages/openapi-response-validator/index.ts @@ -10,7 +10,7 @@ const LOCAL_DEFINITION_REGEX = /^#\/([^\/]+)\/([^\/]+)$/; export interface IOpenAPIResponseValidator { validateResponse( - statusCode: string, + statusCode: number|string, response: any ): void | OpenAPIResponseValidatorValidationError; } @@ -35,7 +35,8 @@ export interface OpenAPIResponseValidatorArgs { errorTransformer?( openAPIResponseValidatorValidationError: OpenAPIResponseValidatorError, - ajvError: ErrorObject + ajvError: ErrorObject, + response: any ): any; } @@ -52,7 +53,7 @@ export interface OpenAPIResponseValidatorValidationError { export default class OpenAPIResponseValidator implements IOpenAPIResponseValidator { - private errorMapper: (ajvError: ErrorObject) => any; + private errorMapper: (ajvError: ErrorObject, response:any) => any; private validators: { [responseCode: string]: ValidateFunction; }; @@ -111,8 +112,8 @@ export default class OpenAPIResponseValidator this.validators = compileValidators(v, schemas); } - public validateResponse(statusCode, response) { - let validator; + public validateResponse(statusCode:number|string, response) { + let validator:ValidateFunction; if (statusCode && statusCode in this.validators) { validator = this.validators[statusCode]; @@ -143,7 +144,7 @@ export default class OpenAPIResponseValidator if (!isValid) { return { message: 'The response was not valid.', - errors: validator.errors.map(this.errorMapper), + errors: validator.errors.map((err) => this.errorMapper(err, response)), }; } @@ -151,7 +152,7 @@ export default class OpenAPIResponseValidator } } -function compileValidators(v, schemas) { +function compileValidators(v:Ajv, schemas:Record) { const validators = {}; Object.keys(schemas).forEach((name) => { @@ -192,8 +193,8 @@ function getSchemas(responses, definitions, components) { return schemas; } -function makeErrorMapper(mapper): (ajvError: ErrorObject) => any { - return (ajvError) => mapper(toOpenapiValidationError(ajvError), ajvError); +function makeErrorMapper(mapper) { + return (ajvError:ErrorObject, response:any) => mapper(toOpenapiValidationError(ajvError), ajvError, response); } function toOpenapiValidationError( diff --git a/packages/openapi-response-validator/test/data-driven/use-errorTransformer-to-format-errors.js b/packages/openapi-response-validator/test/data-driven/use-errorTransformer-to-format-errors.js index 1caf12b8..c94b280e 100644 --- a/packages/openapi-response-validator/test/data-driven/use-errorTransformer-to-format-errors.js +++ b/packages/openapi-response-validator/test/data-driven/use-errorTransformer-to-format-errors.js @@ -15,7 +15,7 @@ module.exports = { definitions: null, - errorTransformer: function (openapiError, jsonschemaError) { + errorTransformer: function (openapiError, jsonschemaError, response) { return arguments.length; }, }, @@ -25,6 +25,6 @@ module.exports = { expectedValidationError: { message: 'The response was not valid.', - errors: [2], + errors: [3], }, }; diff --git a/packages/openapi-response-validator/test/data-driven/use-errorTransformer-with-response-arg.js b/packages/openapi-response-validator/test/data-driven/use-errorTransformer-with-response-arg.js new file mode 100644 index 00000000..8a7c536f --- /dev/null +++ b/packages/openapi-response-validator/test/data-driven/use-errorTransformer-with-response-arg.js @@ -0,0 +1,30 @@ +module.exports = { + constructorArgs: { + responses: { + 200: { + schema: { + type: 'object', + properties: { + foo: { + type: 'string', + }, + }, + }, + }, + }, + + definitions: null, + + errorTransformer: function (openapiError, jsonschemaError, response) { + return `Incorrect type "${typeof response.foo}" for "${openapiError.path}": ${openapiError.message}`; + }, + }, + + inputStatusCode: 200, + inputResponseBody: { foo: 2345 }, + + expectedValidationError: { + message: 'The response was not valid.', + errors: ['Incorrect type "number" for "foo": must be string'], + }, +};