Metakit / Write a rule once

Write a business rule once and run it in JS, SQL, forms, and proofs

Effect Schema already gives you types, runtime decoding, and JSON Schema from one shape—until you add a cross-field rule with Schema.makeFilter or an update function in plain TypeScript. Because JavaScript closures are opaque, you end up repeating the same rule in SQL CHECK/WHERE clauses, in UI disabled checks, and in tests.

@doeixd/metakit extends Effect Schema by keeping those rules and single-step transitions as inspectable data. This tutorial takes you from an ordinary Effect Schema to one rule that validates values, generates Postgres SQL, disables UI actions when a row is frozen, and proves a withdrawal cannot overdraw an account. For the deeper design rationale, see Why Effect Schema needs inspectable expressions; the full runnable version lives in examples/metakit/src/demo.ts.

Prerequisites

Install @doeixd/metakit alongside effect. z3-solver is a general-purpose solver; add it as a dev dependency only if you want counterexamples for transitions the built-in checks cannot decide (this tutorial's proof does not need it).

bash
pnpm add @doeixd/metakit effect
pnpm add -D z3-solver

The four steps below build one file. Keep every block in it, in order, as an ES module (step 4 uses top-level await), and run it with your TypeScript runner. Each step's result is shown in a comment under the code.