|
| 1 | +import { expect, expectTypeOf, test } from "vitest"; |
| 2 | +import { z } from "zod/v4"; |
| 3 | + |
| 4 | +/* ------------------------------------------------------------------ * |
| 5 | + * TYPE-LEVEL — the dangerous half. Defaults must stay OPTIONAL in |
| 6 | + * z.input and REQUIRED in z.output, exactly as before the ladder. |
| 7 | + * ------------------------------------------------------------------ */ |
| 8 | +test("ladder: z.input / z.output unchanged by the extra rung", () => { |
| 9 | + const objDefault = z.object({ a: z.string().default("D"), b: z.number() }); |
| 10 | + expectTypeOf<z.input<typeof objDefault>>().toEqualTypeOf<{ a?: string | undefined; b: number }>(); |
| 11 | + expectTypeOf<z.output<typeof objDefault>>().toEqualTypeOf<{ a: string; b: number }>(); |
| 12 | + |
| 13 | + const objPrefault = z.object({ a: z.string().prefault("P"), b: z.number() }); |
| 14 | + expectTypeOf<z.input<typeof objPrefault>>().toEqualTypeOf<{ a?: string | undefined; b: number }>(); |
| 15 | + expectTypeOf<z.output<typeof objPrefault>>().toEqualTypeOf<{ a: string; b: number }>(); |
| 16 | + |
| 17 | + const objOptional = z.object({ a: z.string().optional() }); |
| 18 | + expectTypeOf<z.input<typeof objOptional>>().toEqualTypeOf<{ a?: string | undefined }>(); |
| 19 | + |
| 20 | + const objTransform = z.object({ |
| 21 | + a: z |
| 22 | + .string() |
| 23 | + .default("") |
| 24 | + .transform((v) => v.length), |
| 25 | + }); |
| 26 | + expectTypeOf<z.input<typeof objTransform>>().toEqualTypeOf<{ a?: string | undefined }>(); |
| 27 | + expectTypeOf<z.output<typeof objTransform>>().toEqualTypeOf<{ a: number }>(); |
| 28 | + |
| 29 | + const standalone = z.string().default("D"); |
| 30 | + expectTypeOf<z.input<typeof standalone>>().toEqualTypeOf<string | undefined>(); |
| 31 | + expectTypeOf<z.output<typeof standalone>>().toEqualTypeOf<string>(); |
| 32 | + |
| 33 | + // a trailing defaulted tuple slot stays optional in the input tuple |
| 34 | + const tup = z.tuple([z.string(), z.number().default(1)]); |
| 35 | + expectTypeOf<z.input<typeof tup>>().toEqualTypeOf<[string, (number | undefined)?]>(); |
| 36 | + |
| 37 | + const partialed = objDefault.partial(); |
| 38 | + expectTypeOf<z.input<typeof partialed>>().toEqualTypeOf<{ a?: string | undefined; b?: number | undefined }>(); |
| 39 | +}); |
| 40 | + |
| 41 | +/* ------------------------------------------------------------------ * |
| 42 | + * RUNTIME |
| 43 | + * ------------------------------------------------------------------ */ |
| 44 | +test("ladder: the #6321 regression is fixed", () => { |
| 45 | + const arr = z |
| 46 | + .string() |
| 47 | + .default("") |
| 48 | + .transform((v) => (v ? v.split(",") : [])); |
| 49 | + expect(z.object({ a: arr }).partial().parse({})).toEqual({ a: [] }); |
| 50 | + expect(z.object({ a: arr }).partial().parse({ a: undefined })).toEqual({ a: [] }); |
| 51 | + expect(z.object({ a: arr }).partial().parse({ a: "x,y" })).toEqual({ a: ["x", "y"] }); |
| 52 | + expect(arr.optional().parse(undefined)).toEqual([]); |
| 53 | + expect( |
| 54 | + z |
| 55 | + .string() |
| 56 | + .prefault("hi") |
| 57 | + .transform((v) => v.length) |
| 58 | + .optional() |
| 59 | + .parse(undefined) |
| 60 | + ).toBe(2); |
| 61 | +}); |
| 62 | + |
| 63 | +test("ladder: key-level admissibility across every rung", () => { |
| 64 | + expect(z.object({ a: z.string().default("D") }).parse({})).toEqual({ a: "D" }); |
| 65 | + expect(z.object({ a: z.string().prefault("P") }).parse({})).toEqual({ a: "P" }); |
| 66 | + expect(z.object({ a: z.string().optional() }).parse({})).toEqual({}); |
| 67 | + expect(z.object({ a: z.transform(() => "T") }).parse({})).toEqual({ a: "T" }); |
| 68 | + expect(z.object({ a: z.string().catch("C") }).parse({})).toEqual({ a: "C" }); |
| 69 | + expect(z.object({ a: z.string() }).safeParse({}).success).toBe(false); |
| 70 | +}); |
| 71 | + |
| 72 | +test("ladder: exactOptional keeps its distinct meaning", () => { |
| 73 | + expect(z.object({ a: z.string().exactOptional() }).parse({})).toEqual({}); |
| 74 | + expect(z.object({ a: z.string().exactOptional() }).safeParse({ a: undefined }).success).toBe(false); |
| 75 | +}); |
| 76 | + |
| 77 | +test("ladder: tuple minimum length respects every rung", () => { |
| 78 | + expect(z.tuple([z.string().default("D")]).parse([])).toEqual(["D"]); |
| 79 | + expect(z.tuple([z.string().prefault("P")]).parse([])).toEqual(["P"]); |
| 80 | + expect(z.tuple([z.string().optional()]).parse([])).toEqual([]); |
| 81 | + expect(z.tuple([z.string()]).safeParse([]).success).toBe(false); |
| 82 | + expect(z.tuple([z.string(), z.number().default(1)]).parse(["x"])).toEqual(["x", 1]); |
| 83 | +}); |
| 84 | + |
| 85 | +test("ladder: the #5941 / #5939 clobber invariants still hold", () => { |
| 86 | + expect( |
| 87 | + z |
| 88 | + .preprocess((v) => v ?? "X", z.string()) |
| 89 | + .optional() |
| 90 | + .parse(undefined) |
| 91 | + ).toBeUndefined(); |
| 92 | + expect(z.string().catch("X").optional().parse(undefined)).toBeUndefined(); |
| 93 | + expect( |
| 94 | + z |
| 95 | + .string() |
| 96 | + .catch("X") |
| 97 | + .transform((s) => `${s}!`) |
| 98 | + .optional() |
| 99 | + .parse(undefined) |
| 100 | + ).toBeUndefined(); |
| 101 | + expect( |
| 102 | + z |
| 103 | + .transform(() => "T") |
| 104 | + .optional() |
| 105 | + .parse(undefined) |
| 106 | + ).toBeUndefined(); |
| 107 | + expect( |
| 108 | + z |
| 109 | + .union([z.preprocess((v) => v ?? "X", z.string()), z.number()]) |
| 110 | + .optional() |
| 111 | + .parse(undefined) |
| 112 | + ).toBeUndefined(); |
| 113 | + expect(z.object({ a: z.preprocess((v) => v ?? "X", z.string()) }).parse({})).toEqual({ a: "X" }); |
| 114 | +}); |
| 115 | + |
| 116 | +test("ladder: the top rung survives every wrapper order", () => { |
| 117 | + expect(z.string().default("D").optional().parse(undefined)).toBe("D"); |
| 118 | + expect(z.string().default("D").optional().optional().parse(undefined)).toBe("D"); |
| 119 | + expect(z.string().default("D").nullable().optional().parse(undefined)).toBe("D"); |
| 120 | + expect(z.string().default("D").readonly().optional().parse(undefined)).toBe("D"); |
| 121 | + expect(z.string().default("D").catch("C").optional().parse(undefined)).toBe("D"); |
| 122 | + expect(z.string().default("D").pipe(z.string()).optional().parse(undefined)).toBe("D"); |
| 123 | + expect( |
| 124 | + z |
| 125 | + .union([z.string().default("D"), z.number()]) |
| 126 | + .optional() |
| 127 | + .parse(undefined) |
| 128 | + ).toBe("D"); |
| 129 | + expect( |
| 130 | + z |
| 131 | + .lazy(() => z.string().default("D")) |
| 132 | + .optional() |
| 133 | + .parse(undefined) |
| 134 | + ).toBe("D"); |
| 135 | +}); |
| 136 | + |
| 137 | +test("ladder: record and catchall paths that read optin", () => { |
| 138 | + expect(z.record(z.string(), z.string().default("D")).parse({ k: undefined })).toEqual({ k: "D" }); |
| 139 | + expect(z.object({}).catchall(z.string().default("D")).parse({ x: undefined })).toEqual({ x: "D" }); |
| 140 | +}); |
| 141 | + |
| 142 | +test("ladder: async parity", async () => { |
| 143 | + const a = z |
| 144 | + .string() |
| 145 | + .default("") |
| 146 | + .transform(async (v) => (v ? v.split(",") : [])); |
| 147 | + expect((await a.optional().safeParseAsync(undefined)).data).toEqual([]); |
| 148 | + expect((await z.object({ a }).partial().safeParseAsync({})).data).toEqual({ a: [] }); |
| 149 | + expect( |
| 150 | + ( |
| 151 | + await z |
| 152 | + .preprocess(async (v) => v ?? "X", z.string()) |
| 153 | + .optional() |
| 154 | + .safeParseAsync(undefined) |
| 155 | + ).data |
| 156 | + ).toBeUndefined(); |
| 157 | +}); |
| 158 | + |
| 159 | +test("ladder: the rung values themselves", () => { |
| 160 | + expect(z.string()._zod.optin).toBeUndefined(); |
| 161 | + expect(z.string().optional()._zod.optin).toBe("optional"); |
| 162 | + expect(z.transform(() => 1)._zod.optin).toBe("optional"); |
| 163 | + expect(z.string().catch("C")._zod.optin).toBe("optional"); |
| 164 | + expect(z.string().default("D")._zod.optin).toBe("defaulted"); |
| 165 | + expect(z.string().prefault("P")._zod.optin).toBe("defaulted"); |
| 166 | + expect( |
| 167 | + z |
| 168 | + .string() |
| 169 | + .default("D") |
| 170 | + .transform((v) => v)._zod.optin |
| 171 | + ).toBe("defaulted"); |
| 172 | + expect(z.string().default("D").optional()._zod.optin).toBe("defaulted"); |
| 173 | + expect(z.union([z.string().default("D"), z.number()])._zod.optin).toBe("defaulted"); |
| 174 | + expect(z.union([z.string().optional(), z.number()])._zod.optin).toBe("optional"); |
| 175 | +}); |
0 commit comments