Created
July 22, 2022 07:38
-
-
Save giuseppeg/8448e9d393c45cbf6c234c87483baf12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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