import type { z } from "zod"; export type ParseResult = | { success: true; data: T } | { success: false; fieldErrors: Record }; /** * Parses JSON request body against a Zod schema. * Returns parsed data on success, or field-level errors on validation failure. * Intended for use with SendouForm which always submits JSON. */ export async function parseFormData({ request, schema, }: { request: Request; schema: T; }): Promise>> { const json = await request.json(); const result = await schema.safeParseAsync(json); if (result.success) { return { success: true, data: result.data }; } const fieldErrors: Record = {}; for (const issue of result.error.issues) { const path = issue.path.join("."); if (path && !fieldErrors[path]) { fieldErrors[path] = issue.message; } } return { success: false, fieldErrors }; }