Something went wrong. Try again.
Extra schema for validating FormData on Zod & Zod mini 4+
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829import { z } from "zod/v4";import { transformFormDataToObject } from "./shared";import type { FormDataValidatorInput, FormDataValidatorParams } from "./types";
/** * Transforms and validates a `FormData` instance into an object parseable with `z.object()`. * * # Important notes * - If parsing server side, directly use Zod's schemas instead of using the included `dom` schemas. * - By spec, `FormData` allows for a duplicated key (e.g, having 2 or more different values per key). This validator will only use the latest / bottom-most field value * * @example * ```ts * import { z } from "zod/v4"; * import { formData } from "@angelo/zfd/full-formdata"; * * const schema = formData({ * key: z.string() * }); * * const parsed = schema.parse(new FormData()) * // ^?: { key: string } * ``` */export const formData = <T extends FormDataValidatorParams>( formSchema: T,): z.ZodType<z.output<z.ZodObject<T>>, FormDataValidatorInput> => z.instanceof(FormData).transform(transformFormDataToObject).pipe(z.object(formSchema));