Form / Limits and guards from a metakit declaration
Read input attributes from the Schema
Declared.of(form) reads the form's input Schema once, through metakit's
Inspect.fieldSpecs, and gives each key a constraint: its bounds, its literal
options, its domain type, and the HTML attributes those imply. This form edits
an amount of US dollars, which metakit's Money type stores as whole cents. Form.make(name, Form.input(Schema), { inputs }) declares a form over that Schema, with a control for each key under inputs (the next section says what a control is):
import { Schema } from 'effect'
import { Form } from 'foldkit-form'
import { Declared } from 'foldkit-form/metakit'
import { Expr } from '@doeixd/metakit'
import { Money } from '@doeixd/metakit/domain'
const Usd = Money.make('USD')
const WithdrawInput = Schema.Struct({
amount: Usd.pipe(
Expr.refine($ =>
Expr.all(Money.gt($, Money.minor(Usd, 0)), Money.lte($, Money.minor(Usd, 1_000_000))),
),
),
})
const WithdrawForm = Form.make('Withdraw', Form.input(WithdrawInput), {
inputs: Declared.inputs(WithdrawInput),
})
const { constraints } = Declared.of(WithdrawForm)
const { min, max, step } = constraints.amount.attributes
// min: Option.some('0.01'), max: Option.some('10000.00'), step: Option.some('0.01')
// constraints.amount.attributes.type -> Option.some('number')The bounds come out in the units the input shows: dollars, not cents. "Above
zero" became 0.01, because an amount held as whole cents has no value between
0 and 1 cent. Each attribute is an Effect Option, none when the Schema says
nothing about it, so your view adds only the attributes that are there:
h.input([...Option.toArray(Option.map(min, h.Min)), ...Option.toArray(Option.map(max, h.Max))])What each kind of key gets:
| The key's Schema | type | step | Bounds shown as |
|---|---|---|---|
Schema.Int | number | 1 | Whole numbers; an exclusive bound becomes the next one |
Schema.Number | number | any | Numbers; an exclusive bound is left to validation |
Money or Decimal | number | One unit at the scale (0.01) | Whole units (12.50) |
CalendarDay | date | none | Days (2026-01-01) |
| Text, a flag, literals | none | none | none |
HTML has no exclusive bound, so Schema.Number.check(Schema.isLessThan(1))
gives no max; the key's own validation still refuses 1. The constraint keeps
the bound itself as exclusiveMax, with min, max, literals and domain
beside it as Inspect.fieldSpecs read them.
Whether a key must be filled stays the form's own controlsByKey[key].required,
which asks whether an empty draft decodes. The constraint's optional says
only whether the key may be left out: a plain Schema.String may not, yet
accepts an empty draft, so it is not required.