Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/util/src/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z, ZodError, ZodIssue } from "zod";
import { z, ZodIssue } from "zod";
import { isZodError } from "./zod";

/**
* Parse a JSON string into an object and validate it against a schema
Expand All @@ -16,11 +17,8 @@ export function parseWithSchema<T extends z.ZodTypeAny>(
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return schema.parse(jsonParsed) as z.infer<T>;
} catch (error) {
// instanceof ZodError is not working from our module
if ((error as ZodError)["issues"] !== undefined) {
throw new Error(
(error as ZodError).issues.map(prettyErrorMessage).join("\n")
);
if (isZodError(error)) {
throw new Error(error.issues.map(prettyErrorMessage).join("\n"));
} else {
throw error;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/util/src/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ZodError } from "zod";

//from https://github.com/colinhacks/zod/pull/3819
export function isZodError(error: unknown): error is ZodError {
if (!(error instanceof Error)) return false;

if (error instanceof ZodError) return true;
if (error.constructor.name === "ZodError") return true;
if ("issues" in error && error.issues instanceof Array) return true;

return false;
}