@opencrvs/toolkit 1.9.13-rc.581231b → 1.9.13-rc.59bb028
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.
- package/dist/commons/conditionals/conditionals.d.ts +292 -1
- package/dist/commons/conditionals/validate.d.ts +8 -1
- package/dist/commons/events/FieldConfig.d.ts +3387 -289
- package/dist/commons/events/field.d.ts +22 -0
- package/dist/commons/events/utils.d.ts +170 -1
- package/dist/conditionals/index.js +2028 -1
- package/dist/events/index.js +105 -18
- package/dist/notification/index.js +82 -7
- package/package.json +1 -1
package/dist/events/index.js
CHANGED
|
@@ -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."
|
|
@@ -2154,10 +2174,12 @@ var ParagraphConfiguration = import_zod17.z.object({
|
|
|
2154
2174
|
}).default({});
|
|
2155
2175
|
var Paragraph = BaseField.extend({
|
|
2156
2176
|
type: import_zod17.z.literal(FieldType.PARAGRAPH),
|
|
2177
|
+
defaultValue: NonEmptyTextValue.optional(),
|
|
2157
2178
|
configuration: ParagraphConfiguration
|
|
2158
2179
|
}).describe("A read-only HTML <p> paragraph");
|
|
2159
2180
|
var PageHeader = BaseField.extend({
|
|
2160
|
-
type: import_zod17.z.literal(FieldType.PAGE_HEADER)
|
|
2181
|
+
type: import_zod17.z.literal(FieldType.PAGE_HEADER),
|
|
2182
|
+
defaultValue: NonEmptyTextValue.optional()
|
|
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),
|
|
@@ -2207,6 +2229,7 @@ var RadioGroup = BaseField.extend({
|
|
|
2207
2229
|
}).describe("Grouped radio options");
|
|
2208
2230
|
var BulletList = BaseField.extend({
|
|
2209
2231
|
type: import_zod17.z.literal(FieldType.BULLET_LIST),
|
|
2232
|
+
defaultValue: TextValue.optional(),
|
|
2210
2233
|
items: import_zod17.z.array(TranslationConfig).describe("A list of items"),
|
|
2211
2234
|
configuration: import_zod17.z.object({
|
|
2212
2235
|
styles: import_zod17.z.object({
|
|
@@ -2368,7 +2391,7 @@ var Address = BaseField.extend({
|
|
|
2368
2391
|
var StaticDataEntry = import_zod17.z.object({
|
|
2369
2392
|
id: import_zod17.z.string().describe("ID for the data entry."),
|
|
2370
2393
|
label: TranslationConfig,
|
|
2371
|
-
value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference)
|
|
2394
|
+
value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference).or(CodeToEvaluate)
|
|
2372
2395
|
}).describe("Static data entry");
|
|
2373
2396
|
var DataEntry = import_zod17.z.union([StaticDataEntry, import_zod17.z.object({ fieldId: import_zod17.z.string() })]).describe(
|
|
2374
2397
|
"Data entry can be either a static data entry, or a reference to another field in the current form or the declaration."
|
|
@@ -2415,7 +2438,7 @@ var HttpField = BaseField.extend({
|
|
|
2415
2438
|
headers: import_zod17.z.record(import_zod17.z.string()).optional(),
|
|
2416
2439
|
body: import_zod17.z.record(import_zod17.z.any()).optional(),
|
|
2417
2440
|
errorValue: import_zod17.z.any().optional().describe("Value to set if the request fails"),
|
|
2418
|
-
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(),
|
|
2419
2442
|
timeout: import_zod17.z.number().default(15e3).describe("Request timeout in milliseconds")
|
|
2420
2443
|
})
|
|
2421
2444
|
}).describe("HTTP request function triggered by a button click or other event");
|
|
@@ -3365,6 +3388,18 @@ function resolveDataPath(rootData, dataPath, instancePath) {
|
|
|
3365
3388
|
}
|
|
3366
3389
|
return current;
|
|
3367
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
|
+
}
|
|
3368
3403
|
(0, import_ajv_formats.default)(ajv);
|
|
3369
3404
|
ajv.addKeyword({
|
|
3370
3405
|
keyword: "daysFromDate",
|
|
@@ -3410,15 +3445,21 @@ ajv.addKeyword({
|
|
|
3410
3445
|
$data: true,
|
|
3411
3446
|
errors: true,
|
|
3412
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.
|
|
3413
|
-
validate(
|
|
3448
|
+
validate(_schema, data, _2, dataContext) {
|
|
3414
3449
|
const locationIdInput = data;
|
|
3415
3450
|
const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
|
|
3416
3451
|
return locations.some((location) => location.id === locationIdInput);
|
|
3417
3452
|
}
|
|
3418
3453
|
});
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
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) {
|
|
@@ -3461,7 +3502,7 @@ function isOnline() {
|
|
|
3461
3502
|
}
|
|
3462
3503
|
function isConditionMet(conditional, values, context) {
|
|
3463
3504
|
return validate(conditional, {
|
|
3464
|
-
$form:
|
|
3505
|
+
$form: values,
|
|
3465
3506
|
$now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
|
|
3466
3507
|
$online: isOnline(),
|
|
3467
3508
|
$user: context.user,
|
|
@@ -3488,7 +3529,7 @@ function isFieldConditionMet(field3, form, conditionalType, context) {
|
|
|
3488
3529
|
return true;
|
|
3489
3530
|
}
|
|
3490
3531
|
const validConditionals = getConditionalActionsForField(field3, {
|
|
3491
|
-
$form:
|
|
3532
|
+
$form: form,
|
|
3492
3533
|
$now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
|
|
3493
3534
|
$online: isOnline(),
|
|
3494
3535
|
$user: context.user,
|
|
@@ -3641,7 +3682,7 @@ function runFieldValidations({
|
|
|
3641
3682
|
return [];
|
|
3642
3683
|
}
|
|
3643
3684
|
const conditionalParameters = {
|
|
3644
|
-
$form:
|
|
3685
|
+
$form: form,
|
|
3645
3686
|
$now: (0, import_date_fns.formatISO)(/* @__PURE__ */ new Date(), { representation: "date" }),
|
|
3646
3687
|
/**
|
|
3647
3688
|
* In real use cases, there can be hundreds of thousands of locations.
|
|
@@ -3873,7 +3914,7 @@ function omitHiddenPaginatedFields(formConfig, values, validatorContext, include
|
|
|
3873
3914
|
const visibleFields = formConfig.pages.filter((p) => isPageVisible(p, values, validatorContext)).flatMap((p) => p.fields);
|
|
3874
3915
|
const hiddenFields = formConfig.pages.filter((p) => !isPageVisible(p, values, validatorContext)).flatMap((p) => p.fields);
|
|
3875
3916
|
const valuesExceptHiddenPage = (0, import_lodash.omitBy)(values, (_2, fieldId) => {
|
|
3876
|
-
return hiddenFields.some((f) => f.id === fieldId)
|
|
3917
|
+
return hiddenFields.some((f) => f.id === fieldId);
|
|
3877
3918
|
});
|
|
3878
3919
|
return omitHiddenFields(
|
|
3879
3920
|
visibleFields,
|
|
@@ -3920,10 +3961,11 @@ function getVisibleVerificationPageIds(pages, annotation, context) {
|
|
|
3920
3961
|
}
|
|
3921
3962
|
function omitHiddenAnnotationFields(actionConfig, declaration, annotation, context) {
|
|
3922
3963
|
const annotationFields = getActionAnnotationFields(actionConfig);
|
|
3923
|
-
return omitHiddenFields(
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3964
|
+
return omitHiddenFields(
|
|
3965
|
+
annotationFields,
|
|
3966
|
+
{ ...declaration, ...annotation },
|
|
3967
|
+
context
|
|
3968
|
+
);
|
|
3927
3969
|
}
|
|
3928
3970
|
function deepMerge(currentDocument, actionDocument) {
|
|
3929
3971
|
const currentDocumentClone = (0, import_lodash.cloneDeep)(currentDocument);
|
|
@@ -4291,6 +4333,9 @@ function isFieldReference(value) {
|
|
|
4291
4333
|
function isEventFieldReference(value) {
|
|
4292
4334
|
return typeof value === "object" && value !== null && "$$event" in value;
|
|
4293
4335
|
}
|
|
4336
|
+
function isResolvableValueReference(value) {
|
|
4337
|
+
return isFieldReference(value) || isCodeToEvaluate(value);
|
|
4338
|
+
}
|
|
4294
4339
|
function getDateRangeToFieldReference(field3, comparedField, clause) {
|
|
4295
4340
|
return {
|
|
4296
4341
|
type: "object",
|
|
@@ -4725,7 +4770,49 @@ function createFieldConditionals(fieldId) {
|
|
|
4725
4770
|
}
|
|
4726
4771
|
},
|
|
4727
4772
|
required: [fieldId]
|
|
4728
|
-
})
|
|
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
|
+
}
|
|
4729
4816
|
};
|
|
4730
4817
|
}
|
|
4731
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."
|
|
@@ -1660,10 +1672,12 @@ var ParagraphConfiguration = import_zod17.z.object({
|
|
|
1660
1672
|
}).default({});
|
|
1661
1673
|
var Paragraph = BaseField.extend({
|
|
1662
1674
|
type: import_zod17.z.literal(FieldType.PARAGRAPH),
|
|
1675
|
+
defaultValue: NonEmptyTextValue.optional(),
|
|
1663
1676
|
configuration: ParagraphConfiguration
|
|
1664
1677
|
}).describe("A read-only HTML <p> paragraph");
|
|
1665
1678
|
var PageHeader = BaseField.extend({
|
|
1666
|
-
type: import_zod17.z.literal(FieldType.PAGE_HEADER)
|
|
1679
|
+
type: import_zod17.z.literal(FieldType.PAGE_HEADER),
|
|
1680
|
+
defaultValue: NonEmptyTextValue.optional()
|
|
1667
1681
|
}).describe("A read-only header component for form pages");
|
|
1668
1682
|
var File = BaseField.extend({
|
|
1669
1683
|
type: import_zod17.z.literal(FieldType.FILE),
|
|
@@ -1713,6 +1727,7 @@ var RadioGroup = BaseField.extend({
|
|
|
1713
1727
|
}).describe("Grouped radio options");
|
|
1714
1728
|
var BulletList = BaseField.extend({
|
|
1715
1729
|
type: import_zod17.z.literal(FieldType.BULLET_LIST),
|
|
1730
|
+
defaultValue: TextValue.optional(),
|
|
1716
1731
|
items: import_zod17.z.array(TranslationConfig).describe("A list of items"),
|
|
1717
1732
|
configuration: import_zod17.z.object({
|
|
1718
1733
|
styles: import_zod17.z.object({
|
|
@@ -1874,7 +1889,7 @@ var Address = BaseField.extend({
|
|
|
1874
1889
|
var StaticDataEntry = import_zod17.z.object({
|
|
1875
1890
|
id: import_zod17.z.string().describe("ID for the data entry."),
|
|
1876
1891
|
label: TranslationConfig,
|
|
1877
|
-
value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference)
|
|
1892
|
+
value: TranslationConfig.or(import_zod17.z.string()).or(FieldReference).or(CodeToEvaluate)
|
|
1878
1893
|
}).describe("Static data entry");
|
|
1879
1894
|
var DataEntry = import_zod17.z.union([StaticDataEntry, import_zod17.z.object({ fieldId: import_zod17.z.string() })]).describe(
|
|
1880
1895
|
"Data entry can be either a static data entry, or a reference to another field in the current form or the declaration."
|
|
@@ -1921,7 +1936,7 @@ var HttpField = BaseField.extend({
|
|
|
1921
1936
|
headers: import_zod17.z.record(import_zod17.z.string()).optional(),
|
|
1922
1937
|
body: import_zod17.z.record(import_zod17.z.any()).optional(),
|
|
1923
1938
|
errorValue: import_zod17.z.any().optional().describe("Value to set if the request fails"),
|
|
1924
|
-
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(),
|
|
1925
1940
|
timeout: import_zod17.z.number().default(15e3).describe("Request timeout in milliseconds")
|
|
1926
1941
|
})
|
|
1927
1942
|
}).describe("HTTP request function triggered by a button click or other event");
|
|
@@ -2556,6 +2571,15 @@ function resolveDataPath(rootData, dataPath, instancePath) {
|
|
|
2556
2571
|
}
|
|
2557
2572
|
return current;
|
|
2558
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
|
+
}
|
|
2559
2583
|
(0, import_ajv_formats.default)(ajv);
|
|
2560
2584
|
ajv.addKeyword({
|
|
2561
2585
|
keyword: "daysFromDate",
|
|
@@ -2601,12 +2625,21 @@ ajv.addKeyword({
|
|
|
2601
2625
|
$data: true,
|
|
2602
2626
|
errors: true,
|
|
2603
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.
|
|
2604
|
-
validate(
|
|
2628
|
+
validate(_schema, data, _2, dataContext) {
|
|
2605
2629
|
const locationIdInput = data;
|
|
2606
2630
|
const locations = dataContext?.rootData.$leafAdminStructureLocationIds ?? [];
|
|
2607
2631
|
return locations.some((location) => location.id === locationIdInput);
|
|
2608
2632
|
}
|
|
2609
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
|
+
});
|
|
2610
2643
|
|
|
2611
2644
|
// ../commons/src/utils.ts
|
|
2612
2645
|
var z29 = __toESM(require("zod"));
|
|
@@ -3229,7 +3262,49 @@ function createFieldConditionals(fieldId) {
|
|
|
3229
3262
|
}
|
|
3230
3263
|
},
|
|
3231
3264
|
required: [fieldId]
|
|
3232
|
-
})
|
|
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
|
+
}
|
|
3233
3308
|
};
|
|
3234
3309
|
}
|
|
3235
3310
|
|