import { z } from 'zod'; export type FieldErrors = { [K in keyof T]?: string[]; }; export type ActionState = { fieldErrors?: FieldErrors; error?: string | null; data?: TOutput; }; export const createSafeAction = ( schema: z.Schema, handler: (validatedData: TInput) => Promise> ) => { return async (data: TInput): Promise> => { const validationResult = schema.safeParse(data); if (!validationResult.success) { return { fieldErrors: validationResult.error.flatten() .fieldErrors as FieldErrors, }; } return handler(validationResult.data); }; };