|
1 | 1 | import { expect, expectTypeOf, test } from "vitest"; |
2 | 2 | import { z } from "zod/v4"; |
3 | 3 |
|
| 4 | +test.each([false, true])("undefined prefault preserves object keys (jitless: %s)", async (jitless) => { |
| 5 | + const previous = z.config().jitless; |
| 6 | + z.config({ jitless }); |
| 7 | + try { |
| 8 | + let calls = 0; |
| 9 | + const field = z.union([z.string(), z.undefined()]).prefault(() => { |
| 10 | + calls++; |
| 11 | + return undefined; |
| 12 | + }); |
| 13 | + const schema = z.object({ a: field }); |
| 14 | + expectTypeOf<z.output<typeof field>>().toEqualTypeOf<string | undefined>(); |
| 15 | + expectTypeOf<z.input<typeof schema>>().toEqualTypeOf<{ a?: string | undefined }>(); |
| 16 | + expectTypeOf<z.output<typeof schema>>().toEqualTypeOf<{ a: string | undefined }>(); |
| 17 | + expect(schema.parse({})).toStrictEqual({ a: undefined }); |
| 18 | + expect(calls).toBe(1); |
| 19 | + expect(schema.parse({ a: undefined })).toStrictEqual({ a: undefined }); |
| 20 | + expect(calls).toBe(2); |
| 21 | + expect(await schema.parseAsync({})).toStrictEqual({ a: undefined }); |
| 22 | + expect(calls).toBe(3); |
| 23 | + expect(schema.parse({ a: "value" })).toStrictEqual({ a: "value" }); |
| 24 | + expect(schema.safeParse({ a: 123 }).success).toBe(false); |
| 25 | + expect(calls).toBe(3); |
| 26 | + |
| 27 | + const literal = z.prefault(z.string().optional(), undefined); |
| 28 | + expect(z.core.compileFn(z.object({ a: literal }))({})).toStrictEqual({ a: undefined }); |
| 29 | + for (const wrapped of [literal, literal.readonly(), literal.nullable(), z.lazy(() => literal)]) { |
| 30 | + expect(z.object({ a: wrapped }).parse({})).toStrictEqual({ a: undefined }); |
| 31 | + } |
| 32 | + expect(z.object({ a: literal.optional() }).parse({})).toStrictEqual({}); |
| 33 | + expect(z.object({ a: literal.optional() }).parse({ a: undefined })).toStrictEqual({ a: undefined }); |
| 34 | + expect(z.tuple([literal]).parse([])).toStrictEqual([undefined]); |
| 35 | + } finally { |
| 36 | + z.config({ jitless: previous }); |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +test("prefault preserves transformed undefined output", async () => { |
| 41 | + const field = z |
| 42 | + .string() |
| 43 | + .transform(() => undefined) |
| 44 | + .prefault("fallback"); |
| 45 | + expectTypeOf<z.output<typeof field>>().toEqualTypeOf<undefined>(); |
| 46 | + expect(field.parse(undefined)).toBeUndefined(); |
| 47 | + const schema = z.object({ a: field, optional: z.string().optional() }); |
| 48 | + expectTypeOf<z.output<typeof schema>>().toEqualTypeOf<{ a: undefined; optional?: string | undefined }>(); |
| 49 | + expect(schema.parse({})).toStrictEqual({ a: undefined }); |
| 50 | + expect(z.core.compileFn(schema)({})).toStrictEqual({ a: undefined }); |
| 51 | + expect(z.compile(schema).parse({})).toStrictEqual({ a: undefined }); |
| 52 | + expect( |
| 53 | + await z |
| 54 | + .object({ |
| 55 | + a: z |
| 56 | + .string() |
| 57 | + .transform(async () => undefined) |
| 58 | + .prefault("fallback"), |
| 59 | + }) |
| 60 | + .parseAsync({}) |
| 61 | + ).toStrictEqual({ a: undefined }); |
| 62 | + expect( |
| 63 | + z |
| 64 | + .object({ |
| 65 | + a: z |
| 66 | + .string() |
| 67 | + .prefault("fallback") |
| 68 | + .transform(() => undefined), |
| 69 | + }) |
| 70 | + .parse({}) |
| 71 | + ).toStrictEqual({ a: undefined }); |
| 72 | +}); |
| 73 | + |
4 | 74 | test("basic prefault", () => { |
5 | 75 | const a = z.prefault(z.string().trim(), " default "); |
6 | 76 | expect(a).toBeInstanceOf(z.ZodPrefault); |
|
0 commit comments