Repository navigation
schema.safeParse on a zod schema is compiled away where the file reads only what the page can give - #23
Merged
Conversation
…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
…e that compares paths; docs: Next.js sizes and the build time it costs
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 wayzodResolver(schema)already is.What is written. The schema becomes a constant with a
safeParseof 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 withcode,path(a key as a string, an index as a number) andmessage, in zod's order.Where. That object is not a
ZodErrorand 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 analysiserrorFieldsuses, given a second shape): every use of the schema in the file isname.safeParse(one argument), and each result is read throughsuccess,data,error.issuesand those three fields.error.flatten()andz.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
safeParsebeside 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 whosepathnames an index, and one whosepathleads into an array.Option and report.
zodSafeParse, default true. The report gets asafeParse:line and one line per schema left as written with its reason; asafeParsethat is not on a zod schema of the project is not mentioned.unplugin-ata scanlists the same without building.Measured (minified, gzipped, zod 4.6.7):
error.issues: 93,079 B as written, 3,329 B through the plugin (esbuild)How it was checked:
test/zod-safe-parse.test.js, 13 tests: generated schemas against zod'ssafeParseandflatten(); 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 namedtoStringorconstructoris kept by the build's check (zod reads it from the prototype).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 fromlib/read througherror.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 whosesafeParseis 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 devwas not run. How manysafeParseuses in real applications fall inside the rule is not measured.