-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(v4): parseMaybeAsync / safeParseMaybeAsync (JIT-preserving, follow-up to #5924) #5948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dokson
wants to merge
1
commit into
colinhacks:main
Choose a base branch
from
dokson:feat/parse-maybe-async-jit-preserving
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import * as z from "../zod/src/v4/index.js"; | ||
| import { makeData } from "./benchUtil.js"; | ||
| import { metabench } from "./metabench.js"; | ||
|
|
||
| // 1. small flat object — purely sync, exercises ZodObject fastpass directly. | ||
| { | ||
| const schema = z.object({ | ||
| string: z.string(), | ||
| boolean: z.boolean(), | ||
| number: z.number(), | ||
| }); | ||
| const DATA = makeData(1000, () => ({ | ||
| number: Math.random(), | ||
| string: `${Math.random()}`, | ||
| boolean: Math.random() > 0.5, | ||
| })); | ||
| const bench = metabench("flat sync object — safeParse vs safeParseMaybeAsync", { | ||
| safeParse() { | ||
| for (const _ of DATA) schema.safeParse(_); | ||
| }, | ||
| safeParseMaybeAsync() { | ||
| for (const _ of DATA) schema.safeParseMaybeAsync(_); | ||
| }, | ||
| }); | ||
| await bench.run(); | ||
| } | ||
|
|
||
| // 2. nested + discriminated union — sync but non-trivial; proves the | ||
| // fastpass engages end-to-end down the schema tree. | ||
| { | ||
| const inner = z.object({ tag: z.string(), score: z.number() }); | ||
| const schema = z.object({ | ||
| id: z.string(), | ||
| nested: z.object({ a: z.string(), b: z.number(), c: z.boolean() }), | ||
| items: z.array(inner), | ||
| kind: z.discriminatedUnion("type", [ | ||
| z.object({ type: z.literal("a"), x: z.string() }), | ||
| z.object({ type: z.literal("b"), y: z.number() }), | ||
| ]), | ||
| }); | ||
| const DATA = makeData(500, () => ({ | ||
| id: `${Math.random()}`, | ||
| nested: { a: "x", b: 1, c: true }, | ||
| items: [ | ||
| { tag: "p", score: 1 }, | ||
| { tag: "q", score: 2 }, | ||
| ], | ||
| kind: Math.random() > 0.5 ? { type: "a", x: "ok" } : { type: "b", y: 1 }, | ||
| })); | ||
| const bench = metabench("nested + discriminated sync — safeParse vs safeParseMaybeAsync", { | ||
| safeParse() { | ||
| for (const _ of DATA) schema.safeParse(_); | ||
| }, | ||
| safeParseMaybeAsync() { | ||
| for (const _ of DATA) schema.safeParseMaybeAsync(_); | ||
| }, | ||
| }); | ||
| await bench.run(); | ||
| } | ||
|
|
||
| // 3. mostly-sync schema with one deep async refine — quantifies the | ||
| // double-walk cost on the async fallback path vs safeParseAsync. | ||
| { | ||
| const schema = z.object({ | ||
| id: z.string(), | ||
| nested: z.object({ a: z.string(), b: z.number() }), | ||
| tail: z.string().refine(async (s) => s.length >= 0), | ||
| }); | ||
| const DATA = makeData(200, () => ({ | ||
| id: `${Math.random()}`, | ||
| nested: { a: "x", b: 1 }, | ||
| tail: "y", | ||
| })); | ||
| const bench = metabench("mostly-sync + 1 async refine — safeParseAsync vs safeParseMaybeAsync", { | ||
| async safeParseAsync() { | ||
| for (const _ of DATA) await schema.safeParseAsync(_); | ||
| }, | ||
| async safeParseMaybeAsync() { | ||
| for (const _ of DATA) await schema.safeParseMaybeAsync(_); | ||
| }, | ||
| }); | ||
| await bench.run(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/zod/src/v4/classic/tests/parse-maybe-async.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { expect, test } from "vitest"; | ||
| import * as z from "zod/v4"; | ||
|
|
||
| // Tests for #5379. | ||
|
|
||
| test("parseMaybeAsync returns the value sync when no async work is involved", () => { | ||
| const schema = z.string(); | ||
| const result = schema.parseMaybeAsync("hello"); | ||
| expect(result instanceof Promise).toBe(false); | ||
| expect(result).toBe("hello"); | ||
| }); | ||
|
|
||
| test("parseMaybeAsync returns a Promise when an async refinement is encountered", async () => { | ||
| const schema = z.string().refine(async (v) => v.length > 0); | ||
| const result = schema.parseMaybeAsync("hello"); | ||
| expect(result instanceof Promise).toBe(true); | ||
| await expect(result).resolves.toBe("hello"); | ||
| }); | ||
|
|
||
| test("parseMaybeAsync throws sync on validation failure with no async work", () => { | ||
| const schema = z.string(); | ||
| expect(() => schema.parseMaybeAsync(42)).toThrow(z.ZodError); | ||
| }); | ||
|
|
||
| test("parseMaybeAsync rejects with ZodError on async validation failure", async () => { | ||
| const schema = z.string().refine(async (v) => v.length > 5, { message: "too short" }); | ||
| const result = schema.parseMaybeAsync("hi"); | ||
| expect(result instanceof Promise).toBe(true); | ||
| await expect(result).rejects.toThrow(z.ZodError); | ||
| }); | ||
|
|
||
| test("safeParseMaybeAsync mirrors parseMaybeAsync but wraps the result", () => { | ||
| const sync = z.string().safeParseMaybeAsync("hi"); | ||
| expect(sync instanceof Promise).toBe(false); | ||
| expect(sync).toEqual({ success: true, data: "hi" }); | ||
|
|
||
| const failure = z.string().safeParseMaybeAsync(42); | ||
| expect(failure instanceof Promise).toBe(false); | ||
| if (!(failure instanceof Promise)) expect(failure.success).toBe(false); | ||
| }); | ||
|
|
||
| test("safeParseMaybeAsync awaits async refinements (success)", async () => { | ||
| const schema = z.string().refine(async (v) => v.length > 0); | ||
| const result = schema.safeParseMaybeAsync("hi"); | ||
| expect(result instanceof Promise).toBe(true); | ||
| const settled = await result; | ||
| expect(settled).toEqual({ success: true, data: "hi" }); | ||
| }); | ||
|
|
||
| test("safeParseMaybeAsync awaits async refinements (failure)", async () => { | ||
| const schema = z.string().refine(async (v) => v.length > 5, { message: "too short" }); | ||
| const result = schema.safeParseMaybeAsync("hi"); | ||
| expect(result instanceof Promise).toBe(true); | ||
| const settled = await result; | ||
| expect(settled.success).toBe(false); | ||
| if (!settled.success) expect(settled.error.issues[0].message).toBe("too short"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { expect, test } from "vitest"; | ||
| import * as z from "zod/v4-mini"; | ||
|
|
||
| // Mini-side coverage for #5379 — methods are wired the same way on ZodMiniType. | ||
|
|
||
| test("ZodMiniType.parseMaybeAsync stays sync when nothing is async", () => { | ||
| const result = z.string().parseMaybeAsync("hi"); | ||
| expect(result instanceof Promise).toBe(false); | ||
| expect(result).toBe("hi"); | ||
| }); | ||
|
|
||
| test("ZodMiniType.parseMaybeAsync returns a Promise on async refinement", async () => { | ||
| const schema = z.string().check(z.refine(async (v) => v.length > 0)); | ||
| const result = schema.parseMaybeAsync("hi"); | ||
| expect(result instanceof Promise).toBe(true); | ||
| await expect(result).resolves.toBe("hi"); | ||
| }); | ||
|
|
||
| test("ZodMiniType.safeParseMaybeAsync sync success / failure", () => { | ||
| const success = z.string().safeParseMaybeAsync("hi"); | ||
| expect(success instanceof Promise).toBe(false); | ||
| expect(success).toEqual({ success: true, data: "hi" }); | ||
|
|
||
| const failure = z.string().safeParseMaybeAsync(42); | ||
| expect(failure instanceof Promise).toBe(false); | ||
| if (!(failure instanceof Promise)) expect(failure.success).toBe(false); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.