|
| 1 | +import { expect, test } from "vitest"; |
| 2 | +import * as z from "zod/v4"; |
| 3 | + |
| 4 | +// Tests for #5379. |
| 5 | + |
| 6 | +test("parseMaybeAsync returns the value sync when no async work is involved", () => { |
| 7 | + const schema = z.string(); |
| 8 | + const result = schema.parseMaybeAsync("hello"); |
| 9 | + expect(result instanceof Promise).toBe(false); |
| 10 | + expect(result).toBe("hello"); |
| 11 | +}); |
| 12 | + |
| 13 | +test("parseMaybeAsync returns a Promise when an async refinement is encountered", async () => { |
| 14 | + const schema = z.string().refine(async (v) => v.length > 0); |
| 15 | + const result = schema.parseMaybeAsync("hello"); |
| 16 | + expect(result instanceof Promise).toBe(true); |
| 17 | + await expect(result).resolves.toBe("hello"); |
| 18 | +}); |
| 19 | + |
| 20 | +test("parseMaybeAsync throws sync on validation failure with no async work", () => { |
| 21 | + const schema = z.string(); |
| 22 | + expect(() => schema.parseMaybeAsync(42)).toThrow(z.ZodError); |
| 23 | +}); |
| 24 | + |
| 25 | +test("parseMaybeAsync rejects with ZodError on async validation failure", async () => { |
| 26 | + const schema = z.string().refine(async (v) => v.length > 5, { message: "too short" }); |
| 27 | + const result = schema.parseMaybeAsync("hi"); |
| 28 | + expect(result instanceof Promise).toBe(true); |
| 29 | + await expect(result).rejects.toThrow(z.ZodError); |
| 30 | +}); |
| 31 | + |
| 32 | +test("safeParseMaybeAsync mirrors parseMaybeAsync but wraps the result", () => { |
| 33 | + const sync = z.string().safeParseMaybeAsync("hi"); |
| 34 | + expect(sync instanceof Promise).toBe(false); |
| 35 | + expect(sync).toEqual({ success: true, data: "hi" }); |
| 36 | + |
| 37 | + const failure = z.string().safeParseMaybeAsync(42); |
| 38 | + expect(failure instanceof Promise).toBe(false); |
| 39 | + if (!(failure instanceof Promise)) expect(failure.success).toBe(false); |
| 40 | +}); |
| 41 | + |
| 42 | +test("safeParseMaybeAsync awaits async refinements (success)", async () => { |
| 43 | + const schema = z.string().refine(async (v) => v.length > 0); |
| 44 | + const result = schema.safeParseMaybeAsync("hi"); |
| 45 | + expect(result instanceof Promise).toBe(true); |
| 46 | + const settled = await result; |
| 47 | + expect(settled).toEqual({ success: true, data: "hi" }); |
| 48 | +}); |
| 49 | + |
| 50 | +test("safeParseMaybeAsync awaits async refinements (failure)", async () => { |
| 51 | + const schema = z.string().refine(async (v) => v.length > 5, { message: "too short" }); |
| 52 | + const result = schema.safeParseMaybeAsync("hi"); |
| 53 | + expect(result instanceof Promise).toBe(true); |
| 54 | + const settled = await result; |
| 55 | + expect(settled.success).toBe(false); |
| 56 | + if (!settled.success) expect(settled.error.issues[0].message).toBe("too short"); |
| 57 | +}); |
| 58 | + |
| 59 | +// Standard Schema integration: validate() now uses safeParseMaybeAsync |
| 60 | +// internally, so a fully-sync schema must NOT return a Promise. |
| 61 | +test("StandardSchema validate is sync when schema has no async work", () => { |
| 62 | + const schema = z.string(); |
| 63 | + const validated = schema["~standard"].validate("hi"); |
| 64 | + expect(validated instanceof Promise).toBe(false); |
| 65 | +}); |
| 66 | + |
| 67 | +test("StandardSchema validate is async when schema has async refinements", async () => { |
| 68 | + const schema = z.string().refine(async (v) => v.length > 0); |
| 69 | + const validated = schema["~standard"].validate("hi"); |
| 70 | + expect(validated instanceof Promise).toBe(true); |
| 71 | + await expect(validated).resolves.toEqual({ value: "hi" }); |
| 72 | +}); |
0 commit comments