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:

  1. Database drift: You cannot turn a => a.reserved <= a.balance into a Postgres CHECK constraint or a Drizzle WHERE clause, so you write a second copy in SQL.
  2. 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 when status === 'frozen', you write a second canWithdraw(account) function by hand.
  3. Silent invariant bugs: TypeScript checks that account.balance - amount is a number, not that reserved <= balance - amount still holds. If someone accidentally deletes amount <= account.balance - account.reserved, the code compiles cleanly.
  4. 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.