Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi-response-validator: adds response arg to errorTransformer callback (closes #875) #876

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
},
};