Skip to content

Commit 5668088

Browse files
authored
Handle stringified webhook metadata payloads (dubinc#3642)
1 parent 00d48f9 commit 5668088

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

apps/web/lib/webhook/schemas.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,30 @@ export const clickWebhookEventSchema = z.object({
2222
link: linkEventSchema,
2323
});
2424

25+
const coerceJsonString = (val: unknown) => {
26+
if (typeof val === "string") {
27+
try {
28+
return JSON.parse(val);
29+
} catch {
30+
return val;
31+
}
32+
}
33+
34+
return val;
35+
};
36+
37+
const metadataSchema = z.preprocess(
38+
coerceJsonString,
39+
z.record(z.string(), z.any()).nullish().default(null),
40+
);
41+
2542
export const leadWebhookEventSchema = z.object({
2643
eventName: z.string(),
2744
customer: CustomerSchema,
2845
click: clickEventSchema,
2946
link: linkEventSchema,
3047
partner: WebhookPartnerSchema.nullish(),
31-
metadata: z.record(z.string(), z.any()).nullable().default(null),
48+
metadata: metadataSchema,
3249
});
3350

3451
export const saleWebhookEventSchema = z.object({
@@ -38,7 +55,7 @@ export const saleWebhookEventSchema = z.object({
3855
link: linkEventSchema,
3956
sale: webhookSaleSchema,
4057
partner: WebhookPartnerSchema.nullish(),
41-
metadata: z.record(z.string(), z.any()).nullable().default(null),
58+
metadata: metadataSchema,
4259
});
4360

4461
// Schema of the payload sent to the webhook endpoint by Dub

0 commit comments

Comments
 (0)