tasko/lib/create-safe-action.ts

29 lines
728 B
TypeScript
Raw Permalink Normal View History

2024-02-16 01:49:19 +00:00
import { z } from 'zod';
2024-02-15 02:30:10 +00:00
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);
};
};