@opencrvs/toolkit 2.0.0-rc.c07ca60 → 2.0.0-rc.c0adb49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -917,6 +917,7 @@ var DateRangeFieldValue = z12.object({
917
917
  var EmailValue = z12.email();
918
918
  var CheckboxFieldValue = z12.boolean();
919
919
  var NumberFieldValue = z12.number();
920
+ var SignatureFieldValue = FileFieldValue;
920
921
  var ButtonFieldValue = z12.number();
921
922
  var VerificationStatusValue = z12.enum([
922
923
  "verified",
@@ -1665,8 +1666,18 @@ var FieldReference = import_v43.default.object({
1665
1666
  $$field: FieldId.describe("Id of the field to reference"),
1666
1667
  $$subfield: import_v43.default.array(import_v43.default.string()).optional().default([]).describe(
1667
1668
  'If the FieldValue is an object, subfield can be used to refer to e.g. `["foo", "bar"]` in `{ foo: { bar: 3 } }`'
1669
+ ),
1670
+ $$code: import_v43.default.string().optional().describe(
1671
+ "Serialised client-side function body. When present the expression is evaluated rather than dereferenced."
1672
+ )
1673
+ }).describe(
1674
+ "Reference to a field value, with an optional client-side computation applied."
1675
+ );
1676
+ var ComputedDefaultValue = import_v43.default.object({
1677
+ $$code: import_v43.default.string().describe(
1678
+ "Serialised client-side function body. Receives (undefined, context) where context includes $now, $online, and system variables."
1668
1679
  )
1669
- }).describe("Reference to a field by its ID");
1680
+ }).describe("A context-only computation used as a field default value.");
1670
1681
  var ValidationConfig = import_v43.default.object({
1671
1682
  validator: Conditional.describe(
1672
1683
  "Conditional expression that must hold for the field value to be considered valid."
@@ -1707,7 +1718,7 @@ var BaseField = import_v43.default.object({
1707
1718
  "Indicates whether the field can be modified during record correction."
1708
1719
  ),
1709
1720
  value: FieldReference.or(import_v43.default.array(FieldReference)).optional().describe(
1710
- "Reference to the source field or fields. When a value is defined, it is copied from the parent field when changed. If multiple references are provided, the first truthy value is used."
1721
+ "Reference to the source field or fields. When a value is defined, it is copied from the parent field when changed. If multiple references are provided, the first truthy value is used. A FieldReference with $$code computes the value via a custom client-side function."
1711
1722
  ),
1712
1723
  analytics: import_v43.default.boolean().default(false).optional().describe(
1713
1724
  "Indicates whether the field is included in analytics. When enabled, its value becomes available in the analytics dashboard."
@@ -1721,7 +1732,7 @@ var Divider = BaseField.extend({
1721
1732
  });
1722
1733
  var TextField = BaseField.extend({
1723
1734
  type: import_v43.default.literal(FieldType.TEXT),
1724
- defaultValue: import_v43.default.union([NonEmptyTextValue, SerializedUserField]).optional(),
1735
+ defaultValue: import_v43.default.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
1725
1736
  configuration: import_v43.default.object({
1726
1737
  maxLength: import_v43.default.number().optional().describe("Maximum length of the text"),
1727
1738
  type: import_v43.default.enum(["text", "password"]).optional(),
@@ -1734,7 +1745,7 @@ var TextField = BaseField.extend({
1734
1745
  });
1735
1746
  var NumberField = BaseField.extend({
1736
1747
  type: import_v43.default.literal(FieldType.NUMBER),
1737
- defaultValue: NumberFieldValue.optional(),
1748
+ defaultValue: NumberFieldValue.or(ComputedDefaultValue).optional(),
1738
1749
  configuration: import_v43.default.object({
1739
1750
  min: import_v43.default.number().optional().describe("Minimum value"),
1740
1751
  max: import_v43.default.number().optional().describe("Maximum value"),
@@ -1747,7 +1758,7 @@ var NumberField = BaseField.extend({
1747
1758
  });
1748
1759
  var TextAreaField = BaseField.extend({
1749
1760
  type: import_v43.default.literal(FieldType.TEXTAREA),
1750
- defaultValue: NonEmptyTextValue.optional(),
1761
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
1751
1762
  configuration: import_v43.default.object({
1752
1763
  maxLength: import_v43.default.number().optional().describe("Maximum length of the text"),
1753
1764
  rows: import_v43.default.number().optional().describe("Number of visible text lines"),
@@ -1781,7 +1792,7 @@ var SignatureField = BaseField.extend({
1781
1792
  signaturePromptLabel: TranslationConfig.describe(
1782
1793
  "Title of the signature modal"
1783
1794
  ),
1784
- defaultValue: FieldValue.optional(),
1795
+ defaultValue: SignatureFieldValue.or(ComputedDefaultValue).optional(),
1785
1796
  configuration: import_v43.default.object({
1786
1797
  maxFileSize: import_v43.default.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
1787
1798
  acceptedFileTypes: MimeType.array().optional().describe("List of allowed file formats for the signature")
@@ -1797,14 +1808,14 @@ var EmailField = BaseField.extend({
1797
1808
  configuration: import_v43.default.object({
1798
1809
  maxLength: import_v43.default.number().optional().describe("Maximum length of the text")
1799
1810
  }).default({ maxLength: 255 }).optional(),
1800
- defaultValue: NonEmptyTextValue.optional()
1811
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
1801
1812
  }).meta({
1802
1813
  description: "An email input field",
1803
1814
  id: "EmailField"
1804
1815
  });
1805
1816
  var DateField = BaseField.extend({
1806
1817
  type: import_v43.default.literal(FieldType.DATE),
1807
- defaultValue: SerializedNowDateTime.or(PlainDate).optional().describe("Default date value(yyyy-MM-dd)"),
1818
+ defaultValue: SerializedNowDateTime.or(PlainDate).or(ComputedDefaultValue).optional().describe("Default date value(yyyy-MM-dd)"),
1808
1819
  configuration: import_v43.default.object({
1809
1820
  notice: TranslationConfig.describe(
1810
1821
  "Text to display above the date input"
@@ -1816,7 +1827,7 @@ var DateField = BaseField.extend({
1816
1827
  });
1817
1828
  var AgeField = BaseField.extend({
1818
1829
  type: import_v43.default.literal(FieldType.AGE),
1819
- defaultValue: NumberFieldValue.optional(),
1830
+ defaultValue: NumberFieldValue.or(ComputedDefaultValue).optional(),
1820
1831
  configuration: import_v43.default.object({
1821
1832
  asOfDate: FieldReference,
1822
1833
  prefix: TranslationConfig.optional(),
@@ -1828,7 +1839,7 @@ var AgeField = BaseField.extend({
1828
1839
  });
1829
1840
  var TimeField = BaseField.extend({
1830
1841
  type: import_v43.default.literal(FieldType.TIME),
1831
- defaultValue: SerializedNowDateTime.or(TimeValue).optional().describe("Default time value (HH-mm)"),
1842
+ defaultValue: SerializedNowDateTime.or(TimeValue).or(ComputedDefaultValue).optional().describe("Default time value (HH-mm)"),
1832
1843
  configuration: import_v43.default.object({
1833
1844
  use12HourFormat: import_v43.default.boolean().optional().describe("Whether to use 12-hour format"),
1834
1845
  notice: TranslationConfig.describe(
@@ -1841,7 +1852,7 @@ var TimeField = BaseField.extend({
1841
1852
  });
1842
1853
  var DateRangeField = BaseField.extend({
1843
1854
  type: import_v43.default.literal(FieldType.DATE_RANGE),
1844
- defaultValue: DateRangeFieldValue.optional(),
1855
+ defaultValue: DateRangeFieldValue.or(ComputedDefaultValue).optional(),
1845
1856
  configuration: import_v43.default.object({
1846
1857
  notice: TranslationConfig.describe(
1847
1858
  "Text to display above the date input"
@@ -1920,7 +1931,7 @@ var PageHeader = BaseField.extend({
1920
1931
  });
1921
1932
  var File = BaseField.extend({
1922
1933
  type: import_v43.default.literal(FieldType.FILE),
1923
- defaultValue: FileFieldValue.optional(),
1934
+ defaultValue: FileFieldValue.or(ComputedDefaultValue).optional(),
1924
1935
  configuration: import_v43.default.object({
1925
1936
  maxFileSize: import_v43.default.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
1926
1937
  acceptedFileTypes: MimeType.array().optional().describe("List of allowed file formats for the signature"),
@@ -1947,7 +1958,7 @@ var SelectOption = import_v43.default.object({
1947
1958
  });
1948
1959
  var NumberWithUnitField = BaseField.extend({
1949
1960
  type: import_v43.default.literal(FieldType.NUMBER_WITH_UNIT),
1950
- defaultValue: NumberWithUnitFieldValue.optional(),
1961
+ defaultValue: NumberWithUnitFieldValue.or(ComputedDefaultValue).optional(),
1951
1962
  options: import_v43.default.array(SelectOption).describe("A list of options for the unit select"),
1952
1963
  configuration: import_v43.default.object({
1953
1964
  min: import_v43.default.number().optional().describe("Minimum value of the number field"),
@@ -1962,7 +1973,7 @@ var NumberWithUnitField = BaseField.extend({
1962
1973
  });
1963
1974
  var RadioGroup = BaseField.extend({
1964
1975
  type: import_v43.default.literal(FieldType.RADIO_GROUP),
1965
- defaultValue: TextValue.optional(),
1976
+ defaultValue: TextValue.or(ComputedDefaultValue).optional(),
1966
1977
  options: import_v43.default.array(SelectOption).describe("A list of options"),
1967
1978
  configuration: import_v43.default.object({
1968
1979
  styles: import_v43.default.object({
@@ -1987,7 +1998,7 @@ var BulletList = BaseField.extend({
1987
1998
  });
1988
1999
  var Select = BaseField.extend({
1989
2000
  type: import_v43.default.literal(FieldType.SELECT),
1990
- defaultValue: TextValue.optional(),
2001
+ defaultValue: TextValue.or(ComputedDefaultValue).optional(),
1991
2002
  options: import_v43.default.array(SelectOption).describe("A list of options"),
1992
2003
  noOptionsMessage: TranslationConfig.optional().describe(
1993
2004
  `
@@ -2009,7 +2020,7 @@ var SelectDateRangeOption = import_v43.default.object({
2009
2020
  });
2010
2021
  var SelectDateRangeField = BaseField.extend({
2011
2022
  type: import_v43.default.literal(FieldType.SELECT_DATE_RANGE),
2012
- defaultValue: SelectDateRangeValue.optional(),
2023
+ defaultValue: SelectDateRangeValue.or(ComputedDefaultValue).optional(),
2013
2024
  options: import_v43.default.array(SelectDateRangeOption).describe("A list of options")
2014
2025
  }).meta({
2015
2026
  description: "A date range selection field",
@@ -2026,7 +2037,7 @@ var NameField = BaseField.extend({
2026
2037
  firstname: SerializedUserField.or(NonEmptyTextValue).optional(),
2027
2038
  middlename: SerializedUserField.or(NonEmptyTextValue).optional(),
2028
2039
  surname: SerializedUserField.or(NonEmptyTextValue).optional()
2029
- }).optional(),
2040
+ }).or(ComputedDefaultValue).optional(),
2030
2041
  configuration: import_v43.default.object({
2031
2042
  name: NameConfig.default({
2032
2043
  firstname: { required: true },
@@ -2050,14 +2061,14 @@ var NameField = BaseField.extend({
2050
2061
  id: "NameField"
2051
2062
  });
2052
2063
  var PhoneField = BaseField.extend({
2053
- defaultValue: NonEmptyTextValue.optional(),
2064
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2054
2065
  type: import_v43.default.literal(FieldType.PHONE)
2055
2066
  }).meta({
2056
2067
  description: "A field for entering a phone number",
2057
2068
  id: "PhoneField"
2058
2069
  });
2059
2070
  var IdField = BaseField.extend({
2060
- defaultValue: NonEmptyTextValue.optional(),
2071
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2061
2072
  type: import_v43.default.literal(FieldType.ID)
2062
2073
  }).meta({
2063
2074
  description: "A field for entering an ID",
@@ -2065,14 +2076,14 @@ var IdField = BaseField.extend({
2065
2076
  });
2066
2077
  var Checkbox = BaseField.extend({
2067
2078
  type: import_v43.default.literal(FieldType.CHECKBOX),
2068
- defaultValue: CheckboxFieldValue.default(false)
2079
+ defaultValue: CheckboxFieldValue.or(ComputedDefaultValue).default(false)
2069
2080
  }).meta({
2070
2081
  description: "A boolean checkbox field",
2071
2082
  id: "Checkbox"
2072
2083
  });
2073
2084
  var Country = BaseField.extend({
2074
2085
  type: import_v43.default.literal(FieldType.COUNTRY),
2075
- defaultValue: NonEmptyTextValue.optional(),
2086
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2076
2087
  optionOverrides: import_v43.default.array(SelectOption.omit({ label: true })).optional().describe(
2077
2088
  "Conditionals for specific countries. Countries not listed are always shown and enabled."
2078
2089
  )
@@ -2090,7 +2101,7 @@ var AdministrativeAreas = import_v43.default.enum([
2090
2101
  ]);
2091
2102
  var AdministrativeAreaField = BaseField.extend({
2092
2103
  type: import_v43.default.literal(FieldType.ADMINISTRATIVE_AREA),
2093
- defaultValue: import_v43.default.union([NonEmptyTextValue, SerializedUserField]).optional(),
2104
+ defaultValue: import_v43.default.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
2094
2105
  configuration: import_v43.default.object({
2095
2106
  partOf: FieldReference.optional().describe("Parent location"),
2096
2107
  type: AdministrativeAreas,
@@ -2102,7 +2113,7 @@ var AdministrativeAreaField = BaseField.extend({
2102
2113
  });
2103
2114
  var LocationInput = BaseField.extend({
2104
2115
  type: import_v43.default.literal(FieldType.LOCATION),
2105
- defaultValue: import_v43.default.union([NonEmptyTextValue, SerializedUserField]).optional(),
2116
+ defaultValue: import_v43.default.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
2106
2117
  configuration: import_v43.default.object({
2107
2118
  locationTypes: import_v43.default.array(import_v43.default.string()).optional().describe("Types of the locations that are available for selection."),
2108
2119
  allowedLocations: AllowedLocations
@@ -2114,7 +2125,7 @@ var LocationInput = BaseField.extend({
2114
2125
  var FileUploadWithOptions = BaseField.extend({
2115
2126
  type: import_v43.default.literal(FieldType.FILE_WITH_OPTIONS),
2116
2127
  options: import_v43.default.array(SelectOption).describe("A list of options"),
2117
- defaultValue: FileFieldWithOptionValue.optional(),
2128
+ defaultValue: FileFieldWithOptionValue.or(ComputedDefaultValue).optional(),
2118
2129
  configuration: import_v43.default.object({
2119
2130
  maxFileSize: import_v43.default.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2120
2131
  maxImageSize: import_v43.default.object({
@@ -2130,12 +2141,12 @@ var FileUploadWithOptions = BaseField.extend({
2130
2141
  });
2131
2142
  var Facility = BaseField.extend({
2132
2143
  type: import_v43.default.literal(FieldType.FACILITY),
2133
- defaultValue: NonEmptyTextValue.optional(),
2144
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2134
2145
  configuration: import_v43.default.object({ allowedLocations: AllowedLocations }).optional()
2135
2146
  }).describe("Input field for a facility");
2136
2147
  var Office = BaseField.extend({
2137
2148
  type: import_v43.default.literal(FieldType.OFFICE),
2138
- defaultValue: NonEmptyTextValue.optional(),
2149
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2139
2150
  configuration: import_v43.default.object({ allowedLocations: AllowedLocations }).optional()
2140
2151
  }).describe("Input field for an office");
2141
2152
  var DefaultAddressFieldValue = DomesticAddressFieldValue.extend({
@@ -2171,7 +2182,7 @@ var Address = BaseField.extend({
2171
2182
  ).optional(),
2172
2183
  allowedLocations: AllowedLocations
2173
2184
  }).optional(),
2174
- defaultValue: DefaultAddressFieldValue.optional()
2185
+ defaultValue: DefaultAddressFieldValue.or(ComputedDefaultValue).optional()
2175
2186
  }).meta({
2176
2187
  description: "Address input field \u2013 a combination of location and text fields",
2177
2188
  id: "Address"
@@ -2218,7 +2229,7 @@ var ButtonConfiguration = import_v43.default.object({
2218
2229
  });
2219
2230
  var ButtonField = BaseField.extend({
2220
2231
  type: import_v43.default.literal(FieldType.BUTTON),
2221
- defaultValue: ButtonFieldValue.optional(),
2232
+ defaultValue: ButtonFieldValue.or(ComputedDefaultValue).optional(),
2222
2233
  configuration: ButtonConfiguration
2223
2234
  }).meta({
2224
2235
  description: "A generic button that can be used to trigger an action",
@@ -2246,7 +2257,7 @@ var AlphaPrintButton = BaseField.extend({
2246
2257
  });
2247
2258
  var HttpField = BaseField.extend({
2248
2259
  type: import_v43.default.literal(FieldType.HTTP),
2249
- defaultValue: HttpFieldValue.optional(),
2260
+ defaultValue: HttpFieldValue.or(ComputedDefaultValue).optional(),
2250
2261
  configuration: import_v43.default.object({
2251
2262
  trigger: FieldReference.optional().describe(
2252
2263
  "Reference to the field that triggers the HTTP request when its value changes. If not provided, the HTTP request is triggered once on component mount."
@@ -2332,7 +2343,7 @@ var LinkButtonField = BaseField.extend({
2332
2343
  });
2333
2344
  var VerificationStatus = BaseField.extend({
2334
2345
  type: import_v43.default.literal(FieldType.VERIFICATION_STATUS),
2335
- defaultValue: VerificationStatusValue.optional(),
2346
+ defaultValue: VerificationStatusValue.or(ComputedDefaultValue).optional(),
2336
2347
  configuration: import_v43.default.object({
2337
2348
  status: TranslationConfig.describe("Text to display on the status pill."),
2338
2349
  description: TranslationConfig.describe(
@@ -2354,7 +2365,7 @@ var QueryParamReaderField = BaseField.extend({
2354
2365
  });
2355
2366
  var QrReaderField = BaseField.extend({
2356
2367
  type: import_v43.default.literal(FieldType.QR_READER),
2357
- defaultValue: QrReaderFieldValue.optional(),
2368
+ defaultValue: QrReaderFieldValue.or(ComputedDefaultValue).optional(),
2358
2369
  configuration: import_v43.default.object({
2359
2370
  validator: import_v43.default.any().meta({
2360
2371
  description: "JSON Schema to validate the scanned QR code data against before populating the form fields.",
@@ -2367,7 +2378,7 @@ var QrReaderField = BaseField.extend({
2367
2378
  });
2368
2379
  var IdReaderField = BaseField.extend({
2369
2380
  type: import_v43.default.literal(FieldType.ID_READER),
2370
- defaultValue: IdReaderFieldValue.optional(),
2381
+ defaultValue: IdReaderFieldValue.or(ComputedDefaultValue).optional(),
2371
2382
  methods: import_v43.default.array(
2372
2383
  import_v43.default.union([QrReaderField, LinkButtonField]).describe("Methods for reading an ID")
2373
2384
  )
@@ -3032,7 +3043,37 @@ function resolveDataPath(rootData, dataPath, instancePath) {
3032
3043
  }
3033
3044
  return current;
3034
3045
  }
3046
+ function todayISO() {
3047
+ return (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" });
3048
+ }
3049
+ var compiledFunctionCache = /* @__PURE__ */ new Map();
3050
+ function compileClientFunction(code) {
3051
+ let fn = compiledFunctionCache.get(code);
3052
+ if (!fn) {
3053
+ fn = new Function(`return (${code})`)();
3054
+ compiledFunctionCache.set(code, fn);
3055
+ }
3056
+ return fn;
3057
+ }
3035
3058
  (0, import_ajv_formats.default)(ajv);
3059
+ function buildClientFunctionContext(input) {
3060
+ return {
3061
+ $form: input.form,
3062
+ $now: todayISO(),
3063
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
3064
+ $online: isOnline(),
3065
+ $user: input.validatorContext?.user,
3066
+ $event: input.validatorContext?.event,
3067
+ $leafAdminStructureLocationIds: input.validatorContext?.leafAdminStructureLocationIds ?? [],
3068
+ user: input.systemVariables?.user,
3069
+ $window: input.systemVariables?.$window,
3070
+ locations: input.locations,
3071
+ adminLevelIds: input.adminLevelIds
3072
+ };
3073
+ }
3074
+ function runClientFunction(code, data, context) {
3075
+ return compileClientFunction(code)(data, context);
3076
+ }
3036
3077
  ajv.addKeyword({
3037
3078
  keyword: "daysFromDate",
3038
3079
  type: "string",
@@ -3077,12 +3118,35 @@ ajv.addKeyword({
3077
3118
  $data: true,
3078
3119
  errors: true,
3079
3120
  // @ts-ignore -- Force type. We will move this away from AJV next. Parsing the array will take seconds and is only called by core.
3080
- validate(schema, data, _2, dataContext) {
3121
+ validate(_schema, data, _2, dataContext) {
3081
3122
  const locationIdInput = data;
3082
3123
  const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
3083
3124
  return locations.some((location) => location.id === locationIdInput);
3084
3125
  }
3085
3126
  });
3127
+ ajv.addKeyword({
3128
+ keyword: "customClientValidator",
3129
+ schemaType: "object",
3130
+ errors: true,
3131
+ // @ts-expect-error -- AJV's public types don't expose `rootData`. All
3132
+ // `validate()` callers build root data via `buildClientFunctionContext`,
3133
+ // so the cast holds.
3134
+ validate(schema, data, _2, dataContext) {
3135
+ return Boolean(
3136
+ runClientFunction(
3137
+ schema.code,
3138
+ data,
3139
+ dataContext?.rootData ?? buildClientFunctionContext({ form: {} })
3140
+ )
3141
+ );
3142
+ }
3143
+ });
3144
+ function isOnline() {
3145
+ if (typeof window !== "undefined" && typeof navigator !== "undefined") {
3146
+ return navigator.onLine;
3147
+ }
3148
+ return true;
3149
+ }
3086
3150
 
3087
3151
  // ../commons/src/utils.ts
3088
3152
  var z32 = __toESM(require("zod/v4"));
@@ -3701,7 +3765,54 @@ function createFieldConditionals(fieldId) {
3701
3765
  }
3702
3766
  },
3703
3767
  required: [fieldId]
3704
- })
3768
+ }),
3769
+ /**
3770
+ * Custom client-side validator. The provided function is serialised and executed
3771
+ * just-in-time on the client only. External references (e.g. lodash) are not
3772
+ * available inside the function body — all logic must be self-contained.
3773
+ *
3774
+ * @example
3775
+ * field('nid').customClientValidator((value) => {
3776
+ * // LUHN check — all logic must be inline
3777
+ * const digits = String(value).split('').map(Number)
3778
+ * // ...
3779
+ * return isValid
3780
+ * })
3781
+ */
3782
+ customClientValidator(validationFn) {
3783
+ const code = validationFn.toString();
3784
+ return defineFormConditional({
3785
+ type: "object",
3786
+ properties: wrapToPath(
3787
+ { [fieldId]: { customClientValidator: { code } } },
3788
+ this.$$subfield
3789
+ ),
3790
+ required: [fieldId]
3791
+ });
3792
+ },
3793
+ /**
3794
+ * Custom client-side evaluation. Returns a {@link FieldReference} descriptor
3795
+ * that can be used as the `value` property or a DATA component entry.
3796
+ * The function receives the referenced field's value as the first argument and
3797
+ * the full form context as the second; its return value replaces the field reference.
3798
+ * The function is serialised and executed just-in-time on the client only.
3799
+ * External references (e.g. lodash) are not available inside the function body.
3800
+ *
3801
+ * For computing a default value without referencing a specific field, use
3802
+ * `evaluate(fn)` in the `defaultValue` property instead.
3803
+ *
3804
+ * @example
3805
+ * field('a').customClientEvaluation((aValue, ctx) =>
3806
+ * Number(aValue) + Number(ctx.$form.b)
3807
+ * )
3808
+ */
3809
+ customClientEvaluation(computationFn) {
3810
+ return {
3811
+ $$code: computationFn.toString(),
3812
+ $$field: fieldId,
3813
+ $$subfield: this.$$subfield
3814
+ };
3815
+ }
3705
3816
  };
3706
3817
  }
3707
3818
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencrvs/toolkit",
3
- "version": "2.0.0-rc.c07ca60",
3
+ "version": "2.0.0-rc.c0adb49",
4
4
  "description": "OpenCRVS toolkit for building country configurations",
5
5
  "license": "MPL-2.0",
6
6
  "bin": {