Metakit / Why rules should be data
The natural extension: keep Effect Schema, make the rules data
@doeixd/metakit does not replace Effect or effect/Schema. It extends SchemaAST by replacing only the opaque predicate with a small, typed expression tree (Expr) and replacing imperative update logic with a single-step Transition (when + simultaneous apply writes).
Your Account is still an ordinary Effect Schema: Schema.decodeUnknownSync, Schema.is, Arbitrary, Option, Result, and Effect all keep working unchanged.
The imports below come from one package: @doeixd/metakit holds the expression and transition builders (Expr, Transition) and the operation namespaces (Eq for equality, Num for numbers), and each reader lives on its own subpath (@doeixd/metakit/sql, @doeixd/metakit/prove, and so on) so a page loads only the readers it names. $ in Expr.refine($ => …) stands for the value being checked, so $.balance is its balance field.
import { Effect, Result, Schema } from 'effect'
import { Eq, Expr, Num, Path, Transition, Verdict } from '@doeixd/metakit'
import * as Graph from '@doeixd/metakit/graph'
import * as Inspect from '@doeixd/metakit/inspect'
import * as Machine from '@doeixd/metakit/machine'
import * as Prove from '@doeixd/metakit/prove'
import * as Sql from '@doeixd/metakit/sql'
const Account = Schema.Struct({
id: Schema.String,
balance: Schema.Int,
reserved: Schema.Int,
status: Schema.Literals(['open', 'frozen']),
promoEnabled: Schema.Boolean,
}).pipe(
Expr.refine($ =>
Expr.all(Num.gte($.balance, 0), Num.gte($.reserved, 0), Num.lte($.reserved, $.balance)),
),
)
const Withdraw = Transition.make({
state: Account,
input: Schema.Struct({ amount: Schema.Int }),
when: ({ state, input }) =>
Expr.all(
Eq.is(state.status, 'open'),
Num.gt(input.amount, 0),
Num.lte(input.amount, Num.sub(state.balance, state.reserved)),
),
apply: ({ state, input }) => [
Transition.set(state.balance, Num.sub(state.balance, input.amount)),
],
})