UNPKG

@abyrd9/zod-form-data

Version:

Type-safe Zod v4 utilities to flatten and unflatten data, parse FormData, and collect schema-aligned errors.

64 lines 2.07 kB
import * as z4 from "zod/v4/core"; import type { DeepPartial } from "./deep-partial"; import type { FlattenedFormData } from "./schema-paths"; /** * Flattens nested form data based on a Zod v4 schema into a flat key-value map. * * This function recursively traverses a Zod schema and corresponding data, converting * nested objects, arrays, and complex types into a flat structure where each field * is represented by a dot-notation path (e.g., "user.address.street"). * * Supports all Zod v4 schema types: * - Primitive types (string, number, boolean, etc.) * - Objects with nested fields * - Arrays of primitives or objects * - Records (key-value mappings) * - Maps and Sets * - Tuples with fixed positions * - Unions and intersections * - Optional, nullable, and default fields * - Transform and pipe operations * - Lazy schemas * - Literal values and enums * - Promise types (stored as-is) * - Custom validation schemas * * @param schema - The Zod v4 schema that defines the data structure * @param data - The nested data to flatten, can be partial * @returns A flat object with dot-notation keys and corresponding values * * @example * ```typescript * const schema = z.object({ * user: z.object({ * name: z.string(), * addresses: z.array(z.object({ * street: z.string(), * city: z.string() * })) * }) * }); * * const data = { * user: { * name: "John", * addresses: [ * { street: "123 Main St", city: "Boston" }, * { street: "456 Oak Ave", city: "NYC" } * ] * } * }; * * const flattened = flattenZodFormData(schema, data); * // Result: * // { * // "user.name": "John", * // "user.addresses.0.street": "123 Main St", * // "user.addresses.0.city": "Boston", * // "user.addresses.1.street": "456 Oak Ave", * // "user.addresses.1.city": "NYC" * // } * ``` */ export declare function flattenZodFormData<T extends z4.$ZodType>(schema: T, data: DeepPartial<z4.output<T>>): FlattenedFormData<T>; //# sourceMappingURL=flatten-zod-form-data.d.ts.map