MCPcopy Create free account
hub / github.com/code100x/cms / createSafeAction

Function createSafeAction

src/lib/create-safe-action.ts:19–34  ·  view source on GitHub ↗
(
  schema: z.Schema<TInput>,
  handler: (validatedData: TInput) => Promise<ActionState<TInput, TOutput>>,
)

Source from the content-addressed store, hash-verified

17};
18
19export const createSafeAction = <TInput, TOutput>(
20 schema: z.Schema<TInput>,
21 handler: (validatedData: TInput) => Promise<ActionState<TInput, TOutput>>,
22) => {
23 return async (data: TInput): Promise<ActionState<TInput, TOutput>> => {
24 const validationResult = schema.safeParse(data);
25 if (!validationResult.success) {
26 return {
27 fieldErrors: validationResult.error.flatten()
28 .fieldErrors as FieldErrors<TInput>,
29 };
30 }
31
32 return handler(validationResult.data);
33 };
34};

Callers 7

index.tsFile · 0.90
index.tsFile · 0.90
userActions.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected