import type { z } from "zod"; type ErrorMessage = string; type Errors = Partial>; export type FormError> = Partial< Record >; type ValidationResult = { data: Data; errors: undefined } | { data: undefined; errors: Errors }; export function validate["_type"]>( schema: z.Schema, value: unknown, ): ValidationResult { const result = schema.safeParse(value); if (result.success) { return { data: result.data, errors: undefined, }; } const errors: Errors = {}; result.error.issues.forEach((error) => { const path = error.path[0] as keyof Schema; if (!errors[path]) { errors[path] = error.message; } }); return { data: undefined, errors, }; }