Skip to content

Scheduling infrastructure for absolutely everyone.

perf: add z.ZodType annotations to high-impact schemas (#26114)

Keith Williams851cca25aa8b
diff --git a/packages/app-store/routing-forms/zod.ts b/packages/app-store/routing-forms/zod.ts
index 60145f30581..8054b8fd952 100644
--- a/packages/app-store/routing-forms/zod.ts
+++ b/packages/app-store/routing-forms/zod.ts
@@ -4,6 +4,26 @@ import { raqbQueryValueSchema } from "@calcom/lib/raqb/zod";
import { routingFormAppDataSchemas } from "./appDataSchemas";
+export type FieldOption = {
+ label: string;
+ id: string | null;
+};
+
+export type TNonRouterField = {
+ id: string;
+ label: string;
+ identifier?: string;
+ placeholder?: string;
+ type: string;
+ /** @deprecated in favour of `options` */
+ selectText?: string;
+ required?: boolean;
+ deleted?: boolean;
+ options?: FieldOption[];
+};
+
+// Note: zodNonRouterField is NOT annotated with z.ZodType because it uses .extend() below
+// which requires the full ZodObject type to be preserved
export const zodNonRouterField = z.object({
id: z.string(),
label: z.string(),
@@ -29,13 +49,21 @@ export const zodNonRouterField = z.object({
.optional(),
});
+export type TRouterField = TNonRouterField & {
+ routerId: string;
+};
+
+// Note: zodRouterField is NOT annotated with z.ZodType because it uses .extend() below
export const zodRouterField = zodNonRouterField.extend({
routerId: z.string(),
});
+export type TField = TNonRouterField | TRouterField;
+export type TFields = TField[] | undefined;
+
// This ordering is important - If routerId is present then it should be in the parsed object. Moving zodNonRouterField to first position doesn't do that
-export const zodField = z.union([zodRouterField, zodNonRouterField]);
-export const zodFields = z.array(zodField).optional();
+export const zodField: z.ZodType<TField> = z.union([zodRouterField, zodNonRouterField]);
+export const zodFields: z.ZodType<TFields> = z.array(zodField).optional();
export const zodNonRouterFieldView = zodNonRouterField;
export const zodRouterFieldView = zodRouterField.extend({
diff --git a/packages/features/eventtypes/lib/schemas.ts b/packages/features/eventtypes/lib/schemas.ts
index 82350a70369..30b467797d5 100644
--- a/packages/features/eventtypes/lib/schemas.ts
+++ b/packages/features/eventtypes/lib/schemas.ts
@@ -3,7 +3,21 @@ import { z } from "zod";
import { SchedulingType } from "@calcom/prisma/enums";
import { eventTypeLocations, EventTypeMetaDataSchema, eventTypeSlug } from "@calcom/prisma/zod-utils";
-const calVideoSettingsSchema = z
+type CalVideoSettings =
+ | {
+ disableRecordingForGuests?: boolean | null;
+ disableRecordingForOrganizer?: boolean | null;
+ enableAutomaticTranscription?: boolean | null;
+ enableAutomaticRecordingForOrganizer?: boolean | null;
+ disableTranscriptionForGuests?: boolean | null;
+ disableTranscriptionForOrganizer?: boolean | null;
+ redirectUrlOnExit?: string | null;
+ requireEmailForGuests?: boolean | null;
+ }
+ | null
+ | undefined;
+
+const calVideoSettingsSchema: z.ZodType<CalVideoSettings> = z
.object({
disableRecordingForGuests: z.boolean().nullish(),
disableRecordingForOrganizer: z.boolean().nullish(),
@@ -17,7 +31,29 @@ const calVideoSettingsSchema = z
.optional()
.nullable();
-export const EventTypeDuplicateInput = z
+type EventTypeLocation = {
+ type: string;
+ address?: string;
+ link?: string;
+ displayLocationPublicly?: boolean;
+ hostPhoneNumber?: string;
+ credentialId?: number;
+ teamName?: string;
+ customLabel?: string;
+};
+
+type EventTypeMetadata = z.infer<typeof EventTypeMetaDataSchema>;
+
+export type TEventTypeDuplicateInput = {
+ id: number;
+ slug: string;
+ title: string;
+ description: string;
+ length: number;
+ teamId?: number | null;
+};
+
+export const EventTypeDuplicateInput: z.ZodType<TEventTypeDuplicateInput> = z
.object({
id: z.number(),
slug: z.string(),
@@ -28,7 +64,26 @@ export const EventTypeDuplicateInput = z
})
.strict();
-export const createEventTypeInput = z
+export type TCreateEventTypeInput = {
+ title: string;
+ slug: string;
+ description?: string | null;
+ length: number;
+ hidden?: boolean;
+ teamId?: number | null;
+ schedulingType?: SchedulingType | null;
+ locations?: EventTypeLocation[];
+ metadata?: EventTypeMetadata;
+ disableGuests?: boolean;
+ slotInterval?: number | null;
+ minimumBookingNotice?: number;
+ beforeEventBuffer?: number;
+ afterEventBuffer?: number;
+ scheduleId?: number;
+ calVideoSettings?: CalVideoSettings;
+};
+
+export const createEventTypeInput: z.ZodType<TCreateEventTypeInput> = z
.object({
title: z.string().trim().min(1),
slug: eventTypeSlug,
diff --git a/packages/prisma/zod-utils.ts b/packages/prisma/zod-utils.ts
index deb1498707c..88c880c32ef 100644
--- a/packages/prisma/zod-utils.ts
+++ b/packages/prisma/zod-utils.ts
@@ -327,7 +327,18 @@ export const bookingResponses = z
export type BookingResponses = z.infer<typeof bookingResponses>;
-export const eventTypeLocations = z.array(
+export type EventTypeLocation = {
+ type: string;
+ address?: string;
+ link?: string;
+ displayLocationPublicly?: boolean;
+ hostPhoneNumber?: string;
+ credentialId?: number;
+ teamName?: string;
+ customLabel?: string;
+};
+
+export const eventTypeLocations: z.ZodType<EventTypeLocation[]> = z.array(
z.object({
// TODO: Couldn't find a way to make it a union of types from App Store locations
// Creating a dynamic union by iterating over the object doesn't seem to make TS happy