Metakit / Why rules should be data
The cliff: opaque JavaScript closures
In plain Effect Schema, a cross-field rule is a JavaScript predicate passed to Schema.makeFilter, and a state update is a plain TypeScript function:
ts
import { Schema } from 'effect'
const Account = Schema.Struct({
balance: Schema.Int,
reserved: Schema.Int,
status: Schema.Literals(['open', 'frozen']),
}).pipe(
Schema.check(
Schema.makeFilter(a => a.balance >= 0 && a.reserved >= 0 && a.reserved <= a.balance),
),
)
function withdraw(account: typeof Account.Type, amount: number) {
if (account.status !== 'open' || amount <= 0 || amount > account.balance - account.reserved) {
return null
}
return { ...account, balance: account.balance - amount }
}Both work at runtime when you have every value in memory. Neither can be read by any other tool, because JavaScript functions are black boxes. Four concrete costs follow immediately:
- Database drift: You cannot turn
a => a.reserved <= a.balanceinto a PostgresCHECKconstraint or a DrizzleWHEREclause, so you write a second copy in SQL. - UI and agent drift: You cannot call
withdraw(frozenAccount, ???)before the user types an amount. To disable the "Withdraw" button or hide an AI agent tool whenstatus === 'frozen', you write a secondcanWithdraw(account)function by hand. - Silent invariant bugs: TypeScript checks that
account.balance - amountis anumber, not thatreserved <= balance - amountstill holds. If someone accidentally deletesamount <= account.balance - account.reserved, the code compiles cleanly. - Over-serialization and wasted queries: Because selectors and updates are opaque closures, SSR cannot tell which model fields a page actually needs to serialize for resumability, and a cascading form cannot tell which filter parameters are still missing before issuing a query.