Metakit / Write a rule once
1. Attach an inspectable invariant to an Effect Schema
An Effect Schema describes the shape of a record. Expr.refine attaches a data-level expression (Expr, a small tree of operations such as Num.gte) to that schema instead of an opaque JavaScript callback, so every tool can read it. In Expr.refine($ => …), $ stands for the record being checked, so $.balance is its balance field.
ts
import { Result, Schema } from 'effect'
import { Eq, Expr, Num, Transition } from '@doeixd/metakit'
export const Account = Schema.Struct({
id: Schema.String,
balance: Schema.Int,
reserved: Schema.Int,
status: Schema.Literals(['open', 'frozen']),
}).pipe(
Expr.refine($ =>
Expr.all(Num.gte($.balance, 0), Num.gte($.reserved, 0), Num.lte($.reserved, $.balance)),
),
)Decoding with Schema.decodeUnknownSync(Account) now enforces balance >= 0, reserved >= 0, and reserved <= balance at runtime, as a Schema.makeFilter rule would. A value that breaks one fails to decode.