Skip to content

Schema Compilers

Effect's experimental JIT and AOT compilers generate specialized JavaScript behind the ordinary Schema APIs and fall back to the interpreter for unsupported constructs. Benchmark your own schemas before relying on a speedup. Plain builders follow the call rules.

How compiled fast paths work

// schemas/Parcel.vibe
export const Parcel = Schema.Struct({
  reference: Schema.String,
  count: Schema.Int
})
 
effect function readParcel(input: unknown) {
  return try Schema.decodeUnknownEffect(Parcel)(input)
}
// Run in an effect function:
//   {
//     const jsonValue = try readParcel({ reference: "pkg-94", count: 2 })
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
//   try Effect.catch(readParcel({ reference: "pkg-94", count: "2" }), (error) => Console.log(error._tag))
{"reference":"pkg-94","count":2}
SchemaError

Compiled parsers register against exact schema AST instances:

  • Decoders and constructors run a synchronous fast path; on failure they fall through to the detailed interpreter to build full SchemaIssue trees.
  • Type guards (Schema.is) return false immediately without falling back.
  • Because a failing fast path re-runs checks and property getters in the interpreter, keep them deterministic and side-effect-free. Compilers automatically skip fast paths that would unsafely replay defaults, class construction, transformations, middleware, or effectful work.

Just-in-time (JIT) compilation

// main.vibe
import "effect/schema/SchemaJITCompiler/enable"
 
const CompiledParcel = Schema.Struct({
  reference: Schema.String,
  count: Schema.Int
})
 
effect function receiveCompiled(input: unknown) {
  return try Schema.decodeUnknownEffect(CompiledParcel)(input)
}
// Run in an effect function:
//   {
//     const jsonValue = try receiveCompiled({ reference: "pkg-94", count: 2 })
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
//   try Effect.catch(receiveCompiled({}), (error) => Console.log(error.message))
{"reference":"pkg-94","count":2}
Missing key
  at ["reference"]

Import "effect/schema/SchemaJITCompiler/enable" before creating or running parsers; operations compile lazily on first use.

function enableSelectedDirections() {
  SchemaJITCompiler.enable(Schema.FiniteFromString.ast)
  SchemaJITCompiler.enable(SchemaAST.toType(Schema.FiniteFromString.ast))
  SchemaJITCompiler.enable(SchemaAST.flip(Schema.FiniteFromString.ast))
}
// Run in an effect function:
//   enableSelectedDirections()
//   try Console.log(try Schema.decodeEffect(Schema.FiniteFromString)("2"))
//   try Console.log(Schema.is(Schema.toType(Schema.FiniteFromString))(2))
//   try Console.log(try Schema.encodeEffect(Schema.FiniteFromString)(2))
2
true
2

For targeted compilation, enable each AST direction explicitly: .ast for decoding, SchemaAST.toType(ast) for type guards and construction, and SchemaAST.flip(ast) for encoding. If dynamic function construction is blocked (for example by CSP) or codegen fails, Effect silently stays on the interpreter; exceptions thrown by an already-compiled parser propagate normally.

Ahead-of-time (AOT) compilation

// TypeScript (.ts)
// build-schema-aot.ts
// types: node
import { fileURLToPath } from "node:url"
import { Console, Effect } from "effect"
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"
import * as NodePath from "@effect/platform-node/NodePath"
import * as Build from "effect/schema/SchemaAOTCompiler/Build"
 
export const buildSchemas = () => Effect.provide(
  Build.build({
    modules: {
      "./schemas/Parcel.js": () => import("./schemas/Parcel.js")
    },
    baseUrl: import.meta.url,
    outFile: fileURLToPath(new URL("./generated/parcel-validators.js", import.meta.url)),
    operations: ["decode", "encode", "is", "make"]
  }),
  [NodeFileSystem.layer, NodePath.layer]
)
// Run: return Effect.flatMap(buildSchemas(), (result) => Console.log(`Prepared ${result.schemas} schema exports`))
Prepared 1 schema exports
// build.vibe
import { buildSchemas } from "./build-schema-aot.ts"
 
export effect function main() {
  const build = try sync(() => buildSchemas())
  const result = try build
  try Console.log(`Prepared ${result.schemas} schema exports`)
}
 
await runPromise(main())
// Run in an effect function: void 0
Prepared 1 schema exports

Build.build scans directly exported schemas from each loader (resolved relative to baseUrl, defaulting to operations: ["decode"]) and writes a self-installing ES module. Keep the dynamic import() loaders and NodeFileSystem / NodePath wiring in a .ts helper, and pass an absolute URL-derived outFile so emission does not depend on the process working directory.

Compile the build entry first with a dedicated tsconfig.aot.json so the app does not yet require the generated file:

{
  "extends": "./tsconfig.json",
  "files": ["src/build.vibe"],
  "include": []
}

Run vibelang -p tsconfig.aot.json then node dist/build.js. Set "rootDirs": ["src", "dist"] in your application's tsconfig.json so imports of the generated module resolve cleanly.

// fragment
// application.vibe
import "./generated/parcel-validators.js"
import { Parcel } from "./schemas/Parcel.vibe"
 
effect function receive(input: unknown) {
  return try Schema.decodeUnknownEffect(Parcel)(input)
}

The application excerpt needs the generated validator module from the preceding build. Import it before touching its schemas and preserve its side effect in your bundler. AOT emits static functions (no eval / new Function), but must be regenerated whenever schemas or the Effect version change.

Low-level compiler APIs

import * as SchemaAOTCompiler from "effect/schema/SchemaAOTCompiler"
 
function generateSource() {
  return SchemaAOTCompiler.compile([
    { ast: Parcel.ast, operations: ["decode"] },
    { ast: SchemaAST.toType(Parcel.ast), operations: ["is", "make"] },
    { ast: SchemaAST.flip(Parcel.ast), operations: ["decode"] }
  ])
}
// Run in an effect function: try Console.log(generateSource().includes("export function install"))
true

SchemaAOTCompiler.compile returns raw module source exporting install(asts). Pass the exact same AST array in the exact same order when calling install:

GoalTarget ASTLow-level operation
Decodeoriginal .astdecode
EncodeSchemaAST.flip(ast)decode
Validate decoded valueSchemaAST.toType(ast)is
Construct decoded valueSchemaAST.toType(ast)make

Use SchemaCompiler.set to register custom compiled implementations directly.

Coverage, registry, and limits

effect function parseNormally(input: unknown) {
  return try Schema.decodeUnknownEffect(Parcel)(input, {
    errors: "all", onExcessProperty: "error"
  })
}
// Run in an effect function:
//   {
//     const jsonValue = try parseNormally({ reference: "pkg-94", count: 2 })
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
//   try Effect.catch(parseNormally({ reference: "pkg-94", count: 2, extra: true }), (error) => Console.log(error.message))
{"reference":"pkg-94","count":2}
Expected no excess property
  at ["extra"]
  • Coverage: Primitives, structs, homogeneous arrays, tuples, unions, and template literals compile; transformations, middleware, and declarations typically remain interpreted while eligible child schemas compile.
  • Recursion: JIT compiles Schema.suspend targets lazily; AOT does not invoke suspend thunks, so list suspended schemas as explicit targets if they need AOT compilation.
  • Registry: The interpreter, JIT, AOT, and SchemaCompiler.set share one registry keyed by AST identity. Install compilers at startup before parsers capture entries.
  • Contract: Runtime parse options (including concurrency) still apply. Proxied inputs and patched built-ins fall outside the optimization contract.

The matching Effect page: effect.website/docs/v4/schema/compilers