@opencrvs/toolkit 1.9.13-rc.0e16587 → 1.9.13-rc.0f86459

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.
@@ -240,6 +240,7 @@ export declare const defineConfig: (config: EventConfigInput) => {
240
240
  dateOfEvent?: {
241
241
  $$field: string;
242
242
  $$subfield: string[];
243
+ $$code?: string | undefined;
243
244
  } | {
244
245
  $$event: "createdAt" | "updatedAt" | "legalStatuses.DECLARED.createdAt" | "legalStatuses.DECLARED.acceptedAt" | "legalStatuses.REGISTERED.createdAt" | "legalStatuses.REGISTERED.acceptedAt";
245
246
  } | undefined;
@@ -1,6 +1,30 @@
1
+ import { FormConditionalParameters } from '../conditionals/conditionals';
1
2
  import { FieldConditional } from './Conditional';
2
3
  import { TranslationConfig } from './TranslationConfig';
3
- import { SelectOption, ValidationConfig } from './FieldConfig';
4
+ import { ComputedDefaultValue, SelectOption, ValidationConfig } from './FieldConfig';
5
+ /**
6
+ * Creates a {@link ComputedDefaultValue} descriptor for use in a field's `defaultValue`.
7
+ *
8
+ * Unlike `field(id).customClientEvaluation(fn)`, this builder is **not tied to any
9
+ * specific field**. Use it when the initial value should be derived purely from
10
+ * context variables (`$now`, `$online`, system variables such as `$user`) rather
11
+ * than from another field's current value.
12
+ *
13
+ * The serialised function receives `(undefined, context)` at runtime, where
14
+ * `context` is a {@link FormConditionalParameters} object.
15
+ *
16
+ * External references (e.g. lodash) are **not** available inside the function body —
17
+ * all logic must be self-contained so the code survives serialisation.
18
+ *
19
+ * @example
20
+ * // Default a date field to today
21
+ * defaultValue: evaluate((_, ctx) => ctx.$now)
22
+ *
23
+ * @example
24
+ * // Default to the current user's role
25
+ * defaultValue: evaluate((_, ctx) => ctx.user.role)
26
+ */
27
+ export declare function evaluate(computationFn: (fieldValue: undefined, context: FormConditionalParameters) => unknown): ComputedDefaultValue;
4
28
  /**
5
29
  * Entry point for defining conditional logic or configuration for a form field.
6
30
  * @param fieldId - The ID of the field to define rules or config for.
@@ -107,6 +131,8 @@ export declare function field(fieldId: string, options?: {
107
131
  fieldId: string;
108
132
  };
109
133
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
134
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
135
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
110
136
  };
111
137
  asDob(): any;
112
138
  asAge(): any;
@@ -144,6 +170,8 @@ export declare function field(fieldId: string, options?: {
144
170
  fieldId: string;
145
171
  };
146
172
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
173
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
174
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
147
175
  };
148
176
  getByPath(fieldPath: string[]): {
149
177
  $$subfield: string[];
@@ -189,6 +217,8 @@ export declare function field(fieldId: string, options?: {
189
217
  fieldId: string;
190
218
  };
191
219
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
220
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
221
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
192
222
  };
193
223
  getByPath(fieldPath: string[]): any;
194
224
  asDob(): {
@@ -232,6 +262,8 @@ export declare function field(fieldId: string, options?: {
232
262
  fieldId: string;
233
263
  };
234
264
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
265
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
266
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
235
267
  };
236
268
  asAge(): {
237
269
  $$subfield: string[];
@@ -274,6 +306,8 @@ export declare function field(fieldId: string, options?: {
274
306
  fieldId: string;
275
307
  };
276
308
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
309
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
310
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
277
311
  };
278
312
  isAfter(): {
279
313
  days: (days: number) => {
@@ -309,6 +343,8 @@ export declare function field(fieldId: string, options?: {
309
343
  fieldId: string;
310
344
  };
311
345
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
346
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
347
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
312
348
  };
313
349
  asDob(): {
314
350
  $$subfield: string[];
@@ -355,6 +391,8 @@ export declare function field(fieldId: string, options?: {
355
391
  fieldId: string;
356
392
  };
357
393
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
394
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
395
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
358
396
  };
359
397
  asDob(): any;
360
398
  asAge(): any;
@@ -392,6 +430,8 @@ export declare function field(fieldId: string, options?: {
392
430
  fieldId: string;
393
431
  };
394
432
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
433
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
434
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
395
435
  };
396
436
  asAge(): {
397
437
  $$subfield: string[];
@@ -438,6 +478,8 @@ export declare function field(fieldId: string, options?: {
438
478
  fieldId: string;
439
479
  };
440
480
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
481
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
482
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
441
483
  };
442
484
  asDob(): any;
443
485
  asAge(): any;
@@ -475,6 +517,8 @@ export declare function field(fieldId: string, options?: {
475
517
  fieldId: string;
476
518
  };
477
519
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
520
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
521
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
478
522
  };
479
523
  isAfter(): {
480
524
  days: (days: number) => {
@@ -510,5 +554,7 @@ export declare function field(fieldId: string, options?: {
510
554
  fieldId: string;
511
555
  };
512
556
  object: (options: Record<string, any>) => import("../conditionals/conditionals").JSONSchema;
557
+ customClientValidator(validationFn: (fieldValue: unknown, context: FormConditionalParameters) => boolean): import("../conditionals/conditionals").JSONSchema;
558
+ customClientEvaluation(computationFn: (fieldValue: unknown, context: FormConditionalParameters) => unknown): import("./FieldConfig").CodeToEvaluate;
513
559
  };
514
560
  //# sourceMappingURL=field.d.ts.map
@@ -449,6 +449,7 @@ export declare function getEventConfigById(eventConfigs: EventConfig[], id: stri
449
449
  dateOfEvent?: {
450
450
  $$field: string;
451
451
  $$subfield: string[];
452
+ $$code?: string | undefined;
452
453
  } | {
453
454
  $$event: "createdAt" | "updatedAt" | "legalStatuses.DECLARED.createdAt" | "legalStatuses.DECLARED.acceptedAt" | "legalStatuses.REGISTERED.createdAt" | "legalStatuses.REGISTERED.acceptedAt";
454
455
  } | undefined;
@@ -684,6 +684,53 @@ function createFieldConditionals(fieldId) {
684
684
  }
685
685
  },
686
686
  required: [fieldId]
687
- })
687
+ }),
688
+ /**
689
+ * Custom client-side validator. The provided function is serialised and executed
690
+ * just-in-time on the client only. External references (e.g. lodash) are not
691
+ * available inside the function body — all logic must be self-contained.
692
+ *
693
+ * @example
694
+ * field('nid').customClientValidator((value) => {
695
+ * // LUHN check — all logic must be inline
696
+ * const digits = String(value).split('').map(Number)
697
+ * // ...
698
+ * return isValid
699
+ * })
700
+ */
701
+ customClientValidator(validationFn) {
702
+ const code = validationFn.toString();
703
+ return defineFormConditional({
704
+ type: "object",
705
+ properties: wrapToPath(
706
+ { [fieldId]: { customClientValidator: { code } } },
707
+ this.$$subfield
708
+ ),
709
+ required: [fieldId]
710
+ });
711
+ },
712
+ /**
713
+ * Custom client-side evaluation. Returns a {@link FieldReference} descriptor
714
+ * that can be used as the `value` property or a DATA component entry.
715
+ * The function receives the referenced field's value as the first argument and
716
+ * the full form context as the second; its return value replaces the field reference.
717
+ * The function is serialised and executed just-in-time on the client only.
718
+ * External references (e.g. lodash) are not available inside the function body.
719
+ *
720
+ * For computing a default value without referencing a specific field, use
721
+ * `evaluate(fn)` in the `defaultValue` property instead.
722
+ *
723
+ * @example
724
+ * field('a').customClientEvaluation((aValue, ctx) =>
725
+ * Number(aValue) + Number(ctx.$form.b)
726
+ * )
727
+ */
728
+ customClientEvaluation(computationFn) {
729
+ return {
730
+ $$code: computationFn.toString(),
731
+ $$field: fieldId,
732
+ $$subfield: this.$$subfield
733
+ };
734
+ }
688
735
  };
689
736
  }
@@ -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,
@@ -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,
@@ -250,6 +252,7 @@ __export(events_exports, {
250
252
  defineWorkqueuesColumns: () => defineWorkqueuesColumns,
251
253
  deserializeQuery: () => deserializeQuery,
252
254
  errorMessages: () => errorMessages,
255
+ evaluate: () => evaluate,
253
256
  event: () => event,
254
257
  eventMetadataLabelMap: () => eventMetadataLabelMap,
255
258
  eventPayloadGenerator: () => eventPayloadGenerator,
@@ -313,6 +316,7 @@ __export(events_exports, {
313
316
  isBulletListFieldType: () => isBulletListFieldType,
314
317
  isButtonFieldType: () => isButtonFieldType,
315
318
  isCheckboxFieldType: () => isCheckboxFieldType,
319
+ isCodeToEvaluate: () => isCodeToEvaluate,
316
320
  isConditionMet: () => isConditionMet,
317
321
  isCountryFieldType: () => isCountryFieldType,
318
322
  isDataFieldType: () => isDataFieldType,
@@ -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,
@@ -1982,11 +1987,26 @@ var FieldId = import_zod17.z.string().refine(
1982
1987
  })
1983
1988
  ).describe("Unique identifier for the field");
1984
1989
  var FieldReference = import_zod17.z.object({
1985
- $$field: FieldId,
1990
+ $$field: FieldId.describe(
1991
+ "Field whose value is passed as the first argument to the computation"
1992
+ ),
1986
1993
  $$subfield: import_zod17.z.array(import_zod17.z.string()).optional().default([]).describe(
1987
1994
  'If the FieldValue is an object, subfield can be used to refer to e.g. `["foo", "bar"]` in `{ foo: { bar: 3 } }`'
1995
+ ),
1996
+ $$code: import_zod17.z.string().optional().describe(
1997
+ "Serialised client-side function body. When present the expression is evaluated rather than dereferenced."
1998
+ )
1999
+ }).describe(
2000
+ "Reference to a field value, with an optional client-side computation applied."
2001
+ );
2002
+ function isCodeToEvaluate(v) {
2003
+ return !!v && typeof v === "object" && "$$code" in v;
2004
+ }
2005
+ var ComputedDefaultValue = import_zod17.z.object({
2006
+ $$code: import_zod17.z.string().describe(
2007
+ "Serialised client-side function body. Receives (undefined, context) where context includes $now, $online, and system variables."
1988
2008
  )
1989
- }).describe("Reference to a field by its ID");
2009
+ }).describe("A context-only computation used as a field default value.");
1990
2010
  var ValidationConfig = import_zod17.z.object({
1991
2011
  validator: Conditional,
1992
2012
  message: TranslationConfig
@@ -2020,7 +2040,7 @@ var BaseField = import_zod17.z.object({
2020
2040
  "Indicates whether the field can be modified during record correction."
2021
2041
  ),
2022
2042
  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."
2043
+ "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
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."
@@ -2031,7 +2051,7 @@ var Divider = BaseField.extend({
2031
2051
  });
2032
2052
  var TextField = BaseField.extend({
2033
2053
  type: import_zod17.z.literal(FieldType.TEXT),
2034
- defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField]).optional(),
2054
+ defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
2035
2055
  configuration: import_zod17.z.object({
2036
2056
  maxLength: import_zod17.z.number().optional().describe("Maximum length of the text"),
2037
2057
  type: import_zod17.z.enum(["text", "password"]).optional(),
@@ -2041,7 +2061,7 @@ var TextField = BaseField.extend({
2041
2061
  }).describe("Text input");
2042
2062
  var NumberField = BaseField.extend({
2043
2063
  type: import_zod17.z.literal(FieldType.NUMBER),
2044
- defaultValue: NumberFieldValue.optional(),
2064
+ defaultValue: NumberFieldValue.or(ComputedDefaultValue).optional(),
2045
2065
  configuration: import_zod17.z.object({
2046
2066
  min: import_zod17.z.number().optional().describe("Minimum value"),
2047
2067
  max: import_zod17.z.number().optional().describe("Maximum value"),
@@ -2051,7 +2071,7 @@ var NumberField = BaseField.extend({
2051
2071
  }).describe("Number input");
2052
2072
  var TextAreaField = BaseField.extend({
2053
2073
  type: import_zod17.z.literal(FieldType.TEXTAREA),
2054
- defaultValue: NonEmptyTextValue.optional(),
2074
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2055
2075
  configuration: import_zod17.z.object({
2056
2076
  maxLength: import_zod17.z.number().optional().describe("Maximum length of the text"),
2057
2077
  rows: import_zod17.z.number().optional().describe("Number of visible text lines"),
@@ -2082,7 +2102,7 @@ var SignatureField = BaseField.extend({
2082
2102
  signaturePromptLabel: TranslationConfig.describe(
2083
2103
  "Title of the signature modal"
2084
2104
  ),
2085
- defaultValue: SignatureFieldValue.optional(),
2105
+ defaultValue: SignatureFieldValue.or(ComputedDefaultValue).optional(),
2086
2106
  configuration: import_zod17.z.object({
2087
2107
  maxFileSize: import_zod17.z.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2088
2108
  acceptedFileTypes: MimeType.array().optional().describe("List of allowed file formats for the signature")
@@ -2095,11 +2115,11 @@ var EmailField = BaseField.extend({
2095
2115
  configuration: import_zod17.z.object({
2096
2116
  maxLength: import_zod17.z.number().optional().describe("Maximum length of the text")
2097
2117
  }).default({ maxLength: 10 }).optional(),
2098
- defaultValue: NonEmptyTextValue.optional()
2118
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
2099
2119
  });
2100
2120
  var DateField = BaseField.extend({
2101
2121
  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)"),
2122
+ defaultValue: SerializedNowDateTime.or(PlainDate).or(ComputedDefaultValue).optional().openapi({ effectType: "input", type: "string" }).describe("Default date value(yyyy-MM-dd)"),
2103
2123
  configuration: import_zod17.z.object({
2104
2124
  notice: TranslationConfig.describe(
2105
2125
  "Text to display above the date input"
@@ -2108,7 +2128,7 @@ var DateField = BaseField.extend({
2108
2128
  }).describe("A single date input (yyyy-MM-dd)");
2109
2129
  var AgeField = BaseField.extend({
2110
2130
  type: import_zod17.z.literal(FieldType.AGE),
2111
- defaultValue: NumberFieldValue.optional(),
2131
+ defaultValue: NumberFieldValue.or(ComputedDefaultValue).optional(),
2112
2132
  configuration: import_zod17.z.object({
2113
2133
  asOfDate: FieldReference,
2114
2134
  prefix: TranslationConfig.optional(),
@@ -2117,7 +2137,7 @@ var AgeField = BaseField.extend({
2117
2137
  }).describe("An age input field which uses the current date as the asOfDate");
2118
2138
  var TimeField = BaseField.extend({
2119
2139
  type: import_zod17.z.literal(FieldType.TIME),
2120
- defaultValue: SerializedNowDateTime.or(TimeValue).optional().openapi({ effectType: "input", type: "object" }).describe("Default time value (HH-mm)"),
2140
+ defaultValue: SerializedNowDateTime.or(TimeValue).or(ComputedDefaultValue).optional().openapi({ effectType: "input", type: "object" }).describe("Default time value (HH-mm)"),
2121
2141
  configuration: import_zod17.z.object({
2122
2142
  use12HourFormat: import_zod17.z.boolean().optional().describe("Whether to use 12-hour format"),
2123
2143
  notice: TranslationConfig.describe(
@@ -2127,7 +2147,7 @@ var TimeField = BaseField.extend({
2127
2147
  }).describe("A single date input (HH-mm)");
2128
2148
  var DateRangeField = BaseField.extend({
2129
2149
  type: import_zod17.z.literal(FieldType.DATE_RANGE),
2130
- defaultValue: DateRangeFieldValue.optional(),
2150
+ defaultValue: DateRangeFieldValue.or(ComputedDefaultValue).optional(),
2131
2151
  configuration: import_zod17.z.object({
2132
2152
  notice: TranslationConfig.describe(
2133
2153
  "Text to display above the date input"
@@ -2161,7 +2181,7 @@ var PageHeader = BaseField.extend({
2161
2181
  }).describe("A read-only header component for form pages");
2162
2182
  var File = BaseField.extend({
2163
2183
  type: import_zod17.z.literal(FieldType.FILE),
2164
- defaultValue: FileFieldValue.optional(),
2184
+ defaultValue: FileFieldValue.or(ComputedDefaultValue).optional(),
2165
2185
  configuration: import_zod17.z.object({
2166
2186
  maxFileSize: import_zod17.z.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2167
2187
  acceptedFileTypes: MimeType.array().optional().describe("List of allowed file formats for the signature"),
@@ -2185,7 +2205,7 @@ var SelectOption = import_zod17.z.object({
2185
2205
  });
2186
2206
  var NumberWithUnitField = BaseField.extend({
2187
2207
  type: import_zod17.z.literal(FieldType.NUMBER_WITH_UNIT),
2188
- defaultValue: NumberWithUnitFieldValue.optional(),
2208
+ defaultValue: NumberWithUnitFieldValue.or(ComputedDefaultValue).optional(),
2189
2209
  options: import_zod17.z.array(SelectOption).describe("A list of options for the unit select"),
2190
2210
  configuration: import_zod17.z.object({
2191
2211
  min: import_zod17.z.number().optional().describe("Minimum value of the number field"),
@@ -2197,7 +2217,7 @@ var NumberWithUnitField = BaseField.extend({
2197
2217
  }).describe("Number with unit input");
2198
2218
  var RadioGroup = BaseField.extend({
2199
2219
  type: import_zod17.z.literal(FieldType.RADIO_GROUP),
2200
- defaultValue: TextValue.optional(),
2220
+ defaultValue: TextValue.or(ComputedDefaultValue).optional(),
2201
2221
  options: import_zod17.z.array(SelectOption).describe("A list of options"),
2202
2222
  configuration: import_zod17.z.object({
2203
2223
  styles: import_zod17.z.object({
@@ -2216,7 +2236,7 @@ var BulletList = BaseField.extend({
2216
2236
  }).describe("A list of bullet points");
2217
2237
  var Select = BaseField.extend({
2218
2238
  type: import_zod17.z.literal(FieldType.SELECT),
2219
- defaultValue: TextValue.optional(),
2239
+ defaultValue: TextValue.or(ComputedDefaultValue).optional(),
2220
2240
  options: import_zod17.z.array(SelectOption).describe("A list of options"),
2221
2241
  noOptionsMessage: TranslationConfig.optional().describe(
2222
2242
  `
@@ -2235,7 +2255,7 @@ var SelectDateRangeOption = import_zod17.z.object({
2235
2255
  });
2236
2256
  var SelectDateRangeField = BaseField.extend({
2237
2257
  type: import_zod17.z.literal(FieldType.SELECT_DATE_RANGE),
2238
- defaultValue: SelectDateRangeValue.optional(),
2258
+ defaultValue: SelectDateRangeValue.or(ComputedDefaultValue).optional(),
2239
2259
  options: import_zod17.z.array(SelectDateRangeOption).describe("A list of options")
2240
2260
  }).describe("Select input with date range options");
2241
2261
  var NameConfig = import_zod17.z.object({
@@ -2249,7 +2269,7 @@ var NameField = BaseField.extend({
2249
2269
  firstname: SerializedUserField.or(NonEmptyTextValue).optional(),
2250
2270
  middlename: SerializedUserField.or(NonEmptyTextValue).optional(),
2251
2271
  surname: SerializedUserField.or(NonEmptyTextValue).optional()
2252
- }).optional(),
2272
+ }).or(ComputedDefaultValue).optional(),
2253
2273
  configuration: import_zod17.z.object({
2254
2274
  name: NameConfig.default({
2255
2275
  firstname: { required: true },
@@ -2270,20 +2290,20 @@ var NameField = BaseField.extend({
2270
2290
  }).optional()
2271
2291
  }).describe("Name input field");
2272
2292
  var PhoneField = BaseField.extend({
2273
- defaultValue: NonEmptyTextValue.optional(),
2293
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2274
2294
  type: import_zod17.z.literal(FieldType.PHONE)
2275
2295
  }).describe("Phone input field");
2276
2296
  var IdField = BaseField.extend({
2277
- defaultValue: NonEmptyTextValue.optional(),
2297
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2278
2298
  type: import_zod17.z.literal(FieldType.ID)
2279
2299
  }).describe("ID input field");
2280
2300
  var Checkbox = BaseField.extend({
2281
2301
  type: import_zod17.z.literal(FieldType.CHECKBOX),
2282
- defaultValue: CheckboxFieldValue.default(false)
2302
+ defaultValue: CheckboxFieldValue.or(ComputedDefaultValue).default(false)
2283
2303
  }).describe("Boolean checkbox field");
2284
2304
  var Country = BaseField.extend({
2285
2305
  type: import_zod17.z.literal(FieldType.COUNTRY),
2286
- defaultValue: NonEmptyTextValue.optional(),
2306
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2287
2307
  optionOverrides: import_zod17.z.array(SelectOption.omit({ label: true })).optional().describe(
2288
2308
  "Conditionals for specific countries. Countries not listed are always shown and enabled."
2289
2309
  )
@@ -2299,12 +2319,12 @@ var AdministrativeAreaConfiguration = import_zod17.z.object({
2299
2319
  }).describe("Administrative area options");
2300
2320
  var AdministrativeArea = BaseField.extend({
2301
2321
  type: import_zod17.z.literal(FieldType.ADMINISTRATIVE_AREA),
2302
- defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField]).optional(),
2322
+ defaultValue: import_zod17.z.union([NonEmptyTextValue, SerializedUserField, ComputedDefaultValue]).optional(),
2303
2323
  configuration: AdministrativeAreaConfiguration
2304
2324
  }).describe("Administrative area input field e.g. facility, office");
2305
2325
  var LocationInput = BaseField.extend({
2306
2326
  type: import_zod17.z.literal(FieldType.LOCATION),
2307
- defaultValue: NonEmptyTextValue.optional(),
2327
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional(),
2308
2328
  configuration: import_zod17.z.object({
2309
2329
  searchableResource: import_zod17.z.array(import_zod17.z.enum(["locations", "facilities", "offices"]))
2310
2330
  })
@@ -2312,7 +2332,7 @@ var LocationInput = BaseField.extend({
2312
2332
  var FileUploadWithOptions = BaseField.extend({
2313
2333
  type: import_zod17.z.literal(FieldType.FILE_WITH_OPTIONS),
2314
2334
  options: import_zod17.z.array(SelectOption).describe("A list of options"),
2315
- defaultValue: FileFieldWithOptionValue.optional(),
2335
+ defaultValue: FileFieldWithOptionValue.or(ComputedDefaultValue).optional(),
2316
2336
  configuration: import_zod17.z.object({
2317
2337
  maxFileSize: import_zod17.z.number().describe("Maximum file size in bytes").default(DEFAULT_MAX_FILE_SIZE_BYTES),
2318
2338
  maxImageSize: import_zod17.z.object({
@@ -2325,11 +2345,11 @@ var FileUploadWithOptions = BaseField.extend({
2325
2345
  });
2326
2346
  var Facility = BaseField.extend({
2327
2347
  type: import_zod17.z.literal(FieldType.FACILITY),
2328
- defaultValue: NonEmptyTextValue.optional()
2348
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
2329
2349
  }).describe("Input field for a facility");
2330
2350
  var Office = BaseField.extend({
2331
2351
  type: import_zod17.z.literal(FieldType.OFFICE),
2332
- defaultValue: NonEmptyTextValue.optional()
2352
+ defaultValue: NonEmptyTextValue.or(ComputedDefaultValue).optional()
2333
2353
  }).describe("Input field for an office");
2334
2354
  var DefaultAddressFieldValue = DomesticAddressFieldValue.extend({
2335
2355
  administrativeArea: import_zod17.z.union([UUID, SerializedUserField]).optional()
@@ -2363,7 +2383,7 @@ var Address = BaseField.extend({
2363
2383
  })
2364
2384
  ).optional()
2365
2385
  }).optional(),
2366
- defaultValue: DefaultAddressFieldValue.optional()
2386
+ defaultValue: DefaultAddressFieldValue.or(ComputedDefaultValue).optional()
2367
2387
  }).describe("Address input field \u2013 a combination of location and text fields");
2368
2388
  var StaticDataEntry = import_zod17.z.object({
2369
2389
  id: import_zod17.z.string().describe("ID for the data entry."),
@@ -2382,7 +2402,7 @@ var DataField = BaseField.extend({
2382
2402
  }).describe("Data field for displaying read-only data");
2383
2403
  var ButtonField = BaseField.extend({
2384
2404
  type: import_zod17.z.literal(FieldType.BUTTON),
2385
- defaultValue: ButtonFieldValue.optional(),
2405
+ defaultValue: ButtonFieldValue.or(ComputedDefaultValue).optional(),
2386
2406
  configuration: import_zod17.z.object({
2387
2407
  icon: import_zod17.z.string().optional().describe(
2388
2408
  "Icon for the button. You can find icons from OpenCRVS UI-Kit."
@@ -2407,7 +2427,7 @@ var AlphaPrintButton = BaseField.extend({
2407
2427
  }).describe("Print button field for printing certificates");
2408
2428
  var HttpField = BaseField.extend({
2409
2429
  type: import_zod17.z.literal(FieldType.HTTP),
2410
- defaultValue: HttpFieldValue.optional(),
2430
+ defaultValue: HttpFieldValue.or(ComputedDefaultValue).optional(),
2411
2431
  configuration: import_zod17.z.object({
2412
2432
  trigger: FieldReference,
2413
2433
  url: import_zod17.z.string().describe("URL to send the HTTP request to"),
@@ -2462,7 +2482,7 @@ var LinkButtonField = BaseField.extend({
2462
2482
  }).describe("Button that opens a link");
2463
2483
  var VerificationStatus = BaseField.extend({
2464
2484
  type: import_zod17.z.literal(FieldType.VERIFICATION_STATUS),
2465
- defaultValue: VerificationStatusValue.optional(),
2485
+ defaultValue: VerificationStatusValue.or(ComputedDefaultValue).optional(),
2466
2486
  configuration: import_zod17.z.object({
2467
2487
  status: TranslationConfig.describe("Text to display on the status pill."),
2468
2488
  description: TranslationConfig.describe(
@@ -2480,7 +2500,7 @@ var QueryParamReaderField = BaseField.extend({
2480
2500
  );
2481
2501
  var QrReaderField = BaseField.extend({
2482
2502
  type: import_zod17.z.literal(FieldType.QR_READER),
2483
- defaultValue: QrReaderFieldValue.optional(),
2503
+ defaultValue: QrReaderFieldValue.or(ComputedDefaultValue).optional(),
2484
2504
  configuration: import_zod17.z.object({
2485
2505
  validator: import_zod17.z.custom(
2486
2506
  (val) => typeof val === "object" && val !== null
@@ -2489,7 +2509,7 @@ var QrReaderField = BaseField.extend({
2489
2509
  });
2490
2510
  var IdReaderField = BaseField.extend({
2491
2511
  type: import_zod17.z.literal(FieldType.ID_READER),
2492
- defaultValue: IdReaderFieldValue.optional(),
2512
+ defaultValue: IdReaderFieldValue.or(ComputedDefaultValue).optional(),
2493
2513
  methods: import_zod17.z.array(
2494
2514
  import_zod17.z.union([QrReaderField, LinkButtonField]).describe("Methods for reading an ID")
2495
2515
  )
@@ -3365,6 +3385,18 @@ function resolveDataPath(rootData, dataPath, instancePath) {
3365
3385
  }
3366
3386
  return current;
3367
3387
  }
3388
+ function todayISO() {
3389
+ return (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" });
3390
+ }
3391
+ var compiledFunctionCache = /* @__PURE__ */ new Map();
3392
+ function compileClientFunction(code) {
3393
+ let fn = compiledFunctionCache.get(code);
3394
+ if (!fn) {
3395
+ fn = new Function(`return (${code})`)();
3396
+ compiledFunctionCache.set(code, fn);
3397
+ }
3398
+ return fn;
3399
+ }
3368
3400
  (0, import_ajv_formats.default)(ajv);
3369
3401
  ajv.addKeyword({
3370
3402
  keyword: "daysFromDate",
@@ -3410,7 +3442,7 @@ ajv.addKeyword({
3410
3442
  $data: true,
3411
3443
  errors: true,
3412
3444
  // @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) {
3445
+ validate(_schema, data, _2, dataContext) {
3414
3446
  const locationIdInput = data;
3415
3447
  const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
3416
3448
  return locations.some((location) => location.id === locationIdInput);
@@ -3419,6 +3451,15 @@ ajv.addKeyword({
3419
3451
  function mergeWithBaseFormState(values, context) {
3420
3452
  return { ...context.baseFormState, ...values };
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) {
@@ -3462,7 +3503,7 @@ function isOnline() {
3462
3503
  function isConditionMet(conditional, values, context) {
3463
3504
  return validate(conditional, {
3464
3505
  $form: mergeWithBaseFormState(values, context),
3465
- $now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
3506
+ $now: todayISO(),
3466
3507
  $online: isOnline(),
3467
3508
  $user: context.user,
3468
3509
  $leafAdminStructureLocationIds: context.leafAdminStructureLocationIds ?? [],
@@ -3489,7 +3530,7 @@ function isFieldConditionMet(field3, form, conditionalType, context) {
3489
3530
  }
3490
3531
  const validConditionals = getConditionalActionsForField(field3, {
3491
3532
  $form: mergeWithBaseFormState(form, context),
3492
- $now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
3533
+ $now: todayISO(),
3493
3534
  $online: isOnline(),
3494
3535
  $user: context.user,
3495
3536
  $leafAdminStructureLocationIds: context.leafAdminStructureLocationIds ?? [],
@@ -3642,7 +3683,7 @@ function runFieldValidations({
3642
3683
  }
3643
3684
  const conditionalParameters = {
3644
3685
  $form: mergeWithBaseFormState(form, context),
3645
- $now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
3686
+ $now: todayISO(),
3646
3687
  /**
3647
3688
  * In real use cases, there can be hundreds of thousands of locations.
3648
3689
  * Make sure that the context contains only the locations that are needed for validation.
@@ -4725,7 +4766,54 @@ function createFieldConditionals(fieldId) {
4725
4766
  }
4726
4767
  },
4727
4768
  required: [fieldId]
4728
- })
4769
+ }),
4770
+ /**
4771
+ * Custom client-side validator. The provided function is serialised and executed
4772
+ * just-in-time on the client only. External references (e.g. lodash) are not
4773
+ * available inside the function body — all logic must be self-contained.
4774
+ *
4775
+ * @example
4776
+ * field('nid').customClientValidator((value) => {
4777
+ * // LUHN check — all logic must be inline
4778
+ * const digits = String(value).split('').map(Number)
4779
+ * // ...
4780
+ * return isValid
4781
+ * })
4782
+ */
4783
+ customClientValidator(validationFn) {
4784
+ const code = validationFn.toString();
4785
+ return defineFormConditional({
4786
+ type: "object",
4787
+ properties: wrapToPath(
4788
+ { [fieldId]: { customClientValidator: { code } } },
4789
+ this.$$subfield
4790
+ ),
4791
+ required: [fieldId]
4792
+ });
4793
+ },
4794
+ /**
4795
+ * Custom client-side evaluation. Returns a {@link FieldReference} descriptor
4796
+ * that can be used as the `value` property or a DATA component entry.
4797
+ * The function receives the referenced field's value as the first argument and
4798
+ * the full form context as the second; its return value replaces the field reference.
4799
+ * The function is serialised and executed just-in-time on the client only.
4800
+ * External references (e.g. lodash) are not available inside the function body.
4801
+ *
4802
+ * For computing a default value without referencing a specific field, use
4803
+ * `evaluate(fn)` in the `defaultValue` property instead.
4804
+ *
4805
+ * @example
4806
+ * field('a').customClientEvaluation((aValue, ctx) =>
4807
+ * Number(aValue) + Number(ctx.$form.b)
4808
+ * )
4809
+ */
4810
+ customClientEvaluation(computationFn) {
4811
+ return {
4812
+ $$code: computationFn.toString(),
4813
+ $$field: fieldId,
4814
+ $$subfield: this.$$subfield
4815
+ };
4816
+ }
4729
4817
  };
4730
4818
  }
4731
4819
 
@@ -5920,6 +6008,9 @@ function createFieldConfig(fieldId, options) {
5920
6008
  }
5921
6009
 
5922
6010
  // ../commons/src/events/field.ts
6011
+ function evaluate(computationFn) {
6012
+ return { $$code: computationFn.toString() };
6013
+ }
5923
6014
  function field(fieldId, options = {}) {
5924
6015
  return {
5925
6016
  ...createFieldConditionals(fieldId),