Skip to content

schema.safeParse on a zod schema is compiled away where the file reads only what the page can give - #23

Merged
mertcanaltin merged 3 commits into
mainfrom
feat/zod-safe-parse
Nov 15, 2026
Merged

mertcanaltin merged 3 commits into
mainfrom
feat/zod-safe-parse

Conversation

@mertcanaltin

@mertcanaltin mertcanaltin commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

A file that checks a value with zod itself, without a form library, kept zod whatever the plugin did: the same six-field signup form was 93,079 B gzipped with and without it. This compiles schema.safeParse(value) the way zodResolver(schema) already is.

What is written. The schema becomes a constant with a safeParse of its own, built on the plan and the page runtime the resolver rewrite uses; the calls stay as written and the zod import leaves the file. It returns { success: true, data } or { success: false, error: { issues } }, each issue with code, path (a key as a string, an index as a number) and message, in zod's order.

Where. That object is not a ZodError and an issue has no other field, so a schema is replaced only where the build sees nothing else is read (src/error-use.js, the analysis errorFields uses, given a second shape): every use of the schema in the file is name.safeParse(one argument), and each result is read through success, data, error.issues and those three fields. error.flatten() and z.flattenError(error) without a mapper are rewritten to a call that makes the same object from the issues. Anything else keeps zod: a result returned, stored or handed to a call, a mapper, treeifyError, prettifyError, format(), error.message, parse(), safeParseAsync, a schema exported or used as a value. The schema can be a constant of the file, one declared in a function, or one imported from a module of the project, as for the resolver.

Held to zod before anything is replaced. The build's check runs a second time in a safeParse mode: zod's own safeParse beside the written one, on the documents made for the form and on values that are not objects, comparing verdict, data and every issue with its path. Two kinds of refine are refused here that a resolver carries, because a path is an array and not a joined name: one whose path names an index, and one whose path leads into an array.

Option and report. zodSafeParse, default true. The report gets a safeParse: line and one line per schema left as written with its reason; a safeParse that is not on a zod schema of the project is not mentioned. unplugin-ata scan lists the same without building.

Measured (minified, gzipped, zod 4.6.7):

  • six-field signup form read through error.issues: 93,079 B as written, 3,329 B through the plugin (esbuild)
  • the README example: 3,048 B esbuild, 3,370 B Vite, 3,047 B webpack, 3,109 B Rspack, zod in none of the bundles

How it was checked:

  • test/zod-safe-parse.test.js, 13 tests: generated schemas against zod's safeParse and flatten(); 19 ways of using a result that keep zod, each asserted to keep it and to be reported; builds with a local schema, one in a function, one imported, one shared with a resolver; a schema with a field named toString or constructor is kept by the build's check (zod reads it from the prototype).
  • Both generated comparisons on eleven seeds by hand: 212,850 answers, no difference.
  • The built signup page beside the same page with real zod on 100,007 generated documents (13,763 valid): no difference.
  • The README example built with webpack and Rspack answers as zod on five documents.
  • npm test: 379 tests, 377 pass, 2 skipped.

Next.js 16.4.6, two client pages (a schema in the file read through error.issues, and one imported from lib/ read through error.flatten().fieldErrors), all client JavaScript of the build, gzipped: 205,915 B to 180,859 B under Turbopack, 309,323 B to 290,142 B under webpack. The chunk that held zod (30,489 B and 24,430 B) is gone in both.

Build time, esbuild, 200 files of one six-field schema each: 82 ms with zodSafeParse: false, about 1.4 s with it, 7 ms a schema, most of it the comparison with zod. 200 files whose safeParse is not zod's: 72 ms to 116 ms, the parse those files were spared before.

Not covered: the Next.js pages were built, not driven in a browser, and next dev was not run. How many safeParse uses in real applications fall inside the rule is not measured.

…d at build time where the file reads only success, data and each issue's code, path and message
…se, and the scan lists the zod schemas a build would compile
@mertcanaltin
mertcanaltin changed the base branch from feat/zod-resolver-zod4 to main November 15, 2026 18:32
…e that compares paths; docs: Next.js sizes and the build time it costs
@mertcanaltin
mertcanaltin merged commit ac53668 into main Nov 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant