From 262a35b267a0ef4a0a486ccff0e321b3aa4bb218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 6 Oct 2023 11:58:38 +0100 Subject: [PATCH] dp/fix-date-handling --- .../webhooks/service/lowcalSessionEvents/schema.ts | 2 +- .../webhooks/service/paymentRequestEvents/schema.ts | 2 +- api.planx.uk/shared/middleware/validate.ts | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api.planx.uk/modules/webhooks/service/lowcalSessionEvents/schema.ts b/api.planx.uk/modules/webhooks/service/lowcalSessionEvents/schema.ts index 53e73f0b06..2f35683b0e 100644 --- a/api.planx.uk/modules/webhooks/service/lowcalSessionEvents/schema.ts +++ b/api.planx.uk/modules/webhooks/service/lowcalSessionEvents/schema.ts @@ -4,7 +4,7 @@ import { ScheduledEventResponse } from "../../../../hasura/metadata"; export const createSessionEventSchema = z.object({ body: z.object({ - createdAt: z.string().transform((val) => new Date(val)), + createdAt: z.string().pipe(z.coerce.date()), payload: z.object({ sessionId: z.string(), }), diff --git a/api.planx.uk/modules/webhooks/service/paymentRequestEvents/schema.ts b/api.planx.uk/modules/webhooks/service/paymentRequestEvents/schema.ts index 5b7a48404c..042ea05f71 100644 --- a/api.planx.uk/modules/webhooks/service/paymentRequestEvents/schema.ts +++ b/api.planx.uk/modules/webhooks/service/paymentRequestEvents/schema.ts @@ -4,7 +4,7 @@ import { ScheduledEventResponse } from "../../../../hasura/metadata"; export const createPaymentEventSchema = z.object({ body: z.object({ - createdAt: z.string().transform((val) => new Date(val)), + createdAt: z.string().pipe(z.coerce.date()), payload: z.object({ paymentRequestId: z.string(), }), diff --git a/api.planx.uk/shared/middleware/validate.ts b/api.planx.uk/shared/middleware/validate.ts index 854a37938e..b2b740df00 100644 --- a/api.planx.uk/shared/middleware/validate.ts +++ b/api.planx.uk/shared/middleware/validate.ts @@ -13,11 +13,18 @@ export const validate = next: NextFunction, ) => { try { - schema.parse({ + const parsedReq = schema.parse({ params: req.params, body: req.body, query: req.query, }); + + // Assign parsed values to the request object + // Required for schemas to transform or coerce raw requests + req.params = parsedReq.params; + req.body = parsedReq.body; + req.query = parsedReq.query; + return next(); } catch (error) { console.error(error);