import { z } from "zod"; import { airtableOptions } from "./external/airtable/types"; import { googleSheetsOptions } from "./external/google/sheets/types"; import { notionOptions } from "./external/notion/types"; import { zodObjectValidator } from "../utils/zod-utils"; export const externalConnectorSchema = z.enum(["airtable", "google-sheets", "notion"]); export type ExternalFormSchemaConnector = z.infer; export const connectorSchema = z.enum([...externalConnectorSchema.options, "internal"]); export type FormSchemaConnector = z.infer; const indexSchema = z.strictObject({ name: z.string().describe("A unique name for the index"), fields: z.array(z.string()).describe("Fields to be indexed"), unique: z.boolean().optional().describe("Whether the index should enforce uniqueness"), }); export const internalFormSchema = z.strictObject({ provider: z.literal("internal"), id: z.string().describe("A unique identifier, e.g., 'newsletter_subscriptions'"), label: z.string().describe("Human-friendly name"), description: z.string().optional().describe("Short description of usage purpose"), schema: z .custom>(zodObjectValidator) .describe("A Zod object schema representing a row that will be saved."), indexes: z.array(indexSchema).optional().describe("Indexes to be created on the datarecord"), }); // Using discriminated union for better type inference export const genericFormSchema = z.discriminatedUnion("provider", [ z.strictObject({ provider: z.literal("airtable"), options: airtableOptions, id: z.string().describe("A unique identifier, e.g., 'newsletter_subscriptions'"), label: z.string().describe("Human-friendly name"), description: z.string().optional(), schema: z .custom>(zodObjectValidator) .describe("A Zod object schema representing a row that will be saved."), indexes: z.array(indexSchema).optional(), }), z.strictObject({ provider: z.literal("google-sheets"), options: googleSheetsOptions, id: z.string().describe("A unique identifier, e.g., 'newsletter_subscriptions'"), label: z.string().describe("Human-friendly name"), description: z.string().optional(), schema: z .custom>(zodObjectValidator) .describe("A Zod object schema representing a row that will be saved."), indexes: z.array(indexSchema).optional(), }), z.strictObject({ provider: z.literal("notion"), options: notionOptions, id: z.string().describe("A unique identifier, e.g., 'newsletter_subscriptions'"), label: z.string().describe("Human-friendly name"), description: z.string().optional(), schema: z .custom>(zodObjectValidator) .describe("A Zod object schema representing a row that will be saved."), indexes: z.array(indexSchema).optional(), }), internalFormSchema, ]); export type FormSchema = z.infer; export type InternalFormSchema = z.infer;