Repository navigation
0.13.0
One addition. Nothing changes for a build that does not set it.
Added
-
The
zodandfromoptions: modules that export zod, valibot or TypeBox schemas are compiled at build time, so a page keeps the library it writes schemas in and carries neither it nor a validator runtime.ata({ zod: 'src/events/*.ts' })takes zod 4 modules;ata({ from: 'src/schemas/*.ts' })takes modules of any of the three, each export by its own library. The build loads each module (TypeScript through jiti), converts every exported schema (zod's ownz.toJSONSchema, valibot's@valibot/to-json-schema, a TypeBox schema as written), and writes<module>.validators.mjsand.d.mtsbeside it, one{ validate, isValidObject }per export, which the page's build compiles away like any other validator. The page answers as the library does: the schema written is the input schema, so an unknown key passes as it passesz.objectandv.object(the page leaves it in place where the library would drop it;strictObjectstays closed); where zod writes its own regex beside a format name, the regex alone is kept. Errors come in the plain shape (keyword,instancePath,schemaPath,params,message);validatorOptions: { richErrors: true }asks for the rich one. For a TypeScript module the page's type is the library's own:z.input<...>,InferInput<...>orStatic<...>of the exported schema.A
.refine(fn),.transform(fn), valibotcheckortransformwhose function reads nothing but its own parameter is carried: its text is written into the module and run at its place once the compiled check has passed, in the library's order, a check with its message and path, a transform written into the datavalidate()hands back. A zod.pipe()into a second schema is carried as a second compiled check. Of 337 zod refine calls in 195 public TypeScript files, 258 are such functions, 88 of 153 transforms, and 208 of 503 valibot checks and transforms in 185 files.What a page cannot carry stops the build by export and path rather than compile a page that accepts what the schema rejects: a function that closes over an outside name, a
superRefineor a transform that takes a context, apreprocess, a refine inside a union, a valibot validation written after a transform, a TypeBox kind outside the standard set, aTransform, a$refout of the document, or aformatregistered inFormatRegistry, which a page would pass unchecked.dropRefinements: truetakes them out knowingly, and the build reports which.
Measured on three event schemas, one a union of objects, esbuild minified, gzip level 9, ata-validator 1.63.1, zod 4.6.7: the page carries 3.9 KB with the compiled validators and 93.0 KB with zod; one schema alone is 1.1 KB, an Rspack-built fragment importing one validator 1.4 KB, and three carried refines on a signup schema add 0.5 KB. The same event schema in TypeBox 0.34.52 is 1.1 KB compiled against 25.5 KB with Value.Check on the page; in valibot 1.5.0, which is modular and small, 1.1 KB against 1.7 KB, a saving of 0.6 KB on this schema. Converting a module costs the build 4 ms, after a one-time 132 ms to load jiti and the library.
Not covered: zod 3 (z.toJSONSchema is zod 4's), arktype and effect, the stripping of unknown keys, which the page leaves in place, and a carried check's error in a list where the schema's own checks also fail, which appears once those pass; the verdict is the same.