{"version":3,"file":"validate-form.cjs","names":[],"sources":["../../src/forms/validate-form.ts"],"sourcesContent":["import type { z } from \"zod\";\n\nexport interface ValidateFormSuccess<T> {\n    success: true;\n    data: T;\n    errors: Record<string, string>;\n}\n\nexport interface ValidateFormFailure {\n    success: false;\n    errors: Record<string, string>;\n}\n\nexport type ValidateFormResult<T> = ValidateFormSuccess<T> | ValidateFormFailure;\n\n/**\n * Validate `values` against a zod schema and return a per-field error map\n * compatible with most form libraries.\n *\n * Field paths follow zod's `issue.path.join(\".\")` convention — nested fields\n * become `\"address.city\"`, array entries `\"items.0.name\"`. The first issue\n * per path wins (subsequent ones are dropped to keep error UIs tidy).\n *\n * @param schema - zod schema describing the form shape.\n * @param values - Raw form values (typed as `unknown`).\n * @returns Either `{ success: true, data, errors: {} }` or `{ success: false, errors }`.\n */\nexport function validateForm<TSchema extends z.ZodTypeAny>(\n    schema: TSchema,\n    values: unknown,\n): ValidateFormResult<z.infer<TSchema>> {\n    const result = schema.safeParse(values);\n    if (result.success) {\n        return { success: true, data: result.data, errors: {} };\n    }\n    const errors: Record<string, string> = {};\n    for (const issue of result.error.issues) {\n        const path = issue.path.length === 0 ? \"_root\" : issue.path.join(\".\");\n        if (!(path in errors)) errors[path] = issue.message;\n    }\n    return { success: false, errors };\n}\n"],"mappings":"AA2BA,SAAgB,EACZ,EACA,EACoC,CACpC,IAAM,EAAS,EAAO,UAAU,CAAM,EACtC,GAAI,EAAO,QACP,MAAO,CAAE,QAAS,GAAM,KAAM,EAAO,KAAM,OAAQ,CAAC,CAAE,EAE1D,IAAM,EAAiC,CAAC,EACxC,IAAK,IAAM,KAAS,EAAO,MAAM,OAAQ,CACrC,IAAM,EAAO,EAAM,KAAK,SAAW,EAAI,QAAU,EAAM,KAAK,KAAK,GAAG,EAC9D,KAAQ,IAAS,EAAO,GAAQ,EAAM,QAChD,CACA,MAAO,CAAE,QAAS,GAAO,QAAO,CACpC"}