Updated webhook validations

pull/9078/head
Syed Ali Shahbaz 2022-05-26 11:20:49 +05:30 committed by Agusti Fernandez Pardo
parent 2109001af1
commit 4aba0af1b4
1 changed files with 15 additions and 9 deletions

View File

@ -1,9 +1,15 @@
import { z } from "zod"; import { z } from "zod";
import { _WebhookModel as Webhook } from "@calcom/prisma/zod"; import { _WebhookModel as Webhook } from "@calcom/prisma/zod";
import { WebhookTriggerEvents } from "@calcom/prisma/client";
export const WEBHOOK_TRIGGER_EVENTS = [
WebhookTriggerEvents.BOOKING_CANCELLED,
WebhookTriggerEvents.BOOKING_CREATED,
WebhookTriggerEvents.BOOKING_RESCHEDULED,
] as ["BOOKING_CANCELLED", "BOOKING_CREATED", "BOOKING_RESCHEDULED"];
const schemaWebhookBaseBodyParams = Webhook.pick({ const schemaWebhookBaseBodyParams = Webhook.pick({
id: true,
userId: true, userId: true,
eventTypeId: true, eventTypeId: true,
eventTriggers: true, eventTriggers: true,
@ -14,12 +20,12 @@ const schemaWebhookBaseBodyParams = Webhook.pick({
export const schemaWebhookCreateParams = z export const schemaWebhookCreateParams = z
.object({ .object({
userId: z.number().or(z.string()).optional(), subscriberUrl: z.string().url(),
eventTypeId: z.number().or(z.string()).optional(), eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array(),
eventTriggers: z.any().optional(), active: z.boolean(),
active: z.boolean().optional(), payloadTemplate: z.string().nullable(),
subscriberUrl: z.string(), eventTypeId: z.number().optional(),
payloadTemplate: z.string().optional(), appId: z.string().optional().nullable(),
}) })
.strict(); .strict();
@ -30,8 +36,8 @@ export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge(
export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge( export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge(
z.object({ z.object({
payloadTemplate: z.string().optional(), payloadTemplate: z.string().optional(),
/** @todo: don't use any here and validate eventTriggers proper */ /** @todo: don't use 'any' here and validate eventTriggers proper */
eventTriggers: z.any(), eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array().optional(),
subscriberUrl: z.string().optional(), subscriberUrl: z.string().optional(),
}) })
); );