Skip to content
AI Primer
release

Pi releases pi-schema, a JSON Schema validator for runtimes that block eval

Pi released pi-schema, which compiles JSON Schema validation to closures for JavaScript runtimes that block eval. Pi-ai and pi-durable will drop their TypeBox dependency and re-exports, creating a breaking change.

3 min read
Pi releases pi-schema, a JSON Schema validator for runtimes that block eval
Pi releases pi-schema, a JSON Schema validator for runtimes that block eval

TL;DR

  • Pi-schema compiles JSON Schema into closures for JavaScript runtimes that block eval, as described in the launch thread.
  • Strict-CSP performance is the standout: pi-schema keeps sub-microsecond checks across the three scenarios in the launch chart.
  • Pi-ai and pi-durable face an import break: the migration announcement says their TypeBox dependency and re-exports are going away.

The error reporter is built only after a validation failure, so always-valid inputs never pay to construct it. There is a naming gotcha in the builder API: oneOf() emits enum, while union() emits anyOf.

Closure compilation

@earendil-works/pi-schema is a dependency-free builder and validator, installed with npm install @earendil-works/pi-schema. Schemas compile once into ordinary JavaScript closures, without generating source code through eval or new Function.

Mario Zechner, pi-schema’s author, credited SICP and Graal/Truffle as inspirations for the closure approach.

Three API details:

  • Builders return plain, serializable JSON Schema.
  • check() acts as a TypeScript type guard.
  • obj() rejects additional properties by default.

Validation speed

Timings cover boolean checks of valid values, with code generation disabled via --disallow-code-generation-from-strings for the CSP comparison. The suite ran on Node 24.4.1, using one i9-13900 core and the median of 15 rounds after warm-up.

Published validation timings, including TypeBox with runtime code generation blocked

TypeBox retains an advantage on simpler schemas when code generation is available: the README reports 9 ns for a read-tool schema against pi-schema’s 31 ns. Compiling a new schema takes 21 µs for pi-schema versus 203 µs for TypeBox in the same benchmark suite.

Conformance and exclusions

Official JSON Schema core-suite results reported in the launch

The core comparison excludes format tests. With its optional format map, pi-schema separately reports 895 passes from 895 draft 2020-12 format tests.

Pi-schema leaves these features unsupported:

  • Meta-schemas that switch vocabularies on or off through $vocabulary.
  • contentMediaType and contentEncoding assertions, which are optional under the specification.
  • Draft 2019-09’s $recursiveRef.

Heap and bundle size

The published measurements compare retained heap after compiling and checking 3,200 distinct tool-like schemas, plus validator bundle sizes:

| Measurement | pi-schema | TypeBox 1.3.27 | cfworker 4.1.1 | omptype 18.13.3 |
| --- | ---: | ---: | ---: | ---: |
| Retained heap | 7.1 MB | 54.7 MB | 7.6 MB | 20.0 MB |
| Minified, gzipped bundle | 8.5 KB | 35.2 KB | 5.8 KB | 38.8 KB |

Heap measurements used separate processes after garbage collection and excluded the schemas themselves, which occupied 11 MB. Bundle measurements used esbuild 0.28 and gzip -9; pi-schema’s optional format checks were excluded.

TypeBox imports

Pi-ai and pi-durable will stop depending on TypeBox and re-exporting it, according to the migration announcement. Internal schemas are being switched to pi-schema, while existing TypeBox-authored schemas remain compatible.

That compatibility covers TypeBox 1.x JSON Schemas, with two documented exclusions:

  • JavaScript-only types such as BigInt, Undefined, and Function.
  • Refinements and codecs.

AI hill-climbing

Zechner said he had not inspected the code, relying on tests, benchmarks and profiling to drive the implementation. He described his contribution as a single informed prompt, then wrote that the result “does not feel like an accomplishment at all.”

Further reading

Discussion across the web

Where this story is being discussed, in original context.

On X· 7 threads
TL;DR3 posts
Closure compilation1 post
Validation speed1 post
Conformance and exclusions1 post
Heap and bundle size1 post
TypeBox imports1 post
AI hill-climbing2 posts
Share on X