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, }; } type FormFailureData, Action extends keyof Validations> = { errors: FormError; submitted?: never; }; type FormSuccessData = { errors?: never; submitted: true; }; export type FormActionData, Action extends keyof Validations> = Record< Action, FormSuccessData | FormFailureData >;