Rhys Sullivanpr cleanup0c5fbd72 years agoHistory
27 lines · 755 B · TypeScript
RawDownload
/* eslint-disable @typescript-eslint/no-explicit-any */import { z } from "zod"; export type ActionState = {  error?: string;  success?: string;  [key: string]: any; // This allows for additional properties}; type ValidatedActionFunction<S extends z.ZodType<any, any>, T> = (  data: z.infer<S>,  formData: FormData,) => Promise<T>; export function validatedAction<S extends z.ZodType<any, any>, T>(  schema: S,  action: ValidatedActionFunction<S, T>,) {  return async (prevState: ActionState, formData: FormData): Promise<T> => {    const result = schema.safeParse(Object.fromEntries(formData));    if (!result.success) {      return { error: result.error.errors[0].message } as T;    }     return action(result.data, formData);  };}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t