zod-aot 0.0.12 → 0.0.13

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 CHANGED
@@ -27,21 +27,21 @@ Measured with `vitest bench` on Node.js (Apple M-series):
27
27
 
28
28
  | Scenario | Zod v4 | zod-aot | Speedup |
29
29
  |---|---|---|----------|
30
- | simple string | 10.4M ops/s | 17.9M ops/s | **1.7x** |
31
- | string (min/max) | 5.3M ops/s | 17.6M ops/s | **3.3x** |
32
- | number (int+positive) | 5.2M ops/s | 16.4M ops/s | **3.1x** |
33
- | enum | 8.7M ops/s | 15.5M ops/s | **1.8x** |
34
- | tuple [string, int, boolean] | 4.3M ops/s | 17.1M ops/s | **3.9x** |
35
- | record\<string, number\> (5 keys) | 1.9M ops/s | 8.3M ops/s | **4.3x** |
36
- | discriminatedUnion (3 variants) | 3.0M ops/s | 15.7M ops/s | **5.3x** |
37
- | medium object (7 props, valid) | 1.7M ops/s | 6.6M ops/s | **4.0x** |
38
- | medium object (7 props, invalid) | 65K ops/s | 1.5M ops/s | **23x** |
39
- | large object (10 nested items) | 111K ops/s | 4.8M ops/s | **43x** |
40
- | large object (100 nested items) | 11.9K ops/s | 713K ops/s | **64x** |
41
- | event log (combined) | 431K ops/s | 5.0M ops/s | **12x** |
42
- | partial fallback object (transform) | 1.2M ops/s | 3.0M ops/s | **2.5x** |
43
- | partial fallback array 10 (transform) | 447K ops/s | 2.1M ops/s | **4.6x** |
44
- | partial fallback array 50 (transform) | 100K ops/s | 467K ops/s | **4.7x** |
30
+ | simple string | 11.1M ops/s | 19.6M ops/s | **1.8x** |
31
+ | string (min/max) | 5.5M ops/s | 20.0M ops/s | **3.6x** |
32
+ | number (int+positive) | 5.5M ops/s | 18.7M ops/s | **3.4x** |
33
+ | enum | 9.1M ops/s | 17.2M ops/s | **1.9x** |
34
+ | tuple [string, int, boolean] | 4.3M ops/s | 18.1M ops/s | **4.2x** |
35
+ | record\<string, number\> (5 keys) | 1.9M ops/s | 7.1M ops/s | **3.7x** |
36
+ | discriminatedUnion (3 variants) | 3.0M ops/s | 17.3M ops/s | **5.8x** |
37
+ | medium object (7 props, valid) | 1.6M ops/s | 6.6M ops/s | **4.1x** |
38
+ | medium object (7 props, invalid) | 63K ops/s | 474K ops/s | **7.5x** |
39
+ | large object (10 nested items) | 107K ops/s | 5.0M ops/s | **46x** |
40
+ | large object (100 nested items) | 11.6K ops/s | 740K ops/s | **64x** |
41
+ | event log (combined) | 442K ops/s | 5.5M ops/s | **12x** |
42
+ | partial fallback object (transform) | 1.7M ops/s | 3.9M ops/s | **2.3x** |
43
+ | partial fallback array 10 (transform) | 139K ops/s | 577K ops/s | **4.1x** |
44
+ | partial fallback array 50 (transform) | 28K ops/s | 119K ops/s | **4.2x** |
45
45
 
46
46
  Performance gains scale with schema complexity. The `discriminatedUnion` optimization uses an O(1) `switch` dispatch instead of Zod's sequential trial approach. Partial fallback schemas (containing `transform`/`refine`) still show 2.5-4.7x speedups by compiling the optimizable portions.
47
47
 
@@ -152,43 +152,6 @@ The plugin:
152
152
  - Adds `/* @__PURE__ */` annotations for tree-shaking
153
153
  - Supports HMR in development
154
154
 
155
- ## Low-Level API
156
-
157
- For custom build scripts or advanced use cases, you can use the extractor and code generator directly:
158
-
159
- ```typescript
160
- import { z } from "zod";
161
- import { extractSchema, generateValidator } from "zod-aot";
162
-
163
- const UserSchema = z.object({
164
- name: z.string().min(3),
165
- age: z.number().int().positive(),
166
- });
167
-
168
- // Extract schema into intermediate representation (IR)
169
- const ir = extractSchema(UserSchema);
170
-
171
- // Generate optimized JavaScript validation code
172
- const { code, functionName } = generateValidator(ir, "validateUser");
173
-
174
- // Execute the generated code
175
- const safeParse = new Function(`${code}\nreturn ${functionName};`)();
176
- const result = safeParse({ name: "Alice", age: 30 });
177
- ```
178
-
179
- ### `createFallback(zodSchema)`
180
-
181
- Wraps a Zod schema to provide the `CompiledSchema` interface, delegating to Zod at runtime. Useful for development or unsupported schema types:
182
-
183
- ```typescript
184
- import { createFallback } from "zod-aot";
185
-
186
- const validator = createFallback(UserSchema);
187
- validator.parse(data); // throws on failure
188
- validator.safeParse(data); // { success, data/error }
189
- validator.is(data); // type guard (boolean)
190
- ```
191
-
192
155
  ## How It Works
193
156
 
194
157
  ```
@@ -270,104 +233,33 @@ Key optimizations in the generated code:
270
233
 
271
234
  ## API Reference
272
235
 
273
- ### `extractSchema(zodSchema): SchemaIR`
236
+ ### `compile<T>(zodSchema): CompiledSchema<T>`
274
237
 
275
- Extracts a JSON-serializable intermediate representation (IR) from a Zod schema by traversing its internal `_zod.def` structure.
238
+ Wraps a Zod schema for AOT compilation. In development, it falls back to Zod's built-in validation. After build (via CLI or unplugin), it uses the generated optimized validator.
276
239
 
277
240
  ```typescript
278
241
  import { z } from "zod";
279
- import { extractSchema } from "zod-aot";
280
-
281
- const ir = extractSchema(z.string().min(3));
282
- // { type: "string", checks: [{ kind: "min_length", minimum: 3 }] }
283
- ```
284
-
285
- Schemas containing `transform`, `refine`, `superRefine`, or `custom` produce a `FallbackIR`:
286
-
287
- ```typescript
288
- const ir = extractSchema(z.string().transform((s) => parseInt(s)));
289
- // { type: "fallback", reason: "transform" }
290
- ```
291
-
292
- ### `generateValidator(ir, name): CodeGenResult`
293
-
294
- Generates optimized JavaScript validation code from a SchemaIR.
295
-
296
- ```typescript
297
- import { generateValidator } from "zod-aot";
298
-
299
- const { code, functionName } = generateValidator(ir, "myValidator");
300
- // code: preamble (var declarations for Sets, RegExps, etc.)
301
- // functionName: full function expression string
302
- ```
303
-
304
- To execute the generated code:
305
-
306
- ```typescript
307
- const safeParse = new Function(`${code}\nreturn ${functionName};`)();
308
- const result = safeParse({ name: "Alice", age: 30 });
309
- ```
310
-
311
- ### `createFallback<T>(zodSchema): CompiledSchema<T>`
312
-
313
- Wraps a Zod schema to provide the `CompiledSchema` interface, delegating to Zod at runtime.
314
-
315
- ```typescript
316
- import { createFallback } from "zod-aot";
242
+ import { compile } from "zod-aot";
317
243
 
318
- const validator = createFallback<User>(UserSchema);
319
- validator.parse(data); // T — throws on failure
320
- validator.safeParse(data); // SafeParseResult<T>
321
- validator.is(data); // input is T (type guard)
322
- validator.schema; // reference to original Zod schema
244
+ const validateUser = compile(z.object({
245
+ name: z.string().min(3),
246
+ age: z.number().int().positive(),
247
+ }));
323
248
  ```
324
249
 
325
250
  ### `CompiledSchema<T>`
326
251
 
327
- The shared interface for both generated validators and runtime fallbacks:
252
+ The interface returned by `compile()`:
328
253
 
329
254
  ```typescript
330
255
  interface CompiledSchema<T> {
331
- parse(input: unknown): T;
332
- safeParse(input: unknown): SafeParseResult<T>;
333
- is(input: unknown): input is T;
334
- schema: unknown;
256
+ parse(input: unknown): T; // throws on failure
257
+ safeParse(input: unknown): SafeParseResult<T>; // { success, data/error }
258
+ is(input: unknown): input is T; // type guard (boolean)
259
+ schema: unknown; // reference to original Zod schema
335
260
  }
336
261
  ```
337
262
 
338
- ### `SchemaIR`
339
-
340
- The intermediate representation — a discriminated union of all supported schema types:
341
-
342
- | IR Type | Zod Equivalent |
343
- |---|---|
344
- | `StringIR` | `z.string()`, `z.email()`, `z.url()`, `z.uuid()` |
345
- | `NumberIR` | `z.number()`, `z.int()` |
346
- | `BooleanIR` | `z.boolean()` |
347
- | `NullIR` | `z.null()` |
348
- | `UndefinedIR` | `z.undefined()` |
349
- | `LiteralIR` | `z.literal(...)` |
350
- | `EnumIR` | `z.enum([...])` |
351
- | `ObjectIR` | `z.object({...})` |
352
- | `ArrayIR` | `z.array(...)` |
353
- | `UnionIR` | `z.union([...])` |
354
- | `OptionalIR` | `.optional()` |
355
- | `NullableIR` | `.nullable()` |
356
- | `AnyIR` | `z.any()` |
357
- | `UnknownIR` | `z.unknown()` |
358
- | `ReadonlyIR` | `.readonly()` |
359
- | `DateIR` | `z.date()` |
360
- | `TupleIR` | `z.tuple([...])` |
361
- | `RecordIR` | `z.record(...)` |
362
- | `DefaultIR` | `.default(...)` |
363
- | `IntersectionIR` | `z.intersection(...)` / `.and(...)` |
364
- | `DiscriminatedUnionIR` | `z.discriminatedUnion(...)` |
365
- | `BigIntIR` | `z.bigint()` |
366
- | `SetIR` | `z.set(...)` |
367
- | `MapIR` | `z.map(...)` |
368
- | `PipeIR` | `.pipe(...)` (non-transform) |
369
- | `FallbackIR` | `transform`, `refine`, `lazy`, etc. |
370
-
371
263
  ## Supported Types
372
264
 
373
265
  | Type | Supported Checks |
@@ -442,7 +334,8 @@ pnpm -r build
442
334
  zod-aot/
443
335
  ├── packages/zod-aot/ # Main npm package
444
336
  │ ├── src/
445
- │ │ ├── index.ts # Public API exports
337
+ │ │ ├── index.ts # Public API exports (zod-aot)
338
+ │ │ ├── internals.ts # Internal API exports (zod-aot/internals)
446
339
  │ │ ├── discovery.ts # Schema discovery (shared by CLI & unplugin)
447
340
  │ │ ├── loader.ts # Runtime-aware file loader
448
341
  │ │ ├── core/ # Pure logic (no CLI/unplugin deps)
package/dist/index.d.ts CHANGED
@@ -1,10 +1,5 @@
1
- export type { CodeGenResult } from "#src/core/codegen/index.js";
2
- export { generateValidator } from "#src/core/codegen/index.js";
3
1
  export { compile, isCompiledSchema } from "#src/core/compile.js";
4
- export type { FallbackEntry } from "#src/core/extractor.js";
5
- export { extractSchema } from "#src/core/extractor.js";
6
- export { createFallback } from "#src/core/runtime.js";
7
- export type { CheckIR, CompiledSchema, DateCheckIR, SafeParseError, SafeParseResult, SafeParseSuccess, SchemaIR, ZodErrorLike, ZodIssueLike, } from "#src/core/types.js";
2
+ export type { CompiledSchema, SafeParseError, SafeParseResult, SafeParseSuccess, ZodErrorLike, ZodIssueLike, } from "#src/core/types.js";
8
3
  export { unplugin } from "#src/unplugin/index.js";
9
4
  export type { ZodAotPluginOptions } from "#src/unplugin/types.js";
10
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACjE,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,YAAY,EACV,OAAO,EACP,cAAc,EACd,WAAW,EACX,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,QAAQ,EACR,YAAY,EACZ,YAAY,GACb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACjE,YAAY,EACV,cAAc,EACd,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,YAAY,GACb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,3 @@
1
- export { generateValidator } from "#src/core/codegen/index.js";
2
1
  export { compile, isCompiledSchema } from "#src/core/compile.js";
3
- export { extractSchema } from "#src/core/extractor.js";
4
- export { createFallback } from "#src/core/runtime.js";
5
2
  export { unplugin } from "#src/unplugin/index.js";
6
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAEjE,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAYtD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AASjE,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type { CodeGenResult } from "#src/core/codegen/index.js";
2
+ export { generateValidator } from "#src/core/codegen/index.js";
3
+ export type { FallbackEntry } from "#src/core/extractor.js";
4
+ export { extractSchema } from "#src/core/extractor.js";
5
+ export { createFallback } from "#src/core/runtime.js";
6
+ export type { CheckIR, DateCheckIR, SchemaIR } from "#src/core/types.js";
7
+ //# sourceMappingURL=internals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internals.d.ts","sourceRoot":"","sources":["../src/internals.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { generateValidator } from "#src/core/codegen/index.js";
2
+ export { extractSchema } from "#src/core/extractor.js";
3
+ export { createFallback } from "#src/core/runtime.js";
4
+ //# sourceMappingURL=internals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internals.js","sourceRoot":"","sources":["../src/internals.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-aot",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "Compile Zod schemas into zero-overhead validation functions at build time",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -57,6 +57,11 @@
57
57
  "types": "./dist/unplugin/rollup.d.ts",
58
58
  "import": "./dist/unplugin/rollup.js",
59
59
  "default": "./dist/unplugin/rollup.js"
60
+ },
61
+ "./internals": {
62
+ "types": "./dist/internals.d.ts",
63
+ "import": "./dist/internals.js",
64
+ "default": "./dist/internals.js"
60
65
  }
61
66
  },
62
67
  "dependencies": {