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

fix: Correctly parse and transform strings to date in /webhook APIs #2285

Merged
merged 1 commit into from
Oct 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is functionally the same, but syntactically a little nicer colinhacks/zod#879 (comment)

payload: z.object({
sessionId: z.string(),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}),
Expand Down
9 changes: 8 additions & 1 deletion api.planx.uk/shared/middleware/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down