|
| 1 | +import { afterEach, expect, test } from "vitest"; |
| 2 | +import * as z from "zod/v4"; |
| 3 | + |
| 4 | +afterEach(() => { |
| 5 | + z.config({ customError: undefined }); |
| 6 | +}); |
| 7 | + |
| 8 | +/** Collects the owning schema's registered metadata for every issue an error map sees. */ |
| 9 | +function captureMeta(): { seen: (z.core.GlobalMeta | undefined)[] } { |
| 10 | + const seen: (z.core.GlobalMeta | undefined)[] = []; |
| 11 | + z.config({ |
| 12 | + customError: (issue) => { |
| 13 | + seen.push(issue.schema && z.globalRegistry.get(issue.schema)); |
| 14 | + return undefined; |
| 15 | + }, |
| 16 | + }); |
| 17 | + return { seen }; |
| 18 | +} |
| 19 | + |
| 20 | +test("check-originated issues expose the owning schema", () => { |
| 21 | + const { seen } = captureMeta(); |
| 22 | + |
| 23 | + z.object({ name: z.string().min(5).meta({ title: "Name" }) }).safeParse({ name: "ab" }); |
| 24 | + z.string().max(2).meta({ title: "Max" }).safeParse("abc"); |
| 25 | + z.email().meta({ title: "Email" }).safeParse("nope"); |
| 26 | + z.number().multipleOf(3).meta({ title: "Multiple" }).safeParse(4); |
| 27 | + z.array(z.string()).min(2).meta({ title: "Array" }).safeParse([]); |
| 28 | + |
| 29 | + expect(seen).toEqual([ |
| 30 | + { title: "Name" }, |
| 31 | + { title: "Max" }, |
| 32 | + { title: "Email" }, |
| 33 | + { title: "Multiple" }, |
| 34 | + { title: "Array" }, |
| 35 | + ]); |
| 36 | +}); |
| 37 | + |
| 38 | +test("schema-originated issues expose the owning schema", () => { |
| 39 | + const { seen } = captureMeta(); |
| 40 | + |
| 41 | + z.object({ name: z.string().min(5).meta({ title: "Name" }) }).safeParse({ name: 123 }); |
| 42 | + z.email().meta({ title: "Email" }).safeParse(123); |
| 43 | + |
| 44 | + expect(seen).toEqual([{ title: "Name" }, { title: "Email" }]); |
| 45 | +}); |
| 46 | + |
| 47 | +test("refinements attribute to the refined schema, not the internal check schema", () => { |
| 48 | + const { seen } = captureMeta(); |
| 49 | + |
| 50 | + z.string() |
| 51 | + .refine(() => false) |
| 52 | + .meta({ title: "Refine" }) |
| 53 | + .safeParse("abc"); |
| 54 | + z.string() |
| 55 | + .superRefine((_, ctx) => ctx.addIssue({ code: "custom", message: "" })) |
| 56 | + .meta({ title: "SuperRefine" }) |
| 57 | + .safeParse("abc"); |
| 58 | + |
| 59 | + expect(seen).toEqual([{ title: "Refine" }, { title: "SuperRefine" }]); |
| 60 | +}); |
| 61 | + |
| 62 | +test("the innermost schema owns the issue", () => { |
| 63 | + const { seen } = captureMeta(); |
| 64 | + |
| 65 | + z.array(z.string().min(5).meta({ title: "Element" })) |
| 66 | + .meta({ title: "Array" }) |
| 67 | + .safeParse(["ab"]); |
| 68 | + z.string() |
| 69 | + .pipe(z.string().min(5).meta({ title: "Target" })) |
| 70 | + .meta({ title: "Pipe" }) |
| 71 | + .safeParse("ab"); |
| 72 | + z.looseObject({}) |
| 73 | + .check(z.property("a", z.string().meta({ title: "Property" }))) |
| 74 | + .meta({ title: "Object" }) |
| 75 | + .safeParse({ a: 1 }); |
| 76 | + |
| 77 | + expect(seen).toEqual([{ title: "Element" }, { title: "Target" }, { title: "Property" }]); |
| 78 | +}); |
| 79 | + |
| 80 | +test("issues carrying no usable `inst` still get the owning schema", () => { |
| 81 | + const { seen } = captureMeta(); |
| 82 | + |
| 83 | + // A hand-pushed issue may carry no inst at all, and ctx.addIssue(string) puts the check's def — not a Zod object — on inst. |
| 84 | + z.string() |
| 85 | + .check((payload) => { |
| 86 | + payload.issues.push({ code: "custom", input: payload.value }); |
| 87 | + }) |
| 88 | + .meta({ title: "Bare" }) |
| 89 | + .safeParse("abc"); |
| 90 | + const shorthand = z |
| 91 | + .string() |
| 92 | + .superRefine((_, ctx) => ctx.addIssue("bad stuff")) |
| 93 | + .meta({ title: "Shorthand" }) |
| 94 | + .safeParse("abc"); |
| 95 | + |
| 96 | + expect(seen).toEqual([{ title: "Bare" }]); |
| 97 | + expect(shorthand.error!.issues[0].message).toEqual("bad stuff"); |
| 98 | +}); |
| 99 | + |
| 100 | +test("async checks expose the owning schema", async () => { |
| 101 | + const { seen } = captureMeta(); |
| 102 | + |
| 103 | + await z |
| 104 | + .string() |
| 105 | + .refine(async () => false) |
| 106 | + .meta({ title: "Async" }) |
| 107 | + .safeParseAsync("abc"); |
| 108 | + |
| 109 | + expect(seen).toEqual([{ title: "Async" }]); |
| 110 | +}); |
| 111 | + |
| 112 | +test("the owning schema is the clone `.meta()` registered, not the pre-metadata schema", () => { |
| 113 | + const bare = z.string().min(5); |
| 114 | + const labeled = bare.meta({ title: "Labeled" }); |
| 115 | + const seen: unknown[] = []; |
| 116 | + z.config({ |
| 117 | + customError: (issue) => { |
| 118 | + seen.push(issue.schema === labeled); |
| 119 | + return undefined; |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + labeled.safeParse("ab"); |
| 124 | + |
| 125 | + expect(seen).toEqual([true]); |
| 126 | +}); |
| 127 | + |
| 128 | +test("`inst` keeps its meaning and `schema` stays off the finalized issue", () => { |
| 129 | + const insts: string[] = []; |
| 130 | + z.config({ |
| 131 | + customError: (issue) => { |
| 132 | + insts.push(issue.inst!.constructor.name); |
| 133 | + return undefined; |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + const tooSmall = z.string().min(5).safeParse("ab"); |
| 138 | + const wrongType = z.string().safeParse(123); |
| 139 | + |
| 140 | + expect(insts).toEqual(["$ZodCheckMinLength", "ZodString"]); |
| 141 | + expect(Object.keys(tooSmall.error!.issues[0])).not.toContain("schema"); |
| 142 | + expect(Object.keys(wrongType.error!.issues[0])).not.toContain("schema"); |
| 143 | +}); |
0 commit comments