Offer alternative object inputs
Declare a catalogue of ordinary object inputs when users need different fields for different choices. Each variant has a visible label and a hidden type containing a unique, non-empty string. For example, an ISK account can ask for a tax assumption while a VP account asks for a fund fee.
Paste these declarations into the model editor:
const isk = deccf.input.Object({
type: deccf.input.hidden("isk"),
amount: deccf.input.money(2500, deccf.currencies.SEK),
annualReturn: deccf.input.rate(0.02),
taxRate: deccf.input.rate(0.01),
}, {label: "ISK"});
const vp = deccf.input.Object({
type: deccf.input.hidden("vp"),
amount: deccf.input.money(2500, deccf.currencies.SEK),
annualReturn: deccf.input.rate(0.02),
fundFee: deccf.input.rate(0.005),
}, {label: "VP-konto"});
const changeable = deccf.input.objectArray([isk, vp], {type: "manyOfAnyChangeable", title: "Sparande"});
const fixed = deccf.input.objectArray([isk, vp], {type: "manyOfAny", title: "Sparande"});
const unique = deccf.input.objectArray([isk, vp], {type: "anyOf", title: "Sparande"});
const selected = deccf.input.Object([isk, vp], {title: "Sparande"});Register these references in the model's preconditions. input.object is the lowercase alias of input.Object and accepts the same catalogue. input.rate(0.02) means 2%, while input.rate(0.005) means 0.5%. These are illustrative account assumptions, not a tax calculation.
| Declaration | Initial value | Adding and choosing |
|---|---|---|
manyOfAnyChangeable | Empty array | Add creates the first variant. Every card has an editable selector. Repeated types are allowed. |
manyOfAny | Empty array | Add offers the variant labels. Each card keeps its chosen type. Repeated types are allowed. |
anyOf | Empty array | Add offers the variant labels, with existing types disabled. Each type occurs at most once. Removing its card makes it available again. Add is disabled when all types are present. |
Object(variants) | First variant | One always-editable selector, labeled by title, followed by the selected fields in normal layout flow. |
New cards start with the chosen variant's authored defaults: 2,500 SEK and a 2% annual return in this example. They do not use the zero/empty defaults of a new homogeneous-array row. Each new card gets a fresh hidden id unless you declare an explicit key. Saved IDs remain stable. Reusing a variant in several collections leaves its original declaration and the standalone choice unchanged; describing a model generates no IDs.
Changing an editable type preserves the card's identity and common fields with matching names, value types and units or currencies. Here, amount and annualReturn survive a switch. Switching to VP removes taxRate and starts fundFee at 0.5%. Switching back uses the ISK tax default again; earlier edits to inactive fields are not cached. If a preserved value is outside the target field's bounds, its normal validation error remains visible. Nested fields, locks and hidden/readonly values follow the active variant's schema.
Saved values are ordinary objects such as {type:"isk", amount:{amount:2500,currency:"SEK"}, annualReturn:0.02, taxRate:0.01}; collection rows also carry their key. An unknown saved type or malformed saved value stays available for correction and produces a validation error. It is never silently replaced by the first variant. anyOf also rejects repeated types when a calculation receives submitted values. The fixed-type modes restrict card controls; a submitted replacement of a row with another permitted type is still validated as ordinary calculation input.
Empty or malformed catalogues, missing labels, invalid or duplicate hidden type values, and incompatible options produce explicit declaration errors. New variant declarations do not support connectedSource or connectionBinding, including bindings inside their fields. Ordinary connected lists remain supported. An array is a variant catalogue only with one of the three explicit collection modes: existing input.objectArray(rows), table views and ordinary input.Object(fields) keep their existing meaning.
The model description exposes discriminator:'type' and variants:[{value,label,schema}]. Its fallback fields and value describe the first variant; the selected variant determines active fields. Typed collections additionally expose selectionMode matching their declared type, view:'cards', and the alternative object as item.
Use the whole selected row in a repeated calculation when different variants need different financial expressions. rows.item.value supplies an immutable, typed row, including its key, nested values and every active variant field. Money stays Money. Inactive fields are absent, so branch on row.type before reading them. rows.item.data.<field> keeps its existing field-reference meaning; it does not acquire fields from inactive variants.
After the declarations above, this calculates each card's illustrative one-year value and adds the results:
const projected = deccf.calculation({
name: "projected", resultType: "money", bindings: {row: changeable.item.value},
calculate: ({row}) => deccf.money(
row.amount.amount * (1 + row.annualReturn - (row.type === "isk" ? row.taxRate : row.fundFee)),
row.amount.currency,
),
});
const accounts = deccf.repeat(changeable, deccf.component({
calculations: {projected}, outputs: {projected},
}));
const total = deccf.calculation({
name: "total", calculate: deccf.expr.sum(accounts.outputs.projected, {
resultType: "money", currency: deccf.currencies.SEK,
}),
});
return deccf.defineModel({
currency: deccf.currencies.SEK,
timeline: deccf.periods.annual({from: "2026-01", count: 1}),
preconditions: {changeable}, components: {accounts}, calculations: {total},
});Reordering or replacing cards refreshes their values on the next run; deleting cards removes their contribution. An empty collection produces 0 SEK because the sum declares its money type and currency. Nested object views and nested collection items also expose .value within their enclosing repeat scope. Whole-row bindings can be used in cashflows as well as calculations; input validation still follows the selected schema. Money follows the selected run currency just as ordinary input reads do; foreign-currency values stay unchanged. A calculation must be inside the matching repeat: declaring the collection or repeating it elsewhere does not provide a current row. A nested collection item requires its own repeat, while a nested object uses its parent row scope.
Open Savings · alternative account inputs among the included models to try all four modes and see their projected values in a presentation. The complete callable contracts are also in the public API reference.