/** * Zod schemas for form definitions, themes and submission payloads. * * The Form-Builder produces JSON that conforms to {@link FormDefinitionSchema}. * The embedded renderer + submission API both parse against the same schemas * so the contract is enforced at every boundary. * * Field-type set is intentionally conservative for Phase-1: text, email, * number, textarea, select, radio, checkbox, honeypot. File-upload is * out-of-scope (see security notes in the detail-md). */ import { z } from "zod"; /** Field-types accepted by the Phase-1 form-builder. */ export const FieldTypeSchema = z.enum([ "text", "email", "number", "textarea", "select", "radio", "checkbox", "honeypot", ]); /** Stable key used for honeypot fields in payloads / UIs. */ export const HONEYPOT_FIELD_KEY = "hp_extra_data" as const; export type FormFieldType = z.infer; const FieldOption = z.object({ value: z.string().min(1).max(120), label: z.string().min(1).max(200), }); export const FormFieldSchema = z .object({ key: z .string() .min(1) .max(64) .regex(/^[a-zA-Z][a-zA-Z0-9_]*$/, "key must start with a letter and contain only alphanumerics or underscores"), type: FieldTypeSchema, label: z.string().min(1).max(200), helpText: z.string().max(500).optional(), placeholder: z.string().max(200).optional(), required: z.boolean().default(false), options: z.array(FieldOption).max(50).optional(), minLength: z.number().int().min(0).max(10000).optional(), maxLength: z.number().int().min(1).max(10000).optional(), }) .superRefine((field, ctx) => { if ((field.type === "select" || field.type === "radio") && (!field.options || field.options.length === 0)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `field ${field.key}: type ${field.type} requires at least one option`, }); } if (field.minLength !== undefined && field.maxLength !== undefined && field.minLength > field.maxLength) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `field ${field.key}: minLength must be <= maxLength`, }); } }); export type FormField = z.infer; export const FormThemeSchema = z.object({ primaryColor: z .string() .regex(/^#[0-9a-fA-F]{3,8}$/u, "primaryColor must be a hex color") .optional(), backgroundColor: z .string() .regex(/^#[0-9a-fA-F]{3,8}$/u, "backgroundColor must be a hex color") .optional(), textColor: z .string() .regex(/^#[0-9a-fA-F]{3,8}$/u, "textColor must be a hex color") .optional(), fontFamily: z.string().max(200).optional(), borderRadius: z.string().max(40).optional(), }); export type FormTheme = z.infer; export const FormDefinitionSchema = z .object({ title: z.string().min(1).max(200), description: z.string().max(2000).optional(), submitLabel: z.string().min(1).max(100).default("Submit"), successMessage: z.string().min(1).max(500).default("Thanks — we received your submission."), fields: z.array(FormFieldSchema).min(1).max(50), }) .superRefine((def, ctx) => { const seen = new Set(); for (const field of def.fields) { if (seen.has(field.key)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `duplicate field key: ${field.key}` }); } seen.add(field.key); } }); export type FormDefinition = z.infer; /** Payload accepted by the submission API. Field-values are typed loosely * here because each form has a different shape; the route handler validates * required-ness + length against the form's stored definition. */ export const FormSubmissionPayloadSchema = z.object({ values: z.record(z.string().max(64), z.union([z.string().max(10000), z.boolean(), z.number(), z.null()])), origin: z.string().max(500).optional(), turnstileToken: z.string().max(4096).optional(), }); export type FormSubmissionPayload = z.infer;