Ahead-of-time Compiler
Beta
Paseri schemas can be compiled ahead of time into a TypeScript module containing the parser, for faster validation.
There are two ways to do this:
- Via the Vite plugin, which compiles
*.schema.tsfiles automatically as part of your build. This is the simplest option if you already use Vite. - Manually, by calling
toSourceand writing the generated module yourself. Use this if you don’t build with Vite, or want full control over when and how schemas are compiled.
Vite plugin
Section titled “Vite plugin”If you build with Vite, @paseri/vite-plugin 🔗 compiles your schemas automatically, swapping the runtime parser for the ahead-of-time version in production builds. Requires Vite 7+.
deno add jsr:@paseri/paseri jsr:@paseri/vite-pluginbunx jsr add @paseri/paseri @paseri/vite-pluginpnpm add jsr:@paseri/paseri jsr:@paseri/vite-pluginyarn add jsr:@paseri/paseri jsr:@paseri/vite-pluginnpx jsr add @paseri/paseri @paseri/vite-pluginRegister the plugin in your Vite config:
import { paseri } from '@paseri/vite-plugin';
export default { plugins: [paseri()],};Write your schemas in *.schema.ts files:
import * as p from '@paseri/paseri';
export const Greeting = p.object({ hello: p.string(),});Then import and use them as normal:
import { Greeting } from './greeting.schema.ts';
const result = Greeting.safeParse({ hello: 'world' });Manual compilation
Section titled “Manual compilation”Installation
Section titled “Installation”The @paseri/compiler package is a companion to Paseri; install both.
deno add jsr:@paseri/paseri jsr:@paseri/compilerbunx jsr add @paseri/paseri @paseri/compilerpnpm add jsr:@paseri/paseri jsr:@paseri/compileryarn add jsr:@paseri/paseri jsr:@paseri/compilernpx jsr add @paseri/paseri @paseri/compilerCompiling a schema
Section titled “Compiling a schema”Import @paseri/paseri/introspect, then pass schema.toIR() to toSource, which returns the module source as a string:
import * as p from '@paseri/paseri';import '@paseri/paseri/introspect';import { toSource } from '@paseri/compiler';
const schema = p.object({ hello: p.string(),});
const source = toSource(schema.toIR(), { name: 'Greeting' });// Write `source` to a file (e.g. `greeting.ts`) as part of your build.Using the generated module
Section titled “Using the generated module”The generated module exports a single object named after the schema (here, Greeting) that mirrors the runtime schema’s parsing interface, so it is a drop-in replacement for validation.
import { Greeting } from './greeting.ts';
const result = Greeting.safeParse({ hello: 'world' });