Skip to content

Instantly share code, notes, and snippets.

@giuseppeg
Created July 22, 2022 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuseppeg/8448e9d393c45cbf6c234c87483baf12 to your computer and use it in GitHub Desktop.
Save giuseppeg/8448e9d393c45cbf6c234c87483baf12 to your computer and use it in GitHub Desktop.
import { json } from "@remix-run/node";
import type { ZodIssue } from "zod";
import { z } from "zod";
type ActionData<T> = {
ok: false;
data: T;
errors: ZodIssue[];
};
export function actionError<T>(data: T, errors: ZodIssue[]) {
return json<ActionData<T>>(
{
ok: false,
data,
errors,
},
{ status: 400 }
);
}
export function toZodIssues(issues: string[]) {
const zoError = new z.ZodError([]);
issues.forEach((message) => {
zoError.addIssue({
code: z.ZodIssueCode.custom,
path: [],
message,
});
});
return zoError.issues;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment