Skip to content

Commit

Permalink
Resolves kogosoftwarellc#875: adds response arg to openapi-response-v…
Browse files Browse the repository at this point in the history
…alidator errorTransformer callback
  • Loading branch information
webketje committed May 26, 2023
1 parent 09fed6d commit e98077b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
19 changes: 10 additions & 9 deletions packages/openapi-response-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const LOCAL_DEFINITION_REGEX = /^#\/([^\/]+)\/([^\/]+)$/;

export interface IOpenAPIResponseValidator {
validateResponse(
statusCode: string,
statusCode: number|string,
response: any
): void | OpenAPIResponseValidatorValidationError;
}
Expand All @@ -35,7 +35,8 @@ export interface OpenAPIResponseValidatorArgs {

errorTransformer?(
openAPIResponseValidatorValidationError: OpenAPIResponseValidatorError,
ajvError: ErrorObject
ajvError: ErrorObject,
response: any
): any;
}

Expand All @@ -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;
};
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -143,15 +144,15 @@ 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)),
};
}

return undefined;
}
}

function compileValidators(v, schemas) {
function compileValidators(v:Ajv, schemas:Record<string, unknown>) {
const validators = {};

Object.keys(schemas).forEach((name) => {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {

definitions: null,

errorTransformer: function (openapiError, jsonschemaError) {
errorTransformer: function (openapiError, jsonschemaError, response) {
return arguments.length;
},
},
Expand All @@ -25,6 +25,6 @@ module.exports = {

expectedValidationError: {
message: 'The response was not valid.',
errors: [2],
errors: [3],
},
};
Original file line number Diff line number Diff line change
@@ -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'],
},
};

0 comments on commit e98077b

Please sign in to comment.