@opencrvs/toolkit 1.9.13-rc.f568f39 → 1.9.13-rc.f6200fe

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.
@@ -69,6 +69,7 @@ __export(events_exports, {
69
69
  CheckboxFieldValue: () => CheckboxFieldValue,
70
70
  Clause: () => Clause,
71
71
  ClientSpecificAction: () => ClientSpecificAction,
72
+ ComputedDefaultValue: () => ComputedDefaultValue,
72
73
  Conditional: () => Conditional,
73
74
  ConditionalType: () => ConditionalType,
74
75
  ConfirmableActions: () => ConfirmableActions,
@@ -227,8 +228,10 @@ __export(events_exports, {
227
228
  applyDraftToEventIndex: () => applyDraftToEventIndex,
228
229
  areCertificateConditionsMet: () => areCertificateConditionsMet,
229
230
  areConditionsMet: () => areConditionsMet,
231
+ buildClientFunctionContext: () => buildClientFunctionContext,
230
232
  buildFormState: () => buildFormState,
231
233
  canUserReadEvent: () => canUserReadEvent,
234
+ compileClientFunction: () => compileClientFunction,
232
235
  compositeFieldTypes: () => compositeFieldTypes,
233
236
  configurableEventScopeAllowed: () => configurableEventScopeAllowed,
234
237
  createEmptyDraft: () => createEmptyDraft,
@@ -250,6 +253,7 @@ __export(events_exports, {
250
253
  defineWorkqueuesColumns: () => defineWorkqueuesColumns,
251
254
  deserializeQuery: () => deserializeQuery,
252
255
  errorMessages: () => errorMessages,
256
+ evaluate: () => evaluate,
253
257
  event: () => event,
254
258
  eventMetadataLabelMap: () => eventMetadataLabelMap,
255
259
  eventPayloadGenerator: () => eventPayloadGenerator,
@@ -313,6 +317,7 @@ __export(events_exports, {
313
317
  isBulletListFieldType: () => isBulletListFieldType,
314
318
  isButtonFieldType: () => isButtonFieldType,
315
319
  isCheckboxFieldType: () => isCheckboxFieldType,
320
+ isCodeToEvaluate: () => isCodeToEvaluate,
316
321
  isConditionMet: () => isConditionMet,
317
322
  isCountryFieldType: () => isCountryFieldType,
318
323
  isDataFieldType: () => isDataFieldType,
@@ -379,10 +384,12 @@ __export(events_exports, {
379
384
  or: () => or,
380
385
  plainDateToLocalDate: () => plainDateToLocalDate,
381
386
  resolveDateOfEvent: () => resolveDateOfEvent,
387
+ runClientFunction: () => runClientFunction,
382
388
  runFieldValidations: () => runFieldValidations,
383
389
  runStructuralValidations: () => runStructuralValidations,
384
390
  safeUnion: () => safeUnion,
385
391
  timePeriodToDateRange: () => timePeriodToDateRange,
392
+ todayISO: () => todayISO,
386
393
  user: () => user,
387
394
  validate: () => validate,
388
395
  validateFieldInput: () => validateFieldInput,
@@ -1982,11 +1989,26 @@ var FieldId = import_zod17.z.string().refine(
1982
1989
  })
1983
1990
  ).describe("Unique identifier for the field");
1984
1991
  var FieldReference = import_zod17.z.object({
1985
- $$field: FieldId,
1992
+ $$field: FieldId.describe(
1993
+ "Field whose value is passed as the first argument to the computation"
1994
+ ),
1986
1995
  $$subfield: import_zod17.z.array(import_zod17.z.string()).optional().default([]).describe(
1987
1996
  'If the FieldValue is an object, subfield can be used to refer to e.g. `["foo", "bar"]` in `{ foo: { bar: 3 } }`'
1997
+ ),
1998
+ $$code: import_zod17.z.string().optional().describe(
1999
+ "Serialised client-side function body. When present the expression is evaluated rather than dereferenced."
2000
+ )
2001
+ }).describe(
2002
+ "Reference to a field value, with an optional client-side computation applied."
2003
+ );
2004
+ function isCodeToEvaluate(v) {
2005
+ return !!v && typeof v === "object" && "$$code" in v;
2006
+ }
2007
+ var ComputedDefaultValue = import_zod17.z.object({
2008
+ $$code: import_zod17.z.string().describe(
2009
+ "Serialised client-side function body. Receives (undefined, context) where context includes $now, $online, and system variables."
1988
2010
  )
1989
- }).describe("Reference to a field by its ID");
2011
+ }).describe("A context-only computation used as a field default value.");
1990
2012
  var ValidationConfig = import_zod17.z.object({
1991
2013
  validator: Conditional,
1992
2014
  message: TranslationConfig
@@ -2020,7 +2042,7 @@ var BaseField = import_zod17.z.object({
2020
2042
  "Indicates whether the field can be modified during record correction."
2021
2043
  ),
2022
2044
  value: FieldReference.or(import_zod17.z.array(FieldReference)).optional().describe(
2023
- "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."
2045
+ "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."
2024
2046
  ),
2025
2047
  analytics: import_zod17.z.boolean().default(false).optional().describe(
2026
2048
  "Indicates whether the field is included in analytics. When enabled, its value becomes available in the analytics dashboard."
@@ -2031,7 +2053,7 @@ var Divider = BaseField.extend({
2031
2053
  });
2032
2054
  var TextField = BaseField.extend({
2033
2055
  type: import_zod17.z.literal(FieldType.TEXT),
2034
- defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField]).optional(),
2056
+ defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
2035
2057
  configuration: import_zod17.z.object({
2036
2058
  maxLength: import_zod17.z.number().optional().describe("Maximum length of the text"),
2037
2059
  type: import_zod17.z.enum(["text", "password"]).optional(),
@@ -2041,7 +2063,7 @@ var TextField = BaseField.extend({
2041
2063
  }).describe("Text input");
2042
2064
  var NumberField = BaseField.extend({
2043
2065
  type: import_zod17.z.literal(FieldType.NUMBER),
2044
- defaultValue: NumberFieldValue.optional(),
2066
+ defaultValue: NumberFieldValue.or(ComputedDefaultValue).optional(),
2045
2067
  configuration: import_zod17.z.object({
2046
2068
  min: import_zod17.z.number().optional().describe("Minimum value"),
2047
2069
  max: import_zod17.z.number().optional().describe("Maximum value"),
@@ -2051,7 +2073,7 @@ var NumberField = BaseField.extend({
2051
2073
  }).describe("Number input");
2052
2074
  var TextAreaField = BaseField.extend({
2053
2075
  type: import_zod17.z.literal(FieldType.TEXTAREA),
2054
- defaultValue: NonEmptyTextValue.optional(),
2076
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2055
2077
  configuration: import_zod17.z.object({
2056
2078
  maxLength: import_zod17.z.number().optional().describe("Maximum length of the text"),
2057
2079
  rows: import_zod17.z.number().optional().describe("Number of visible text lines"),
@@ -2082,7 +2104,7 @@ var SignatureField = BaseField.extend({
2082
2104
  signaturePromptLabel: TranslationConfig.describe(
2083
2105
  "Title of the signature modal"
2084
2106
  ),
2085
- defaultValue: SignatureFieldValue.optional(),
2107
+ defaultValue: SignatureFieldValue.or(ComputedDefaultValue).optional(),
2086
2108
  configuration: import_zod17.z.object({
2087
2109
  maxFileSize: import_zod17.z.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2088
2110
  acceptedFileTypes: MimeType.array().optional().describe("List of allowed file formats for the signature")
@@ -2095,11 +2117,11 @@ var EmailField = BaseField.extend({
2095
2117
  configuration: import_zod17.z.object({
2096
2118
  maxLength: import_zod17.z.number().optional().describe("Maximum length of the text")
2097
2119
  }).default({ maxLength: 10 }).optional(),
2098
- defaultValue: NonEmptyTextValue.optional()
2120
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
2099
2121
  });
2100
2122
  var DateField = BaseField.extend({
2101
2123
  type: import_zod17.z.literal(FieldType.DATE),
2102
- defaultValue: SerializedNowDateTime.or(PlainDate).optional().openapi({ effectType: "input", type: "string" }).describe("Default date value(yyyy-MM-dd)"),
2124
+ defaultValue: SerializedNowDateTime.or(PlainDate).or(ComputedDefaultValue).optional().openapi({ effectType: "input", type: "string" }).describe("Default date value(yyyy-MM-dd)"),
2103
2125
  configuration: import_zod17.z.object({
2104
2126
  notice: TranslationConfig.describe(
2105
2127
  "Text to display above the date input"
@@ -2108,7 +2130,7 @@ var DateField = BaseField.extend({
2108
2130
  }).describe("A single date input (yyyy-MM-dd)");
2109
2131
  var AgeField = BaseField.extend({
2110
2132
  type: import_zod17.z.literal(FieldType.AGE),
2111
- defaultValue: NumberFieldValue.optional(),
2133
+ defaultValue: NumberFieldValue.or(ComputedDefaultValue).optional(),
2112
2134
  configuration: import_zod17.z.object({
2113
2135
  asOfDate: FieldReference,
2114
2136
  prefix: TranslationConfig.optional(),
@@ -2117,7 +2139,7 @@ var AgeField = BaseField.extend({
2117
2139
  }).describe("An age input field which uses the current date as the asOfDate");
2118
2140
  var TimeField = BaseField.extend({
2119
2141
  type: import_zod17.z.literal(FieldType.TIME),
2120
- defaultValue: SerializedNowDateTime.or(TimeValue).optional().openapi({ effectType: "input", type: "object" }).describe("Default time value (HH-mm)"),
2142
+ defaultValue: SerializedNowDateTime.or(TimeValue).or(ComputedDefaultValue).optional().openapi({ effectType: "input", type: "object" }).describe("Default time value (HH-mm)"),
2121
2143
  configuration: import_zod17.z.object({
2122
2144
  use12HourFormat: import_zod17.z.boolean().optional().describe("Whether to use 12-hour format"),
2123
2145
  notice: TranslationConfig.describe(
@@ -2127,7 +2149,7 @@ var TimeField = BaseField.extend({
2127
2149
  }).describe("A single date input (HH-mm)");
2128
2150
  var DateRangeField = BaseField.extend({
2129
2151
  type: import_zod17.z.literal(FieldType.DATE_RANGE),
2130
- defaultValue: DateRangeFieldValue.optional(),
2152
+ defaultValue: DateRangeFieldValue.or(ComputedDefaultValue).optional(),
2131
2153
  configuration: import_zod17.z.object({
2132
2154
  notice: TranslationConfig.describe(
2133
2155
  "Text to display above the date input"
@@ -2161,7 +2183,7 @@ var PageHeader = BaseField.extend({
2161
2183
  }).describe("A read-only header component for form pages");
2162
2184
  var File = BaseField.extend({
2163
2185
  type: import_zod17.z.literal(FieldType.FILE),
2164
- defaultValue: FileFieldValue.optional(),
2186
+ defaultValue: FileFieldValue.or(ComputedDefaultValue).optional(),
2165
2187
  configuration: import_zod17.z.object({
2166
2188
  maxFileSize: import_zod17.z.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2167
2189
  acceptedFileTypes: MimeType.array().optional().describe("List of allowed file formats for the signature"),
@@ -2185,7 +2207,7 @@ var SelectOption = import_zod17.z.object({
2185
2207
  });
2186
2208
  var NumberWithUnitField = BaseField.extend({
2187
2209
  type: import_zod17.z.literal(FieldType.NUMBER_WITH_UNIT),
2188
- defaultValue: NumberWithUnitFieldValue.optional(),
2210
+ defaultValue: NumberWithUnitFieldValue.or(ComputedDefaultValue).optional(),
2189
2211
  options: import_zod17.z.array(SelectOption).describe("A list of options for the unit select"),
2190
2212
  configuration: import_zod17.z.object({
2191
2213
  min: import_zod17.z.number().optional().describe("Minimum value of the number field"),
@@ -2197,7 +2219,7 @@ var NumberWithUnitField = BaseField.extend({
2197
2219
  }).describe("Number with unit input");
2198
2220
  var RadioGroup = BaseField.extend({
2199
2221
  type: import_zod17.z.literal(FieldType.RADIO_GROUP),
2200
- defaultValue: TextValue.optional(),
2222
+ defaultValue: TextValue.or(ComputedDefaultValue).optional(),
2201
2223
  options: import_zod17.z.array(SelectOption).describe("A list of options"),
2202
2224
  configuration: import_zod17.z.object({
2203
2225
  styles: import_zod17.z.object({
@@ -2216,7 +2238,7 @@ var BulletList = BaseField.extend({
2216
2238
  }).describe("A list of bullet points");
2217
2239
  var Select = BaseField.extend({
2218
2240
  type: import_zod17.z.literal(FieldType.SELECT),
2219
- defaultValue: TextValue.optional(),
2241
+ defaultValue: TextValue.or(ComputedDefaultValue).optional(),
2220
2242
  options: import_zod17.z.array(SelectOption).describe("A list of options"),
2221
2243
  noOptionsMessage: TranslationConfig.optional().describe(
2222
2244
  `
@@ -2235,7 +2257,7 @@ var SelectDateRangeOption = import_zod17.z.object({
2235
2257
  });
2236
2258
  var SelectDateRangeField = BaseField.extend({
2237
2259
  type: import_zod17.z.literal(FieldType.SELECT_DATE_RANGE),
2238
- defaultValue: SelectDateRangeValue.optional(),
2260
+ defaultValue: SelectDateRangeValue.or(ComputedDefaultValue).optional(),
2239
2261
  options: import_zod17.z.array(SelectDateRangeOption).describe("A list of options")
2240
2262
  }).describe("Select input with date range options");
2241
2263
  var NameConfig = import_zod17.z.object({
@@ -2249,7 +2271,7 @@ var NameField = BaseField.extend({
2249
2271
  firstname: SerializedUserField.or(NonEmptyTextValue).optional(),
2250
2272
  middlename: SerializedUserField.or(NonEmptyTextValue).optional(),
2251
2273
  surname: SerializedUserField.or(NonEmptyTextValue).optional()
2252
- }).optional(),
2274
+ }).or(ComputedDefaultValue).optional(),
2253
2275
  configuration: import_zod17.z.object({
2254
2276
  name: NameConfig.default({
2255
2277
  firstname: { required: true },
@@ -2270,20 +2292,20 @@ var NameField = BaseField.extend({
2270
2292
  }).optional()
2271
2293
  }).describe("Name input field");
2272
2294
  var PhoneField = BaseField.extend({
2273
- defaultValue: NonEmptyTextValue.optional(),
2295
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2274
2296
  type: import_zod17.z.literal(FieldType.PHONE)
2275
2297
  }).describe("Phone input field");
2276
2298
  var IdField = BaseField.extend({
2277
- defaultValue: NonEmptyTextValue.optional(),
2299
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2278
2300
  type: import_zod17.z.literal(FieldType.ID)
2279
2301
  }).describe("ID input field");
2280
2302
  var Checkbox = BaseField.extend({
2281
2303
  type: import_zod17.z.literal(FieldType.CHECKBOX),
2282
- defaultValue: CheckboxFieldValue.default(false)
2304
+ defaultValue: CheckboxFieldValue.or(ComputedDefaultValue).default(false)
2283
2305
  }).describe("Boolean checkbox field");
2284
2306
  var Country = BaseField.extend({
2285
2307
  type: import_zod17.z.literal(FieldType.COUNTRY),
2286
- defaultValue: NonEmptyTextValue.optional(),
2308
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2287
2309
  optionOverrides: import_zod17.z.array(SelectOption.omit({ label: true })).optional().describe(
2288
2310
  "Conditionals for specific countries. Countries not listed are always shown and enabled."
2289
2311
  )
@@ -2299,12 +2321,12 @@ var AdministrativeAreaConfiguration = import_zod17.z.object({
2299
2321
  }).describe("Administrative area options");
2300
2322
  var AdministrativeArea = BaseField.extend({
2301
2323
  type: import_zod17.z.literal(FieldType.ADMINISTRATIVE_AREA),
2302
- defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField]).optional(),
2324
+ defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
2303
2325
  configuration: AdministrativeAreaConfiguration
2304
2326
  }).describe("Administrative area input field e.g. facility, office");
2305
2327
  var LocationInput = BaseField.extend({
2306
2328
  type: import_zod17.z.literal(FieldType.LOCATION),
2307
- defaultValue: NonEmptyTextValue.optional(),
2329
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2308
2330
  configuration: import_zod17.z.object({
2309
2331
  searchableResource: import_zod17.z.array(import_zod17.z.enum(["locations", "facilities", "offices"]))
2310
2332
  })
@@ -2312,7 +2334,7 @@ var LocationInput = BaseField.extend({
2312
2334
  var FileUploadWithOptions = BaseField.extend({
2313
2335
  type: import_zod17.z.literal(FieldType.FILE_WITH_OPTIONS),
2314
2336
  options: import_zod17.z.array(SelectOption).describe("A list of options"),
2315
- defaultValue: FileFieldWithOptionValue.optional(),
2337
+ defaultValue: FileFieldWithOptionValue.or(ComputedDefaultValue).optional(),
2316
2338
  configuration: import_zod17.z.object({
2317
2339
  maxFileSize: import_zod17.z.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2318
2340
  maxImageSize: import_zod17.z.object({
@@ -2325,11 +2347,11 @@ var FileUploadWithOptions = BaseField.extend({
2325
2347
  });
2326
2348
  var Facility = BaseField.extend({
2327
2349
  type: import_zod17.z.literal(FieldType.FACILITY),
2328
- defaultValue: NonEmptyTextValue.optional()
2350
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
2329
2351
  }).describe("Input field for a facility");
2330
2352
  var Office = BaseField.extend({
2331
2353
  type: import_zod17.z.literal(FieldType.OFFICE),
2332
- defaultValue: NonEmptyTextValue.optional()
2354
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
2333
2355
  }).describe("Input field for an office");
2334
2356
  var DefaultAddressFieldValue = DomesticAddressFieldValue.extend({
2335
2357
  administrativeArea: import_zod17.z.union([UUID, SerializedUserField]).optional()
@@ -2363,7 +2385,7 @@ var Address = BaseField.extend({
2363
2385
  })
2364
2386
  ).optional()
2365
2387
  }).optional(),
2366
- defaultValue: DefaultAddressFieldValue.optional()
2388
+ defaultValue: DefaultAddressFieldValue.or(ComputedDefaultValue).optional()
2367
2389
  }).describe("Address input field \u2013 a combination of location and text fields");
2368
2390
  var StaticDataEntry = import_zod17.z.object({
2369
2391
  id: import_zod17.z.string().describe("ID for the data entry."),
@@ -2382,7 +2404,7 @@ var DataField = BaseField.extend({
2382
2404
  }).describe("Data field for displaying read-only data");
2383
2405
  var ButtonField = BaseField.extend({
2384
2406
  type: import_zod17.z.literal(FieldType.BUTTON),
2385
- defaultValue: ButtonFieldValue.optional(),
2407
+ defaultValue: ButtonFieldValue.or(ComputedDefaultValue).optional(),
2386
2408
  configuration: import_zod17.z.object({
2387
2409
  icon: import_zod17.z.string().optional().describe(
2388
2410
  "Icon for the button. You can find icons from OpenCRVS UI-Kit."
@@ -2407,7 +2429,7 @@ var AlphaPrintButton = BaseField.extend({
2407
2429
  }).describe("Print button field for printing certificates");
2408
2430
  var HttpField = BaseField.extend({
2409
2431
  type: import_zod17.z.literal(FieldType.HTTP),
2410
- defaultValue: HttpFieldValue.optional(),
2432
+ defaultValue: HttpFieldValue.or(ComputedDefaultValue).optional(),
2411
2433
  configuration: import_zod17.z.object({
2412
2434
  trigger: FieldReference,
2413
2435
  url: import_zod17.z.string().describe("URL to send the HTTP request to"),
@@ -2462,7 +2484,7 @@ var LinkButtonField = BaseField.extend({
2462
2484
  }).describe("Button that opens a link");
2463
2485
  var VerificationStatus = BaseField.extend({
2464
2486
  type: import_zod17.z.literal(FieldType.VERIFICATION_STATUS),
2465
- defaultValue: VerificationStatusValue.optional(),
2487
+ defaultValue: VerificationStatusValue.or(ComputedDefaultValue).optional(),
2466
2488
  configuration: import_zod17.z.object({
2467
2489
  status: TranslationConfig.describe("Text to display on the status pill."),
2468
2490
  description: TranslationConfig.describe(
@@ -2480,7 +2502,7 @@ var QueryParamReaderField = BaseField.extend({
2480
2502
  );
2481
2503
  var QrReaderField = BaseField.extend({
2482
2504
  type: import_zod17.z.literal(FieldType.QR_READER),
2483
- defaultValue: QrReaderFieldValue.optional(),
2505
+ defaultValue: QrReaderFieldValue.or(ComputedDefaultValue).optional(),
2484
2506
  configuration: import_zod17.z.object({
2485
2507
  validator: import_zod17.z.custom(
2486
2508
  (val) => typeof val === "object" && val !== null
@@ -2489,7 +2511,7 @@ var QrReaderField = BaseField.extend({
2489
2511
  });
2490
2512
  var IdReaderField = BaseField.extend({
2491
2513
  type: import_zod17.z.literal(FieldType.ID_READER),
2492
- defaultValue: IdReaderFieldValue.optional(),
2514
+ defaultValue: IdReaderFieldValue.or(ComputedDefaultValue).optional(),
2493
2515
  methods: import_zod17.z.array(
2494
2516
  import_zod17.z.union([QrReaderField, LinkButtonField]).describe("Methods for reading an ID")
2495
2517
  )
@@ -3365,7 +3387,37 @@ function resolveDataPath(rootData, dataPath, instancePath) {
3365
3387
  }
3366
3388
  return current;
3367
3389
  }
3390
+ function todayISO() {
3391
+ return (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" });
3392
+ }
3393
+ var compiledFunctionCache = /* @__PURE__ */ new Map();
3394
+ function compileClientFunction(code) {
3395
+ let fn = compiledFunctionCache.get(code);
3396
+ if (!fn) {
3397
+ fn = new Function(`return (${code})`)();
3398
+ compiledFunctionCache.set(code, fn);
3399
+ }
3400
+ return fn;
3401
+ }
3368
3402
  (0, import_ajv_formats.default)(ajv);
3403
+ function buildClientFunctionContext(input) {
3404
+ return {
3405
+ $form: input.form,
3406
+ $now: todayISO(),
3407
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
3408
+ $online: isOnline(),
3409
+ $user: input.validatorContext?.user,
3410
+ $event: input.validatorContext?.event,
3411
+ $leafAdminStructureLocationIds: input.validatorContext?.leafAdminStructureLocationIds ?? [],
3412
+ user: input.systemVariables?.user,
3413
+ $window: input.systemVariables?.$window,
3414
+ locations: input.locations,
3415
+ adminLevelIds: input.adminLevelIds
3416
+ };
3417
+ }
3418
+ function runClientFunction(code, data, context) {
3419
+ return compileClientFunction(code)(data, context);
3420
+ }
3369
3421
  ajv.addKeyword({
3370
3422
  keyword: "daysFromDate",
3371
3423
  type: "string",
@@ -3410,7 +3462,7 @@ ajv.addKeyword({
3410
3462
  $data: true,
3411
3463
  errors: true,
3412
3464
  // @ts-ignore -- Force type. We will move this away from AJV next. Parsing the array will take seconds and is only called by core.
3413
- validate(schema, data, _2, dataContext) {
3465
+ validate(_schema, data, _2, dataContext) {
3414
3466
  const locationIdInput = data;
3415
3467
  const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
3416
3468
  return locations.some((location) => location.id === locationIdInput);
@@ -3419,6 +3471,23 @@ ajv.addKeyword({
3419
3471
  function mergeWithBaseFormState(values, context) {
3420
3472
  return { ...context.baseFormState, ...values };
3421
3473
  }
3474
+ ajv.addKeyword({
3475
+ keyword: "customClientValidator",
3476
+ schemaType: "object",
3477
+ errors: true,
3478
+ // @ts-expect-error -- AJV's public types don't expose `rootData`. All
3479
+ // `validate()` callers build root data via `buildClientFunctionContext`,
3480
+ // so the cast holds.
3481
+ validate(schema, data, _2, dataContext) {
3482
+ return Boolean(
3483
+ runClientFunction(
3484
+ schema.code,
3485
+ data,
3486
+ dataContext?.rootData ?? buildClientFunctionContext({ form: {} })
3487
+ )
3488
+ );
3489
+ }
3490
+ });
3422
3491
  function validate(schema, data) {
3423
3492
  const validator = ajv.getSchema(schema.$id) || ajv.compile(schema);
3424
3493
  if ("$form" in data) {
@@ -3460,14 +3529,13 @@ function isOnline() {
3460
3529
  return true;
3461
3530
  }
3462
3531
  function isConditionMet(conditional, values, context) {
3463
- return validate(conditional, {
3464
- $form: mergeWithBaseFormState(values, context),
3465
- $now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
3466
- $online: isOnline(),
3467
- $user: context.user,
3468
- $leafAdminStructureLocationIds: context.leafAdminStructureLocationIds ?? [],
3469
- $event: context.event
3470
- });
3532
+ return validate(
3533
+ conditional,
3534
+ buildClientFunctionContext({
3535
+ form: mergeWithBaseFormState(values, context),
3536
+ validatorContext: context
3537
+ })
3538
+ );
3471
3539
  }
3472
3540
  function getConditionalActionsForField(field3, values) {
3473
3541
  if (!field3.conditionals) {
@@ -3487,14 +3555,13 @@ function isFieldConditionMet(field3, form, conditionalType, context) {
3487
3555
  if (!hasRule) {
3488
3556
  return true;
3489
3557
  }
3490
- const validConditionals = getConditionalActionsForField(field3, {
3491
- $form: mergeWithBaseFormState(form, context),
3492
- $now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
3493
- $online: isOnline(),
3494
- $user: context.user,
3495
- $leafAdminStructureLocationIds: context.leafAdminStructureLocationIds ?? [],
3496
- $event: context.event
3497
- });
3558
+ const validConditionals = getConditionalActionsForField(
3559
+ field3,
3560
+ buildClientFunctionContext({
3561
+ form: mergeWithBaseFormState(form, context),
3562
+ validatorContext: context
3563
+ })
3564
+ );
3498
3565
  return validConditionals.includes(conditionalType);
3499
3566
  }
3500
3567
  function isFieldVisible(field3, form, context) {
@@ -3640,20 +3707,10 @@ function runFieldValidations({
3640
3707
  if (!isFieldVisible(field3.config, form, context)) {
3641
3708
  return [];
3642
3709
  }
3643
- const conditionalParameters = {
3644
- $form: mergeWithBaseFormState(form, context),
3645
- $now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
3646
- /**
3647
- * In real use cases, there can be hundreds of thousands of locations.
3648
- * Make sure that the context contains only the locations that are needed for validation.
3649
- * E.g. if the user is a leaf admin, only the leaf locations under their admin structure are needed.
3650
- *
3651
- * Loading few megabytes of locations to memory just for validation is not efficient and will choke the application.
3652
- */
3653
- $leafAdminStructureLocationIds: context.leafAdminStructureLocationIds ?? [],
3654
- $online: isOnline(),
3655
- $user: context.user
3656
- };
3710
+ const conditionalParameters = buildClientFunctionContext({
3711
+ form: mergeWithBaseFormState(form, context),
3712
+ validatorContext: context
3713
+ });
3657
3714
  if (isFieldGroupFieldType(field3)) {
3658
3715
  const subfieldErrors = buildFormState(
3659
3716
  field3.config.fields,
@@ -4721,7 +4778,54 @@ function createFieldConditionals(fieldId) {
4721
4778
  }
4722
4779
  },
4723
4780
  required: [fieldId]
4724
- })
4781
+ }),
4782
+ /**
4783
+ * Custom client-side validator. The provided function is serialised and executed
4784
+ * just-in-time on the client only. External references (e.g. lodash) are not
4785
+ * available inside the function body — all logic must be self-contained.
4786
+ *
4787
+ * @example
4788
+ * field('nid').customClientValidator((value) => {
4789
+ * // LUHN check — all logic must be inline
4790
+ * const digits = String(value).split('').map(Number)
4791
+ * // ...
4792
+ * return isValid
4793
+ * })
4794
+ */
4795
+ customClientValidator(validationFn) {
4796
+ const code = validationFn.toString();
4797
+ return defineFormConditional({
4798
+ type: "object",
4799
+ properties: wrapToPath(
4800
+ { [fieldId]: { customClientValidator: { code } } },
4801
+ this.$$subfield
4802
+ ),
4803
+ required: [fieldId]
4804
+ });
4805
+ },
4806
+ /**
4807
+ * Custom client-side evaluation. Returns a {@link FieldReference} descriptor
4808
+ * that can be used as the `value` property or a DATA component entry.
4809
+ * The function receives the referenced field's value as the first argument and
4810
+ * the full form context as the second; its return value replaces the field reference.
4811
+ * The function is serialised and executed just-in-time on the client only.
4812
+ * External references (e.g. lodash) are not available inside the function body.
4813
+ *
4814
+ * For computing a default value without referencing a specific field, use
4815
+ * `evaluate(fn)` in the `defaultValue` property instead.
4816
+ *
4817
+ * @example
4818
+ * field('a').customClientEvaluation((aValue, ctx) =>
4819
+ * Number(aValue) + Number(ctx.$form.b)
4820
+ * )
4821
+ */
4822
+ customClientEvaluation(computationFn) {
4823
+ return {
4824
+ $$code: computationFn.toString(),
4825
+ $$field: fieldId,
4826
+ $$subfield: this.$$subfield
4827
+ };
4828
+ }
4725
4829
  };
4726
4830
  }
4727
4831
 
@@ -5916,6 +6020,9 @@ function createFieldConfig(fieldId, options) {
5916
6020
  }
5917
6021
 
5918
6022
  // ../commons/src/events/field.ts
6023
+ function evaluate(computationFn) {
6024
+ return { $$code: computationFn.toString() };
6025
+ }
5919
6026
  function field(fieldId, options = {}) {
5920
6027
  return {
5921
6028
  ...createFieldConditionals(fieldId),