Created
April 10, 2026 14:43
-
-
Save imaginamundo/9e48d6381dc40fbb372c901d3f3ad79a to your computer and use it in GitHub Desktop.
Parse form schema using zod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { z } from "astro/zod"; | |
| export function formDataToObject( | |
| formData: FormData, | |
| ): Record<string, FormDataEntryValue | FormDataEntryValue[]> { | |
| const obj = Object.create(null) as Record<string, FormDataEntryValue | FormDataEntryValue[]>; | |
| for (const [key, value] of formData.entries()) { | |
| if (Object.hasOwn(obj, key)) { | |
| const currentValue = obj[key]; | |
| if (!Array.isArray(currentValue)) { | |
| obj[key] = [currentValue, value]; | |
| } else { | |
| currentValue.push(value); | |
| } | |
| } else { | |
| obj[key] = value; | |
| } | |
| } | |
| return obj; | |
| } | |
| export function parseSchema<T extends z.ZodTypeAny>(input: FormData, schema: T) { | |
| const fields = formDataToObject(input); | |
| const validation = schema.safeParse(fields); | |
| type InferedType = z.infer<T>; | |
| if (!validation.success) { | |
| const fieldErrors: Partial<Record<keyof InferedType, string>> = {}; | |
| for (const issue of validation.error.issues) { | |
| const path = String(issue.path[0]) as keyof InferedType; | |
| if (path && !fieldErrors[path]) { | |
| fieldErrors[path] = issue.message; | |
| } | |
| } | |
| return { | |
| success: false as const, | |
| fields: fields as Partial<InferedType>, | |
| fieldErrors, | |
| }; | |
| } | |
| return { | |
| success: true as const, | |
| fields: validation.data as InferedType, | |
| fieldErrors: null, | |
| }; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: