feat: version 2 beta
This commit is contained in:
33
libraries/zod.ts
Normal file
33
libraries/zod.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { ZodError } from "zod";
|
||||
|
||||
export function toPrettyErrorLines(error: ZodError, padding: number = 0): string[] {
|
||||
const lines: string[] = [];
|
||||
const margin = " ".repeat(padding);
|
||||
const issues = [...error.issues].sort((a, b) => a.path.length - b.path.length);
|
||||
for (const issue of issues) {
|
||||
lines.push(`${margin}✖ ${issue.message}`);
|
||||
if (issue.path?.length) {
|
||||
lines.push(`${margin} → at ${toDotPath(issue.path)}`);
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
function toDotPath(path: (string | number | symbol)[]): string {
|
||||
const segs: string[] = [];
|
||||
for (const seg of path) {
|
||||
if (typeof seg === "number") {
|
||||
segs.push(`[${seg}]`);
|
||||
} else if (typeof seg === "symbol") {
|
||||
segs.push(`["${String(seg)}"]`);
|
||||
} else if (seg.includes(".")) {
|
||||
segs.push(`["${seg}"]`);
|
||||
} else {
|
||||
if (segs.length) {
|
||||
segs.push(".");
|
||||
}
|
||||
segs.push(seg);
|
||||
}
|
||||
}
|
||||
return segs.join("");
|
||||
}
|
||||
Reference in New Issue
Block a user