dynamic-forms / Function

collectDerivations

Collects all derivation entries from field definitions.

This function traverses the field definition tree and extracts:

  • Shorthand derivation properties on fields
  • Full logic array entries with type: 'derivation'

The collected entries include dependency information for cycle detection and reactive evaluation. Entries are sorted topologically so derivations are processed in dependency order.

Lookup maps (byTarget, byDependency, etc.) are NOT built here - they are computed lazily via {@link DerivationLookup} to avoid hidden mutation and reduce the size of the returned collection.

Presentation

function collectDerivations(
  fields: FieldDef<unknown, FieldMeta>[],
): DerivationCollection;

Returns

DerivationCollection -

Collection containing sorted derivation entries

Parameters

NameTypeDescription
fields
FieldDef<unknown, FieldMeta>[]

Array of field definitions to traverse

Example usage

const fields = [
  { key: 'quantity', type: 'number' },
  { key: 'unitPrice', type: 'number' },
  {
    key: 'total',
    type: 'number',
    derivation: 'formValue.quantity * formValue.unitPrice',
  },
];

const collection = collectDerivations(fields);
// collection.entries has 1 entry for the 'total' field derivation