zod-compiler 1.22.6 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -2
- package/dist/jit.d.ts +78 -0
- package/dist/jit.d.ts.map +1 -0
- package/dist/jit.js +245 -0
- package/dist/jit.js.map +1 -0
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Keep your existing Zod schemas. Get **2-43x faster** validation. No code changes
|
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
Four ways to use zod-compiler — pick one:
|
|
17
17
|
|
|
18
18
|
### 1. Automatic Mode (Default)
|
|
19
19
|
|
|
@@ -92,6 +92,27 @@ npx zod-compiler generate src/ --schemas explicit --emit bag
|
|
|
92
92
|
npx zod-compiler generate src/ --emit compact
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
+
### 4. Runtime Compilation (No Build Step)
|
|
96
|
+
|
|
97
|
+
`jit()` runs the same pipeline in-process, for `tsx`, `ts-node`, Jest — anywhere no plugin fires:
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { jit } from "zod-compiler/jit";
|
|
101
|
+
|
|
102
|
+
export const UserSchema = jit(z.object({ name: z.string().min(1), email: z.email() }));
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Same validators a build emits, installed on the schema object, so Zod interop is unchanged.
|
|
106
|
+
Compilation is lazy — 0.1-0.3 ms on a schema's first parse; `{ eager: true }` compiles up front,
|
|
107
|
+
`jitAll(namespace)` takes a whole module.
|
|
108
|
+
|
|
109
|
+
The cost is the import: ~570 KB of codegen and `acorn`, **~10 ms of module load**. That suits a
|
|
110
|
+
long-lived process, not a CLI, a cold serverless handler or a browser — use the build plugin there.
|
|
111
|
+
Libraries should ship plain Zod and let the app decide.
|
|
112
|
+
|
|
113
|
+
Needs `new Function`, as Zod's own object fast-path does. `z.config({ jitless: true })` and a CSP
|
|
114
|
+
that blocks eval both leave a working plain-Zod schema.
|
|
115
|
+
|
|
95
116
|
## Build Plugin
|
|
96
117
|
|
|
97
118
|
### Supported Build Tools
|
|
@@ -156,7 +177,8 @@ import zodCompiler from "zod-compiler/bun";
|
|
|
156
177
|
await Bun.build({ entrypoints: ["./src/index.tsx"], outdir: "./dist", plugins: [zodCompiler()] });
|
|
157
178
|
```
|
|
158
179
|
|
|
159
|
-
For code run straight from source (`bun run src/server.ts`) no build plugin fires — use
|
|
180
|
+
For code run straight from source (`bun run src/server.ts`) no build plugin fires — use
|
|
181
|
+
[`jit()`](#4-runtime-compilation-no-build-step) to compile in-process, or the
|
|
160
182
|
[CLI](#3-cli-no-bundler) to compile ahead of time.
|
|
161
183
|
|
|
162
184
|
### Schema Hoisting
|
|
@@ -238,6 +260,9 @@ if (!process.env.ZOD_COMPILER) {
|
|
|
238
260
|
|
|
239
261
|
With `@t3-oss/env-*`, pass `skipValidation: !!process.env.ZOD_COMPILER`.
|
|
240
262
|
|
|
263
|
+
A schema whose SHAPE branches on an env var is baked at build time, and the cache key does not include
|
|
264
|
+
the environment — give each environment its own `cache` directory if you share one across them.
|
|
265
|
+
|
|
241
266
|
### Large projects and CI
|
|
242
267
|
|
|
243
268
|
Discovery executes each schema file inside the bundler's process, so the **first cold run** is the
|
package/dist/jit.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime compilation — the same extract → codegen pipeline the build plugin
|
|
3
|
+
* runs, executed in-process and evaluated through `new Function`.
|
|
4
|
+
*
|
|
5
|
+
* The AOT paths (unplugin, CLI) need a build step to fire. Plenty of everyday
|
|
6
|
+
* code has none: `tsx server.ts`, `node --experimental-strip-types`, a Jest
|
|
7
|
+
* suite, a serverless handler bundled by someone else's toolchain, a library
|
|
8
|
+
* that ships schemas to consumers. There `compile()` is a no-op and every parse
|
|
9
|
+
* runs plain Zod. `jit()` closes that gap — one call, no build integration,
|
|
10
|
+
* measured 3-25x on everyday schemas at ~0.1-0.2 ms of one-time compilation.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here re-implements validation: {@link compileSchemas} and
|
|
13
|
+
* {@link generateIIFE} are the exact modules the plugin and CLI use, so the
|
|
14
|
+
* generated validator, its Zod parity and its performance are identical to what
|
|
15
|
+
* a build would have emitted. The only difference is *when* the code is
|
|
16
|
+
* produced.
|
|
17
|
+
*
|
|
18
|
+
* Compilation is LAZY by default: `jit()` installs accessors that compile on
|
|
19
|
+
* the first read of a parse method and replace themselves with the compiled
|
|
20
|
+
* ones. Importing a module of 500 schemas therefore costs nothing, and a
|
|
21
|
+
* serverless invocation touching three of them pays for three.
|
|
22
|
+
*
|
|
23
|
+
* Runtime code generation is not always permitted — a strict CSP without
|
|
24
|
+
* `unsafe-eval`, some edge runtimes. Zod v4 has the same constraint (its object
|
|
25
|
+
* fast-pass is itself a `new Function`) and already exposes the two switches
|
|
26
|
+
* for it: the `core.util.allowsEval` probe and `z.config({ jitless: true })`.
|
|
27
|
+
* `jit()` honours both and degrades to plain Zod, so one setting governs both
|
|
28
|
+
* compilers. Those targets are where the build plugin belongs anyway — it emits
|
|
29
|
+
* the same validator with no runtime evaluation at all.
|
|
30
|
+
*/
|
|
31
|
+
import { type output, type ZodType } from "zod";
|
|
32
|
+
import type { CompiledSchema } from "./core/types.js";
|
|
33
|
+
export interface JitOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Compile immediately instead of on first use. Costs ~0.1-0.2 ms per schema
|
|
36
|
+
* at import time; useful for a long-lived server that would rather pay during
|
|
37
|
+
* startup than on the first request, or to surface a compilation failure
|
|
38
|
+
* eagerly. Default `false`.
|
|
39
|
+
*/
|
|
40
|
+
eager?: boolean | undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Compile `schema` in-process and install the compiled `parse` / `safeParse` /
|
|
44
|
+
* `parseAsync` / `safeParseAsync` / `is` / `~standard` on it.
|
|
45
|
+
*
|
|
46
|
+
* Returns the SAME object — identity-preserving exactly as the build plugin is,
|
|
47
|
+
* so `.shape`, `_zod`, `instanceof`, `z.toJSONSchema()`, `.meta()` and
|
|
48
|
+
* composition into a larger schema all keep working, and every existing
|
|
49
|
+
* reference to the schema picks the compiled methods up.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { z } from "zod";
|
|
53
|
+
* import { jit } from "zod-compiler/jit";
|
|
54
|
+
*
|
|
55
|
+
* export const UserSchema = jit(z.object({ name: z.string(), email: z.email() }));
|
|
56
|
+
* UserSchema.safeParse(input); // compiled on this first call
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* Schemas the compiler cannot reproduce fall back to Zod per sub-schema, the
|
|
60
|
+
* same way they do at build time; a schema that cannot be compiled at all is
|
|
61
|
+
* left as plain Zod.
|
|
62
|
+
*/
|
|
63
|
+
export declare function jit<T extends ZodType>(schema: T, options?: JitOptions): T & CompiledSchema<output<T>>;
|
|
64
|
+
/**
|
|
65
|
+
* Compile every Zod schema found among an object's own values — typically a
|
|
66
|
+
* module namespace, so a whole schema file opts in with one call:
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import * as schemas from "./schemas.js";
|
|
70
|
+
* jitAll(schemas);
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* The namespace object itself is never written to (a module namespace is
|
|
74
|
+
* read-only); `jit()` mutates the schema objects it holds, which is what every
|
|
75
|
+
* importer of that module already references.
|
|
76
|
+
*/
|
|
77
|
+
export declare function jitAll(schemas: object, options?: JitOptions): void;
|
|
78
|
+
//# sourceMappingURL=jit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jit.d.ts","sourceRoot":"","sources":["../src/jit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAsD,KAAK,MAAM,EAAE,KAAK,OAAO,EAAE,MAAM,KAAK,CAAC;AAYpG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAiCtD,MAAM,WAAW,UAAU;IACzB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,OAAO,EACnC,MAAM,EAAE,CAAC,EACT,OAAO,CAAC,EAAE,UAAU,GACnB,CAAC,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAoD/B;AA0CD;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,IAAI,CAIlE"}
|
package/dist/jit.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime compilation — the same extract → codegen pipeline the build plugin
|
|
3
|
+
* runs, executed in-process and evaluated through `new Function`.
|
|
4
|
+
*
|
|
5
|
+
* The AOT paths (unplugin, CLI) need a build step to fire. Plenty of everyday
|
|
6
|
+
* code has none: `tsx server.ts`, `node --experimental-strip-types`, a Jest
|
|
7
|
+
* suite, a serverless handler bundled by someone else's toolchain, a library
|
|
8
|
+
* that ships schemas to consumers. There `compile()` is a no-op and every parse
|
|
9
|
+
* runs plain Zod. `jit()` closes that gap — one call, no build integration,
|
|
10
|
+
* measured 3-25x on everyday schemas at ~0.1-0.2 ms of one-time compilation.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here re-implements validation: {@link compileSchemas} and
|
|
13
|
+
* {@link generateIIFE} are the exact modules the plugin and CLI use, so the
|
|
14
|
+
* generated validator, its Zod parity and its performance are identical to what
|
|
15
|
+
* a build would have emitted. The only difference is *when* the code is
|
|
16
|
+
* produced.
|
|
17
|
+
*
|
|
18
|
+
* Compilation is LAZY by default: `jit()` installs accessors that compile on
|
|
19
|
+
* the first read of a parse method and replace themselves with the compiled
|
|
20
|
+
* ones. Importing a module of 500 schemas therefore costs nothing, and a
|
|
21
|
+
* serverless invocation touching three of them pays for three.
|
|
22
|
+
*
|
|
23
|
+
* Runtime code generation is not always permitted — a strict CSP without
|
|
24
|
+
* `unsafe-eval`, some edge runtimes. Zod v4 has the same constraint (its object
|
|
25
|
+
* fast-pass is itself a `new Function`) and already exposes the two switches
|
|
26
|
+
* for it: the `core.util.allowsEval` probe and `z.config({ jitless: true })`.
|
|
27
|
+
* `jit()` honours both and degrades to plain Zod, so one setting governs both
|
|
28
|
+
* compilers. Those targets are where the build plugin belongs anyway — it emits
|
|
29
|
+
* the same validator with no runtime evaluation at all.
|
|
30
|
+
*/
|
|
31
|
+
import { config as zodConfig, core as zodCore, ZodRealError } from "zod";
|
|
32
|
+
import { FAIL_CLASS_DECL, FAILZ_CLASS_DECL, FIN_DECL, FIN_DEFERRED_DECL, FINZ_DECL, generateIIFE, MK_VALIDATOR_DECL, ZOD_MSG_DECLARATION, } from "./core/iife.js";
|
|
33
|
+
import { compileSchemas } from "./core/pipeline.js";
|
|
34
|
+
/**
|
|
35
|
+
* The declarations `ZOD_CONFIG_IMPORT` supplies to an emitted module, minus the
|
|
36
|
+
* import itself — `zod`'s three bindings arrive as parameters instead, so the
|
|
37
|
+
* evaluated code has no module scope to resolve. Byte-for-byte the same helper
|
|
38
|
+
* source the CLI emitter writes into a `.compiled.ts`, so a JIT validator and
|
|
39
|
+
* an AOT one share their entire runtime layer.
|
|
40
|
+
*/
|
|
41
|
+
const RUNTIME_PRELUDE = [
|
|
42
|
+
ZOD_MSG_DECLARATION,
|
|
43
|
+
FAIL_CLASS_DECL,
|
|
44
|
+
MK_VALIDATOR_DECL,
|
|
45
|
+
FIN_DECL,
|
|
46
|
+
FIN_DEFERRED_DECL,
|
|
47
|
+
FAILZ_CLASS_DECL,
|
|
48
|
+
FINZ_DECL,
|
|
49
|
+
].join("\n");
|
|
50
|
+
/**
|
|
51
|
+
* Methods `__zcMkv` installs. Each is fronted by a compile-on-read accessor
|
|
52
|
+
* until the schema materializes.
|
|
53
|
+
*
|
|
54
|
+
* `~standard` earns its place: Zod builds it as a closure over `_zod.run`, not
|
|
55
|
+
* over the schema's `safeParse` property, so a Standard Schema consumer (tRPC,
|
|
56
|
+
* Hono, TanStack Form) that never touches `safeParse` would otherwise keep
|
|
57
|
+
* running plain Zod forever behind a "compiled" schema.
|
|
58
|
+
*/
|
|
59
|
+
const SLOTS = ["parse", "safeParse", "parseAsync", "safeParseAsync", "is", "~standard"];
|
|
60
|
+
/** Schemas already handed to `jit()`, so a second call is a no-op rather than a recompile. */
|
|
61
|
+
const seen = new WeakSet();
|
|
62
|
+
/**
|
|
63
|
+
* Compile `schema` in-process and install the compiled `parse` / `safeParse` /
|
|
64
|
+
* `parseAsync` / `safeParseAsync` / `is` / `~standard` on it.
|
|
65
|
+
*
|
|
66
|
+
* Returns the SAME object — identity-preserving exactly as the build plugin is,
|
|
67
|
+
* so `.shape`, `_zod`, `instanceof`, `z.toJSONSchema()`, `.meta()` and
|
|
68
|
+
* composition into a larger schema all keep working, and every existing
|
|
69
|
+
* reference to the schema picks the compiled methods up.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { z } from "zod";
|
|
73
|
+
* import { jit } from "zod-compiler/jit";
|
|
74
|
+
*
|
|
75
|
+
* export const UserSchema = jit(z.object({ name: z.string(), email: z.email() }));
|
|
76
|
+
* UserSchema.safeParse(input); // compiled on this first call
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* Schemas the compiler cannot reproduce fall back to Zod per sub-schema, the
|
|
80
|
+
* same way they do at build time; a schema that cannot be compiled at all is
|
|
81
|
+
* left as plain Zod.
|
|
82
|
+
*/
|
|
83
|
+
export function jit(schema, options) {
|
|
84
|
+
const target = schema;
|
|
85
|
+
if (seen.has(target))
|
|
86
|
+
return schema;
|
|
87
|
+
seen.add(target);
|
|
88
|
+
if (options?.eager === true) {
|
|
89
|
+
materialize(schema);
|
|
90
|
+
return schema;
|
|
91
|
+
}
|
|
92
|
+
// Snapshot Zod's own descriptors first: materialize() restores them before
|
|
93
|
+
// handing the object to `__zcMkv`, so the generated code sees a pristine
|
|
94
|
+
// schema — it captures `~standard`'s original `validate` as its throw path,
|
|
95
|
+
// and capturing a stub there would loop back into itself.
|
|
96
|
+
const original = new Map();
|
|
97
|
+
for (const slot of SLOTS) {
|
|
98
|
+
original.set(slot, Object.getOwnPropertyDescriptor(target, slot));
|
|
99
|
+
}
|
|
100
|
+
// Installing the accessors is the one step that can throw rather than degrade:
|
|
101
|
+
// a slot locked non-configurable (a future Zod, another wrapper) makes
|
|
102
|
+
// defineProperty raise, and `jit()` is called at module scope — so an
|
|
103
|
+
// unhandled throw here takes down the importing app at boot. Roll back to
|
|
104
|
+
// whatever Zod had and leave the schema alone instead.
|
|
105
|
+
let pending = true;
|
|
106
|
+
try {
|
|
107
|
+
installAccessors(target, original, () => {
|
|
108
|
+
if (!pending)
|
|
109
|
+
return;
|
|
110
|
+
pending = false;
|
|
111
|
+
restore(target, original);
|
|
112
|
+
materialize(schema);
|
|
113
|
+
}, () => {
|
|
114
|
+
if (!pending)
|
|
115
|
+
return;
|
|
116
|
+
pending = false;
|
|
117
|
+
// Restore EVERY slot, not just the one being written. A left-behind
|
|
118
|
+
// accessor whose trigger has been cancelled would read `target[slot]`
|
|
119
|
+
// and re-enter itself — unbounded recursion. This is the path the build
|
|
120
|
+
// plugin takes when a file uses `jit()` too: `__zcMkv` assigns the parse
|
|
121
|
+
// methods (cancelling here) and then reads `~standard`.
|
|
122
|
+
restore(target, original);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
pending = false;
|
|
127
|
+
restore(target, original);
|
|
128
|
+
}
|
|
129
|
+
return schema;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Front every installed method with a compile-on-read accessor. `trigger`
|
|
133
|
+
* materializes the schema, which replaces these accessors with the compiled
|
|
134
|
+
* methods (or restores Zod's own), so the read that follows never re-enters.
|
|
135
|
+
*/
|
|
136
|
+
function installAccessors(target, original, trigger, cancel) {
|
|
137
|
+
for (const slot of SLOTS) {
|
|
138
|
+
Object.defineProperty(target, slot, {
|
|
139
|
+
configurable: true,
|
|
140
|
+
// Preserve Zod's own visibility: parse/safeParse/... are enumerable own
|
|
141
|
+
// properties, `~standard` is not. `is` does not exist on a Zod schema, so
|
|
142
|
+
// it follows the non-enumerable convention `compile()` already uses.
|
|
143
|
+
enumerable: original.get(slot)?.enumerable ?? false,
|
|
144
|
+
get() {
|
|
145
|
+
trigger();
|
|
146
|
+
// Whatever now occupies the slot: the compiled method, or — if
|
|
147
|
+
// compilation was impossible — Zod's own, put back by restore().
|
|
148
|
+
return target[slot];
|
|
149
|
+
},
|
|
150
|
+
set(value) {
|
|
151
|
+
// Someone overwrote a method before first use (a test double, another
|
|
152
|
+
// wrapper). Their value wins, and compilation is cancelled outright —
|
|
153
|
+
// materializing later would restore Zod's descriptors over it.
|
|
154
|
+
cancel();
|
|
155
|
+
Object.defineProperty(target, slot, {
|
|
156
|
+
configurable: true,
|
|
157
|
+
enumerable: original.get(slot)?.enumerable ?? false,
|
|
158
|
+
value,
|
|
159
|
+
writable: true,
|
|
160
|
+
});
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Compile every Zod schema found among an object's own values — typically a
|
|
167
|
+
* module namespace, so a whole schema file opts in with one call:
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* import * as schemas from "./schemas.js";
|
|
171
|
+
* jitAll(schemas);
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* The namespace object itself is never written to (a module namespace is
|
|
175
|
+
* read-only); `jit()` mutates the schema objects it holds, which is what every
|
|
176
|
+
* importer of that module already references.
|
|
177
|
+
*/
|
|
178
|
+
export function jitAll(schemas, options) {
|
|
179
|
+
for (const value of Object.values(schemas)) {
|
|
180
|
+
if (isZodSchema(value))
|
|
181
|
+
jit(value, options);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Zod schemas carry `_zod.def`; the same probe auto-discovery uses at build time. */
|
|
185
|
+
function isZodSchema(value) {
|
|
186
|
+
if (typeof value !== "object" || value === null || !("_zod" in value))
|
|
187
|
+
return false;
|
|
188
|
+
const internal = value["_zod"];
|
|
189
|
+
return typeof internal === "object" && internal !== null && "def" in internal;
|
|
190
|
+
}
|
|
191
|
+
/** Put Zod's own descriptors back, dropping the compile-on-read accessors. */
|
|
192
|
+
function restore(target, original) {
|
|
193
|
+
for (const slot of SLOTS) {
|
|
194
|
+
const descriptor = original.get(slot);
|
|
195
|
+
if (descriptor === undefined)
|
|
196
|
+
delete target[slot];
|
|
197
|
+
else
|
|
198
|
+
Object.defineProperty(target, slot, descriptor);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Whether runtime code generation is permitted here. Read per call, never
|
|
203
|
+
* snapshotted: `z.config({ jitless: true })` runs in an entry point, after the
|
|
204
|
+
* schema modules it imports have already been evaluated.
|
|
205
|
+
*/
|
|
206
|
+
function codegenAllowed() {
|
|
207
|
+
return zodCore.globalConfig.jitless !== true && zodCore.util.allowsEval.value;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Run the pipeline and let the generated IIFE install its methods on `schema`.
|
|
211
|
+
* Swallows failure: a schema that cannot be compiled keeps Zod's own methods,
|
|
212
|
+
* which the caller already has, so there is nothing to report and nothing to
|
|
213
|
+
* break.
|
|
214
|
+
*/
|
|
215
|
+
function materialize(schema) {
|
|
216
|
+
if (!codegenAllowed())
|
|
217
|
+
return;
|
|
218
|
+
try {
|
|
219
|
+
buildValidator(schema);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
// Left as plain Zod. Deliberately silent: `jit()` is an optimization, and a
|
|
223
|
+
// schema using a construct the compiler declines is a supported outcome,
|
|
224
|
+
// not an error.
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Generate the validator and evaluate it, reproducing the module a
|
|
229
|
+
* `.compiled.ts` would have been: helper preamble, the file-level shared block,
|
|
230
|
+
* then the `__zcMkv` IIFE whose `__rf[]` bases and install target are the live
|
|
231
|
+
* schema object passed in as `__schema`.
|
|
232
|
+
*/
|
|
233
|
+
function buildValidator(schema) {
|
|
234
|
+
const { schemas, shared } = compileSchemas([{ exportName: "jit", schema }], { mode: "inline" });
|
|
235
|
+
const compiled = schemas[0];
|
|
236
|
+
if (compiled === undefined)
|
|
237
|
+
throw new Error("zod-compiler: schema produced no validator");
|
|
238
|
+
const body = [RUNTIME_PRELUDE, shared.code, `return ${generateIIFE("__schema", compiled)};`].join("\n");
|
|
239
|
+
// The three bindings ZOD_CONFIG_IMPORT would have imported, passed in so the
|
|
240
|
+
// evaluated code needs no module resolution of its own.
|
|
241
|
+
// oxlint-disable-next-line no-new-func -- generating the validator IS the feature
|
|
242
|
+
const factory = new Function("__zodCompilerConfig", "__zcCore", "__zcZodError", "__schema", body);
|
|
243
|
+
factory(zodConfig, zodCore, ZodRealError, schema);
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=jit.js.map
|
package/dist/jit.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jit.js","sourceRoot":"","sources":["../src/jit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,IAAI,IAAI,OAAO,EAAE,YAAY,EAA6B,MAAM,KAAK,CAAC;AACpG,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,QAAQ,EACR,iBAAiB,EACjB,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGpD;;;;;;GAMG;AACH,MAAM,eAAe,GAAG;IACtB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AE;QAAE,OAAO;IAC9B,IAAI,CAAC;QACH,cAAc,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IAAC,MAAM,CAAC;QACP,4EAA4E;QAC5E,yEAAyE;QACzE,gBAAgB;IAClB,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,cAAc,CAAC,MAAe;IACrC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;IAChG,MAAM,QAAQ,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC5B,IAAI,QAAQ,KAAK,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAE1F,MAAM,IAAI,GAAG,CAAC,eAAe,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,YAAY,CAAC,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,CAC/F,IAAI,CACL,CAAC;IAEF,6EAA6E;IAC7E,wDAAwD;IACxD,kFAAkF;IAClF,MAAM,OAAO,GAAG,IAAI,QAAQ,CAC1B,qBAAqB,EACrB,UAAU,EACV,cAAc,EACd,UAAU,EACV,IAAI,CAMM,CAAC;IAEb,OAAO,CAAC,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-compiler",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.0",
|
|
4
4
|
"description": "Compile Zod schemas into zero-overhead validation functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"aot",
|
|
@@ -32,6 +32,12 @@
|
|
|
32
32
|
"import": "./dist/index.js",
|
|
33
33
|
"default": "./dist/index.js"
|
|
34
34
|
},
|
|
35
|
+
"./jit": {
|
|
36
|
+
"types": "./dist/jit.d.ts",
|
|
37
|
+
"source": "./src/jit.ts",
|
|
38
|
+
"import": "./dist/jit.js",
|
|
39
|
+
"default": "./dist/jit.js"
|
|
40
|
+
},
|
|
35
41
|
"./vite": {
|
|
36
42
|
"types": "./dist/unplugin/vite.d.ts",
|
|
37
43
|
"source": "./src/unplugin/vite.ts",
|