From affc9eca9c2db19b3a53237df30a751aae4ddbfd Mon Sep 17 00:00:00 2001 From: Evert De Spiegeleer Date: Fri, 11 Oct 2024 14:09:21 +0200 Subject: [PATCH] =?UTF-8?q?fix(output-validation):=20=F0=9F=90=9B=20no=20o?= =?UTF-8?q?utput=20is=20sent=20when=20no=20validation=20schema=20is=20spec?= =?UTF-8?q?ified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/util/endpoint.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/core/src/util/endpoint.ts b/packages/core/src/util/endpoint.ts index 7634b77..129ebd3 100644 --- a/packages/core/src/util/endpoint.ts +++ b/packages/core/src/util/endpoint.ts @@ -279,14 +279,22 @@ export const endpointToExpressHandler = (endpoint: AnyEndpoint) => { .getHandler()?.(inputParams, req, res) .then((responseObj) => { // Output validation - let postOutputValidationResponseObj: z.output> = responseObj - try { - postOutputValidationResponseObj = endpoint.getResponseValidationSchema()?.parse(responseObj) - } catch (error) { - const e = error as z.ZodError - loggerInstance.logger('endpointOutputValidation').error(e) - next(new InternalServerError()) - return + let postOutputValidationResponseObj: z.output< + NonNullable> + > + + const responseValidationSchema = endpoint.getResponseValidationSchema() + if (responseValidationSchema != null) { + try { + postOutputValidationResponseObj = responseValidationSchema.parse(responseObj) + } catch (error) { + const e = error as z.ZodError + loggerInstance.logger('endpointOutputValidation').error(e) + next(new InternalServerError()) + return + } + } else { + postOutputValidationResponseObj = responseObj } res.header('content-type', endpoint.getResponseContentType())