Form / Limits and guards from a metakit declaration

Show a rule over two keys beside both

A rule written on the input struct itself, such as "leave after you arrive", fails no single key, so the form shows it only in model.errors, after a submit. failures(model) reports each such rule the values entered so far break, with the keys it reads, and field(model, key) is the form's own field with those messages added after the key's own errors:

ts
import { CalendarDay } from '@doeixd/metakit/domain'

const StayInput = Schema.Struct({ arrives: CalendarDay.schema, leaves: CalendarDay.schema }).pipe(
  Expr.refine($ => CalendarDay.before($.arrives, $.leaves), { message: 'Leave after you arrive' }),
)
const StayForm = Form.make('Stay', Form.input(StayInput), { inputs: Declared.inputs(StayInput) })
const stay = Declared.of(StayForm)

const typed = [
  StayForm.Message.Changed({ key: 'arrives', value: '2026-03-05' }),
  StayForm.Message.Changed({ key: 'leaves', value: '2026-03-01' }),
].reduce((model, message) => StayForm.bundle.update(model, message, undefined).model, StayForm.initial)

stay.failures(typed) // [{ keys: ['arrives', 'leaves'], rule, message: Option.some('Leave after you arrive') }]
stay.field(typed, 'leaves') // { _tag: 'Invalid', value: '2026-03-01', errors: ['Leave after you arrive'] }

A rule is broken when it is False, or when it cannot be worked out over the values entered (a product past ±(2^53−1)), which the Schema rejects on submit too. While leaves is empty, or holds text that is not a date, the rule is undecided and nothing is reported. A rule on one key (Schema.isGreaterThan(0) on guests) is left to that key's own validation, which form.field already shows.

A rule on the input struct given no message has nothing to show. Declared.of throws an error naming such a rule when you call it, unless you pass unworded, which words it:

ts
Declared.of(StayForm, { unworded: failure => `Check ${failure.keys.join(' and ')}` })

foldkit-mixins-form's FormView draws each key from the form's own field. Give it the form DeclaredView.of(StayForm, stay) returns instead (foldkit-mixins-form/declared), and these messages show beside the keys as a key's own errors are shown. A view of your own draws the keys with stay.field.

Next