tasko/lib/create-safe-action.ts
2024-02-15 20:49:19 -05:00

28 lines
728 B
TypeScript

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