@opencrvs/toolkit 1.9.13-rc.ec6af6d → 1.9.13-rc.f05e2b4

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
+ CodeToEvaluate: () => CodeToEvaluate,
72
73
  Conditional: () => Conditional,
73
74
  ConditionalType: () => ConditionalType,
74
75
  ConfirmableActions: () => ConfirmableActions,
@@ -229,6 +230,7 @@ __export(events_exports, {
229
230
  areConditionsMet: () => areConditionsMet,
230
231
  buildFormState: () => buildFormState,
231
232
  canUserReadEvent: () => canUserReadEvent,
233
+ compileClientFunction: () => compileClientFunction,
232
234
  compositeFieldTypes: () => compositeFieldTypes,
233
235
  configurableEventScopeAllowed: () => configurableEventScopeAllowed,
234
236
  createEmptyDraft: () => createEmptyDraft,
@@ -313,6 +315,7 @@ __export(events_exports, {
313
315
  isBulletListFieldType: () => isBulletListFieldType,
314
316
  isButtonFieldType: () => isButtonFieldType,
315
317
  isCheckboxFieldType: () => isCheckboxFieldType,
318
+ isCodeToEvaluate: () => isCodeToEvaluate,
316
319
  isConditionMet: () => isConditionMet,
317
320
  isCountryFieldType: () => isCountryFieldType,
318
321
  isDataFieldType: () => isDataFieldType,
@@ -353,6 +356,7 @@ __export(events_exports, {
353
356
  isQrReaderFieldType: () => isQrReaderFieldType,
354
357
  isQueryParamReaderFieldType: () => isQueryParamReaderFieldType,
355
358
  isRadioGroupFieldType: () => isRadioGroupFieldType,
359
+ isResolvableValueReference: () => isResolvableValueReference,
356
360
  isSearchFieldType: () => isSearchFieldType,
357
361
  isSelectDateRangeFieldType: () => isSelectDateRangeFieldType,
358
362
  isSelectFieldType: () => isSelectFieldType,
@@ -383,6 +387,7 @@ __export(events_exports, {
383
387
  runStructuralValidations: () => runStructuralValidations,
384
388
  safeUnion: () => safeUnion,
385
389
  timePeriodToDateRange: () => timePeriodToDateRange,
390
+ todayISO: () => todayISO,
386
391
  user: () => user,
387
392
  validate: () => validate,
388
393
  validateFieldInput: () => validateFieldInput,
@@ -1987,6 +1992,18 @@ var FieldReference = import_zod17.z.object({
1987
1992
  'If the FieldValue is an object, subfield can be used to refer to e.g. `["foo", "bar"]` in `{ foo: { bar: 3 } }`'
1988
1993
  )
1989
1994
  }).describe("Reference to a field by its ID");
1995
+ var CodeToEvaluate = import_zod17.z.object({
1996
+ $$code: import_zod17.z.string().describe("Serialised client-side function body"),
1997
+ $$field: FieldId.describe(
1998
+ "Field whose value is passed as the first argument"
1999
+ ),
2000
+ $$subfield: import_zod17.z.array(import_zod17.z.string()).optional().default([]).describe("Optional nested path within the field value")
2001
+ }).describe(
2002
+ "Serialised client-side evaluation function. Executed just-in-time on the client only."
2003
+ );
2004
+ function isCodeToEvaluate(v) {
2005
+ return !!v && typeof v === "object" && "$$code" in v;
2006
+ }
1990
2007
  var ValidationConfig = import_zod17.z.object({
1991
2008
  validator: Conditional,
1992
2009
  message: TranslationConfig
@@ -2019,8 +2036,11 @@ var BaseField = import_zod17.z.object({
2019
2036
  uncorrectable: import_zod17.z.boolean().default(false).optional().describe(
2020
2037
  "Indicates whether the field can be modified during record correction."
2021
2038
  ),
2022
- 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."
2039
+ value: FieldReference.or(CodeToEvaluate).or(import_zod17.z.array(FieldReference.or(CodeToEvaluate))).optional().describe(
2040
+ "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 CodeToEvaluate can be used to compute the value via a custom client-side function."
2041
+ ),
2042
+ evaluatedDefaultValue: CodeToEvaluate.optional().describe(
2043
+ "Custom client-side function used to compute the default value. Executed just-in-time on the client. Takes precedence over defaultValue when set."
2024
2044
  ),
2025
2045
  analytics: import_zod17.z.boolean().default(false).optional().describe(
2026
2046
  "Indicates whether the field is included in analytics. When enabled, its value becomes available in the analytics dashboard."
@@ -2371,7 +2391,7 @@ var Address = BaseField.extend({
2371
2391
  var StaticDataEntry = import_zod17.z.object({
2372
2392
  id: import_zod17.z.string().describe("ID for the data entry."),
2373
2393
  label: TranslationConfig,
2374
- value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference)
2394
+ value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference).or(CodeToEvaluate)
2375
2395
  }).describe("Static data entry");
2376
2396
  var DataEntry = import_zod17.z.union([StaticDataEntry, import_zod17.z.object({ fieldId: import_zod17.z.string() })]).describe(
2377
2397
  "Data entry can be either a static data entry, or a reference to another field in the current form or the declaration."
@@ -2418,7 +2438,7 @@ var HttpField = BaseField.extend({
2418
2438
  headers: import_zod17.z.record(import_zod17.z.string()).optional(),
2419
2439
  body: import_zod17.z.record(import_zod17.z.any()).optional(),
2420
2440
  errorValue: import_zod17.z.any().optional().describe("Value to set if the request fails"),
2421
- params: import_zod17.z.record(import_zod17.z.string(), import_zod17.z.union([import_zod17.z.string(), FieldReference])).optional(),
2441
+ params: import_zod17.z.record(import_zod17.z.string(), import_zod17.z.union([import_zod17.z.string(), FieldReference, CodeToEvaluate])).optional(),
2422
2442
  timeout: import_zod17.z.number().default(15e3).describe("Request timeout in milliseconds")
2423
2443
  })
2424
2444
  }).describe("HTTP request function triggered by a button click or other event");
@@ -3368,6 +3388,18 @@ function resolveDataPath(rootData, dataPath, instancePath) {
3368
3388
  }
3369
3389
  return current;
3370
3390
  }
3391
+ function todayISO() {
3392
+ return (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" });
3393
+ }
3394
+ var compiledFunctionCache = /* @__PURE__ */ new Map();
3395
+ function compileClientFunction(code) {
3396
+ let fn = compiledFunctionCache.get(code);
3397
+ if (!fn) {
3398
+ fn = new Function(`return (${code})`)();
3399
+ compiledFunctionCache.set(code, fn);
3400
+ }
3401
+ return fn;
3402
+ }
3371
3403
  (0, import_ajv_formats.default)(ajv);
3372
3404
  ajv.addKeyword({
3373
3405
  keyword: "daysFromDate",
@@ -3413,12 +3445,21 @@ ajv.addKeyword({
3413
3445
  $data: true,
3414
3446
  errors: true,
3415
3447
  // @ts-ignore -- Force type. We will move this away from AJV next. Parsing the array will take seconds and is only called by core.
3416
- validate(schema, data, _2, dataContext) {
3448
+ validate(_schema, data, _2, dataContext) {
3417
3449
  const locationIdInput = data;
3418
3450
  const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
3419
3451
  return locations.some((location) => location.id === locationIdInput);
3420
3452
  }
3421
3453
  });
3454
+ ajv.addKeyword({
3455
+ keyword: "customClientValidator",
3456
+ schemaType: "object",
3457
+ errors: true,
3458
+ // @ts-ignore -- DataContext is not typed in AJV's public API
3459
+ validate(schema, data, _2, dataContext) {
3460
+ return compileClientFunction(schema.code)(data, dataContext?.rootData);
3461
+ }
3462
+ });
3422
3463
  function validate(schema, data) {
3423
3464
  const validator = ajv.getSchema(schema.$id) || ajv.compile(schema);
3424
3465
  if ("$form" in data) {
@@ -4292,6 +4333,9 @@ function isFieldReference(value) {
4292
4333
  function isEventFieldReference(value) {
4293
4334
  return typeof value === "object" && value !== null && "$$event" in value;
4294
4335
  }
4336
+ function isResolvableValueReference(value) {
4337
+ return isFieldReference(value) || isCodeToEvaluate(value);
4338
+ }
4295
4339
  function getDateRangeToFieldReference(field3, comparedField, clause) {
4296
4340
  return {
4297
4341
  type: "object",
@@ -4726,7 +4770,49 @@ function createFieldConditionals(fieldId) {
4726
4770
  }
4727
4771
  },
4728
4772
  required: [fieldId]
4729
- })
4773
+ }),
4774
+ /**
4775
+ * Custom client-side validator. The provided function is serialised and executed
4776
+ * just-in-time on the client only. External references (e.g. lodash) are not
4777
+ * available inside the function body — all logic must be self-contained.
4778
+ *
4779
+ * @example
4780
+ * field('nid').customClientValidator((value) => {
4781
+ * // LUHN check — all logic must be inline
4782
+ * const digits = String(value).split('').map(Number)
4783
+ * // ...
4784
+ * return isValid
4785
+ * })
4786
+ */
4787
+ customClientValidator(validationFn) {
4788
+ const code = validationFn.toString();
4789
+ return defineFormConditional({
4790
+ type: "object",
4791
+ properties: wrapToPath(
4792
+ { [fieldId]: { customClientValidator: { code } } },
4793
+ this.$$subfield
4794
+ ),
4795
+ required: [fieldId]
4796
+ });
4797
+ },
4798
+ /**
4799
+ * Custom client-side evaluation. Returns a {@link CodeToEvaluate} descriptor
4800
+ * that can be used as `value`, `evaluatedDefaultValue`, or a DATA component entry.
4801
+ * The function is serialised and executed just-in-time on the client only.
4802
+ * External references (e.g. lodash) are not available inside the function body.
4803
+ *
4804
+ * @example
4805
+ * field('a').customClientEvaluation((aValue, ctx) =>
4806
+ * Number(aValue) + Number(ctx.$form.b)
4807
+ * )
4808
+ */
4809
+ customClientEvaluation(computationFn) {
4810
+ return {
4811
+ $$code: computationFn.toString(),
4812
+ $$field: fieldId,
4813
+ $$subfield: this.$$subfield
4814
+ };
4815
+ }
4730
4816
  };
4731
4817
  }
4732
4818
 
@@ -1493,6 +1493,15 @@ var FieldReference = import_zod17.z.object({
1493
1493
  'If the FieldValue is an object, subfield can be used to refer to e.g. `["foo", "bar"]` in `{ foo: { bar: 3 } }`'
1494
1494
  )
1495
1495
  }).describe("Reference to a field by its ID");
1496
+ var CodeToEvaluate = import_zod17.z.object({
1497
+ $$code: import_zod17.z.string().describe("Serialised client-side function body"),
1498
+ $$field: FieldId.describe(
1499
+ "Field whose value is passed as the first argument"
1500
+ ),
1501
+ $$subfield: import_zod17.z.array(import_zod17.z.string()).optional().default([]).describe("Optional nested path within the field value")
1502
+ }).describe(
1503
+ "Serialised client-side evaluation function. Executed just-in-time on the client only."
1504
+ );
1496
1505
  var ValidationConfig = import_zod17.z.object({
1497
1506
  validator: Conditional,
1498
1507
  message: TranslationConfig
@@ -1525,8 +1534,11 @@ var BaseField = import_zod17.z.object({
1525
1534
  uncorrectable: import_zod17.z.boolean().default(false).optional().describe(
1526
1535
  "Indicates whether the field can be modified during record correction."
1527
1536
  ),
1528
- value: FieldReference.or(import_zod17.z.array(FieldReference)).optional().describe(
1529
- "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."
1537
+ value: FieldReference.or(CodeToEvaluate).or(import_zod17.z.array(FieldReference.or(CodeToEvaluate))).optional().describe(
1538
+ "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 CodeToEvaluate can be used to compute the value via a custom client-side function."
1539
+ ),
1540
+ evaluatedDefaultValue: CodeToEvaluate.optional().describe(
1541
+ "Custom client-side function used to compute the default value. Executed just-in-time on the client. Takes precedence over defaultValue when set."
1530
1542
  ),
1531
1543
  analytics: import_zod17.z.boolean().default(false).optional().describe(
1532
1544
  "Indicates whether the field is included in analytics. When enabled, its value becomes available in the analytics dashboard."
@@ -1877,7 +1889,7 @@ var Address = BaseField.extend({
1877
1889
  var StaticDataEntry = import_zod17.z.object({
1878
1890
  id: import_zod17.z.string().describe("ID for the data entry."),
1879
1891
  label: TranslationConfig,
1880
- value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference)
1892
+ value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference).or(CodeToEvaluate)
1881
1893
  }).describe("Static data entry");
1882
1894
  var DataEntry = import_zod17.z.union([StaticDataEntry, import_zod17.z.object({ fieldId: import_zod17.z.string() })]).describe(
1883
1895
  "Data entry can be either a static data entry, or a reference to another field in the current form or the declaration."
@@ -1924,7 +1936,7 @@ var HttpField = BaseField.extend({
1924
1936
  headers: import_zod17.z.record(import_zod17.z.string()).optional(),
1925
1937
  body: import_zod17.z.record(import_zod17.z.any()).optional(),
1926
1938
  errorValue: import_zod17.z.any().optional().describe("Value to set if the request fails"),
1927
- params: import_zod17.z.record(import_zod17.z.string(), import_zod17.z.union([import_zod17.z.string(), FieldReference])).optional(),
1939
+ params: import_zod17.z.record(import_zod17.z.string(), import_zod17.z.union([import_zod17.z.string(), FieldReference, CodeToEvaluate])).optional(),
1928
1940
  timeout: import_zod17.z.number().default(15e3).describe("Request timeout in milliseconds")
1929
1941
  })
1930
1942
  }).describe("HTTP request function triggered by a button click or other event");
@@ -2559,6 +2571,15 @@ function resolveDataPath(rootData, dataPath, instancePath) {
2559
2571
  }
2560
2572
  return current;
2561
2573
  }
2574
+ var compiledFunctionCache = /* @__PURE__ */ new Map();
2575
+ function compileClientFunction(code) {
2576
+ let fn = compiledFunctionCache.get(code);
2577
+ if (!fn) {
2578
+ fn = new Function(`return (${code})`)();
2579
+ compiledFunctionCache.set(code, fn);
2580
+ }
2581
+ return fn;
2582
+ }
2562
2583
  (0, import_ajv_formats.default)(ajv);
2563
2584
  ajv.addKeyword({
2564
2585
  keyword: "daysFromDate",
@@ -2604,12 +2625,21 @@ ajv.addKeyword({
2604
2625
  $data: true,
2605
2626
  errors: true,
2606
2627
  // @ts-ignore -- Force type. We will move this away from AJV next. Parsing the array will take seconds and is only called by core.
2607
- validate(schema, data, _2, dataContext) {
2628
+ validate(_schema, data, _2, dataContext) {
2608
2629
  const locationIdInput = data;
2609
2630
  const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
2610
2631
  return locations.some((location) => location.id === locationIdInput);
2611
2632
  }
2612
2633
  });
2634
+ ajv.addKeyword({
2635
+ keyword: "customClientValidator",
2636
+ schemaType: "object",
2637
+ errors: true,
2638
+ // @ts-ignore -- DataContext is not typed in AJV's public API
2639
+ validate(schema, data, _2, dataContext) {
2640
+ return compileClientFunction(schema.code)(data, dataContext?.rootData);
2641
+ }
2642
+ });
2613
2643
 
2614
2644
  // ../commons/src/utils.ts
2615
2645
  var z29 = __toESM(require("zod"));
@@ -3232,7 +3262,49 @@ function createFieldConditionals(fieldId) {
3232
3262
  }
3233
3263
  },
3234
3264
  required: [fieldId]
3235
- })
3265
+ }),
3266
+ /**
3267
+ * Custom client-side validator. The provided function is serialised and executed
3268
+ * just-in-time on the client only. External references (e.g. lodash) are not
3269
+ * available inside the function body — all logic must be self-contained.
3270
+ *
3271
+ * @example
3272
+ * field('nid').customClientValidator((value) => {
3273
+ * // LUHN check — all logic must be inline
3274
+ * const digits = String(value).split('').map(Number)
3275
+ * // ...
3276
+ * return isValid
3277
+ * })
3278
+ */
3279
+ customClientValidator(validationFn) {
3280
+ const code = validationFn.toString();
3281
+ return defineFormConditional({
3282
+ type: "object",
3283
+ properties: wrapToPath(
3284
+ { [fieldId]: { customClientValidator: { code } } },
3285
+ this.$$subfield
3286
+ ),
3287
+ required: [fieldId]
3288
+ });
3289
+ },
3290
+ /**
3291
+ * Custom client-side evaluation. Returns a {@link CodeToEvaluate} descriptor
3292
+ * that can be used as `value`, `evaluatedDefaultValue`, or a DATA component entry.
3293
+ * The function is serialised and executed just-in-time on the client only.
3294
+ * External references (e.g. lodash) are not available inside the function body.
3295
+ *
3296
+ * @example
3297
+ * field('a').customClientEvaluation((aValue, ctx) =>
3298
+ * Number(aValue) + Number(ctx.$form.b)
3299
+ * )
3300
+ */
3301
+ customClientEvaluation(computationFn) {
3302
+ return {
3303
+ $$code: computationFn.toString(),
3304
+ $$field: fieldId,
3305
+ $$subfield: this.$$subfield
3306
+ };
3307
+ }
3236
3308
  };
3237
3309
  }
3238
3310
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencrvs/toolkit",
3
- "version": "1.9.13-rc.ec6af6d",
3
+ "version": "1.9.13-rc.f05e2b4",
4
4
  "description": "OpenCRVS toolkit for building country configurations",
5
5
  "license": "MPL-2.0",
6
6
  "exports": {