Skip to content

Conversation

@najuna-brian
Copy link
Contributor

@najuna-brian najuna-brian commented Feb 9, 2026

Description

Adds two core capabilities to the formplayer and formulus extension pipeline:
Closes #304

  1. $data cross-field validation: AJV now supports $data references, enabling schema rules like {"const": {"$data": "1/otherField"}} to validate that one field matches another.
  2. Computed fields engine: Apps can declare auto-calculated fields in ext.json. The formplayer watches dependencies, calls the registered extension function, and updates the field value automatically.

Why

  • QR code validation: Forms used at different points in a project may require the QR code scanned at the end to match the one assigned at the start. $data rules enforce this in the schema with a clear error message no custom code needed per form.
  • Auto-generated PDC codes : Household codes like 2-2-4-A (region-sector-ponto-household) must be computed from multiple fields. The computed fields engine lets apps define this logic in ext.json + a helper JS module, keeping the formplayer generic.

Changes

formulus-formplayer (6 files):

  • App.tsx : Enable $data: true in AJV; expose window.formulusCurrentFormData; add computed fields processing in handleDataChange
  • ExtensionsLoader.ts : Add computedFields to metadata and load result interfaces
  • FormulusInterfaceDefinition.ts : Add computedFields to ExtensionMetadata
  • FormEvaluationContext.tsx : Export shared ExtensionFunction type
  • builtinExtensions.ts : Use shared type, fix lint issues
  • DynamicEnumControl.tsx : Fix lint warnings, apply prettier

formulus (2 files):

  • ExtensionService.ts : Add computedFields to interfaces; pass through from ext.json; deep merge by formType; fix pre-existing type errors
  • FormplayerModal.tsx : Include computedFields in the extensions object sent to the formplayer WebView

How apps use it

$data validation in schema.json:

{ "const": { "$data": "1/qr_captura" } }

Computed fields in ext.json:

"computedFields": {
  "pdc": {
    "pdc_name": {
      "function": "computePdcName",
      "dependencies": ["region", "sector", "ponto", "ponto_colheita", "codigo_amostra"]
    }
  }
}

The function is a standard extension function loaded from a JS module. It receives the full form data and returns the computed value.

@najuna-brian najuna-brian force-pushed the feature/formplayer-ajv-data-support branch from 1a4cf73 to 04d904d Compare February 9, 2026 13:36
@najuna-brian najuna-brian changed the title feat(formplayer): Enable AJV $data cross-field validation feat(formplayer): Enable AJV $data cross-field validation & computed fields engine Feb 9, 2026
@najuna-brian najuna-brian marked this pull request as draft February 9, 2026 17:17
@najuna-brian najuna-brian marked this pull request as draft February 9, 2026 17:17
@najuna-brian najuna-brian marked this pull request as draft February 9, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Formplayer] Support cross-field validation ($data)

1 participant