From ea8ffdb9dc8c7e207b1af818f7e0eefb0f85c97f Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Sun, 7 Jul 2024 16:39:10 -0400 Subject: [PATCH] don't validate 400 request body or query params Signed-off-by: aramissennyeydd --- packages/backend-openapi-utils/src/schema/utils.ts | 2 +- packages/backend-openapi-utils/src/schema/validation.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/backend-openapi-utils/src/schema/utils.ts b/packages/backend-openapi-utils/src/schema/utils.ts index 9c5a400ec9..aaf3fc6c41 100644 --- a/packages/backend-openapi-utils/src/schema/utils.ts +++ b/packages/backend-openapi-utils/src/schema/utils.ts @@ -39,7 +39,7 @@ export function mockttpToFetchResponse(response: CompletedResponse) { export function humanifyAjvError(error: ErrorObject) { switch (error.keyword) { case 'required': - return `The ${error.params.missingProperty} property is required`; + return `The "${error.params.missingProperty}" property is required`; case 'type': return `${ error.instancePath ? `"${error.instancePath}"` : 'Value' diff --git a/packages/backend-openapi-utils/src/schema/validation.ts b/packages/backend-openapi-utils/src/schema/validation.ts index d9d80ddcf5..5f7c6fe353 100644 --- a/packages/backend-openapi-utils/src/schema/validation.ts +++ b/packages/backend-openapi-utils/src/schema/validation.ts @@ -33,7 +33,12 @@ class RequestBodyValidator implements Validator { } async validate({ pair, operation }: ValidatorParams) { - const { request } = pair; + const { request, response } = pair; + if (response.statusCode === 400) { + // If the response is a 400, then the request is invalid and we shouldn't validate the parameters + return; + } + // NOTE: There may be a worthwhile optimization here to cache these results to avoid re-parsing the schema for every request. As is, I don't think this is a big deal. const parser = RequestBodyParser.fromOperation(operation, { ajv }); const fetchRequest = mockttpToFetchRequest(request);