Skip to content

Value Functions Reference

This document covers value functions in Formosaic -- imperative, named functions that compute or set field values at specific lifecycle moments. It also covers how value functions differ from computed values (computedValue).

All value function infrastructure is in packages/core/src/helpers/ValueFunctionRegistry.ts.


Built-in Value Functions

NameWhat It DoesReturnsWhen Triggered
setDateSets the field to the current date/timenew Date()On create / on dependency trigger
setDateIfNullSets the field to the current date/time only if the field has no existing valuenew Date() if value is falsy; otherwise existing valueOn create / on dependency trigger
setLoggedInUserSets the field to the current user's identity object{ id: currentUserId }On create

Example config

json
{
  "createdDate": {
    "type": "DateControl",
    "label": "Created Date",
    "computedValue": "$fn.setDate()",
    "computeOnCreateOnly": true,
    "readOnly": true
  }
}

Custom Value Functions

Registration

typescript
import { registerValueFunctions, ValueFunction } from "@formosaic/core";

const calculateEndDate: ValueFunction = ({ fieldValue, parentEntity }) => {
  const startDate = parentEntity?.["startDate"] as string;
  const durationDays = parentEntity?.["durationDays"] as number;

  if (startDate && durationDays) {
    const end = new Date(startDate);
    end.setDate(end.getDate() + durationDays);
    return end;
  }
  return fieldValue;
};

registerValueFunctions({
  calculateEndDate,
});

Function Signature

typescript
type ValueFunction = (context: {
  fieldName: string;
  fieldValue?: SubEntityType;
  parentEntity?: IEntityData;
  currentUserId?: string;
}) => SubEntityType;

Value Functions vs Computed Values

AspectValue FunctionsComputed Value Expressions
Definition styleImperative: named function registered in codeDeclarative: expression string in JSON config
Config syntaxcomputedValue: "$fn.functionName()"computedValue: "$values.qty * $values.price"
RegistrationregisterValueFunctions({ name: fn })None needed
Access to datafieldName, fieldValue, parentEntity, currentUserIdAll form field values via $values.fieldName
ComplexityUnlimited (full JavaScript/TypeScript)Simple arithmetic and string expressions
computeOnCreateOnly supportYesNo -- always reactive

When to Use Value Functions

  • You need access to currentUserId or parentEntity
  • The computation requires API calls, complex logic, or external dependencies
  • You want the function to run only during entity creation (computeOnCreateOnly: true)

When to Use Computed Value Expressions

  • The value is a simple arithmetic expression based on other form field values
  • You want reactive updates whenever referenced fields change
  • You prefer configuration-only solutions without writing code

Registration API Reference

typescript
function registerValueFunctions(custom: Record<string, ValueFunction>): void;
function getValueFunction(name: string): ValueFunction | undefined;
function executeValueFunction(
  fieldName: string,
  valueFunction: string,
  fieldValue?: SubEntityType,
  parentEntity?: IEntityData,
  currentUserId?: string
): SubEntityType;

Important: registerValueFunctions() merges into the existing registry. Call it at application startup before rendering forms.

Released under the MIT License.