Files
cal.diy/packages/app-store/routing-forms/zod.ts
T
Keith WilliamsandDevin AI 851cca25aa perf: add z.ZodType annotations to high-impact schemas (#26114)
* perf: add z.ZodType annotations to high-impact schemas

Add explicit type annotations to reduce TypeScript type inference overhead
and .d.ts file bloat in the tRPC package.

Changes:
- packages/prisma/zod-utils.ts: Add EventTypeLocation type and annotate eventTypeLocations schema
- packages/features/eventtypes/lib/schemas.ts: Add TEventTypeDuplicateInput and TCreateEventTypeInput types with annotations
- packages/app-store/routing-forms/zod.ts: Add FieldOption, TNonRouterField, TRouterField, TField, TFields types with annotations for zodField and zodFields

These schemas feed into multiple tRPC routers and were identified as high-impact
targets for reducing type checking time and declaration file sizes.

Co-Authored-By: [email protected] <[email protected]>

* fix: replace z.infer with explicit types and define EventTypeLocation locally

Address PR feedback:
- Replace z.infer<typeof calVideoSettingsSchema> with explicit CalVideoSettings type
- Define EventTypeLocation type locally instead of importing from prisma
- Add z.ZodType annotation to calVideoSettingsSchema

Note: Still importing Zod schemas from @calcom/prisma/zod-utils for validation.
Awaiting guidance on whether to move those to @calcom/lib.

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-12-21 23:15:04 -03:00

170 lines
5.6 KiB
TypeScript

import { z } from "zod";
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(),
identifier: z.string().optional(),
placeholder: z.string().optional(),
type: z.string(),
/**
* @deprecated in favour of `options`
*/
selectText: z.string().optional(),
required: z.boolean().optional(),
deleted: z.boolean().optional(),
options: z
.array(
z.object({
label: z.string(),
// To keep backwards compatibility with the options generated from legacy selectText, we allow saving null as id
// It helps in differentiating whether the routing logic should consider the option.label as value or option.id as value.
// This is important for legacy routes which has option.label saved in conditions and it must keep matching with the value of the option
id: z.string().or(z.null()),
})
)
.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.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({
routerField: zodNonRouterFieldView,
router: z.object({
name: z.string(),
description: z.string(),
id: z.string(),
}),
});
/**
* Has some additional fields that are not supposed to be saved to DB but are required for the UI
*/
export const zodFieldView = z.union([zodNonRouterFieldView, zodRouterFieldView]);
export const zodFieldsView = z.array(zodFieldView).optional();
export enum RouteActionType {
CustomPageMessage = "customPageMessage",
ExternalRedirectUrl = "externalRedirectUrl",
EventTypeRedirectUrl = "eventTypeRedirectUrl",
}
export const routeActionTypeSchema = z.nativeEnum(RouteActionType);
export const attributeRoutingConfigSchema = z
.object({
skipContactOwner: z.boolean().optional(),
salesforce: routingFormAppDataSchemas["salesforce"],
})
.nullish();
export const zodNonRouterRoute = z.object({
id: z.string(),
name: z.string().optional(),
attributeIdForWeights: z.string().optional(),
attributeRoutingConfig: attributeRoutingConfigSchema,
// TODO: It should be renamed to formFieldsQueryValue but it would take some effort
/**
* RAQB query value for form fields
* BRANDED to ensure we don't give it Attributes
*/
queryValue: raqbQueryValueSchema.brand<"formFieldsQueryValue">(),
/**
* RAQB query value for attributes. It is only applicable for Team Events as it is used to find matching team members
* BRANDED to ensure we don't give it Form Fields
*/
attributesQueryValue: raqbQueryValueSchema.optional(),
/**
* RAQB query value for fallback of `attributesQueryValue`
* BRANDED to ensure we don't give it Form Fields, It needs Attributes
*/
fallbackAttributesQueryValue: raqbQueryValueSchema.optional(),
/**
* Whether the route is a fallback if no other routes match
*/
isFallback: z.boolean().optional(),
action: z.object({
type: routeActionTypeSchema,
eventTypeId: z.number().optional(),
value: z.string(),
}),
});
export const zodNonRouterRouteView = zodNonRouterRoute;
export const zodRouterRoute = z.object({
// This is the id of the Form being used as router
id: z.string(),
name: z.string().optional(),
isRouter: z.literal(true),
});
export const zodRoute = z.union([zodNonRouterRoute, zodRouterRoute]);
export const zodRouterRouteView = zodRouterRoute.extend({
//TODO: Extend it from form
name: z.string(),
description: z.string().nullable(),
routes: z.array(z.union([zodRoute, z.null()])),
});
export const zodRoutes = z.union([z.array(zodRoute), z.null()]).optional();
export const zodRouteView = z.union([zodNonRouterRouteView, zodRouterRouteView]);
export const zodRoutesView = z.union([z.array(zodRouteView), z.null()]).optional();
// TODO: This is a requirement right now that zod.ts file (if it exists) must have appDataSchema export(which is only required by apps having EventTypeAppCard interface)
// This is a temporary solution and will be removed in future
export const appDataSchema = z.any();
export const appKeysSchema = z.object({});
// This is different from FormResponse in types.d.ts in that it has label optional. We don't seem to be using label at this point, so we might want to use this only while saving the response when Routing Form is submitted
// Record key is formFieldId
export const routingFormResponseInDbSchema = z.record(
z.object({
label: z.string().optional(),
value: z.union([z.string(), z.number(), z.array(z.string())]),
})
);