@opencrvs/toolkit 1.8.0-rc.f876361 → 1.8.0-rc.f8e4107

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.
@@ -23,8 +23,11 @@ __export(conditionals_exports, {
23
23
  alwaysTrue: () => alwaysTrue,
24
24
  and: () => and,
25
25
  defineConditional: () => defineConditional,
26
+ defineFormConditional: () => defineFormConditional,
26
27
  event: () => event,
28
+ eventField: () => eventField,
27
29
  field: () => field,
30
+ never: () => never,
28
31
  not: () => not,
29
32
  or: () => or,
30
33
  user: () => user
@@ -35,6 +38,16 @@ module.exports = __toCommonJS(conditionals_exports);
35
38
  function defineConditional(schema) {
36
39
  return schema;
37
40
  }
41
+ function defineFormConditional(schema) {
42
+ const schemaWithForm = {
43
+ type: "object",
44
+ properties: {
45
+ $form: schema
46
+ },
47
+ required: ["$form"]
48
+ };
49
+ return defineConditional(schemaWithForm);
50
+ }
38
51
  function alwaysTrue() {
39
52
  return {};
40
53
  }
@@ -59,6 +72,9 @@ function not(condition) {
59
72
  required: []
60
73
  });
61
74
  }
75
+ function never() {
76
+ return not(alwaysTrue());
77
+ }
62
78
  var user = {
63
79
  hasScope: (scope) => defineConditional({
64
80
  type: "object",
@@ -106,100 +122,118 @@ var event = {
106
122
  required: ["$event"]
107
123
  })
108
124
  };
125
+ function getDateFromNow(days) {
126
+ return new Date(Date.now() - days * 24 * 60 * 60 * 1e3).toISOString().split("T")[0];
127
+ }
128
+ function getDateRangeToFieldReference(fieldId, comparedFieldId, clause) {
129
+ return {
130
+ type: "object",
131
+ properties: {
132
+ [fieldId]: {
133
+ type: "string",
134
+ format: "date",
135
+ [clause]: { $data: `1/${comparedFieldId}` }
136
+ },
137
+ [comparedFieldId]: { type: "string", format: "date" }
138
+ },
139
+ required: [fieldId]
140
+ };
141
+ }
142
+ function isFieldReference(value) {
143
+ return typeof value === "object" && value !== null && "_fieldId" in value;
144
+ }
109
145
  function field(fieldId) {
110
- const getDateFromNow = (days) => new Date(Date.now() - days * 24 * 60 * 60 * 1e3).toISOString().split("T")[0];
111
146
  const getDateRange = (date, clause) => ({
112
147
  type: "object",
113
148
  properties: {
114
- $form: {
115
- type: "object",
116
- properties: {
117
- [fieldId]: {
118
- type: "string",
119
- format: "date",
120
- [clause]: date
121
- }
122
- },
123
- required: [fieldId]
149
+ [fieldId]: {
150
+ type: "string",
151
+ format: "date",
152
+ [clause]: date
124
153
  }
125
154
  },
126
- required: ["$form"]
155
+ required: [fieldId]
127
156
  });
128
157
  return {
129
158
  /**
130
159
  * @private Internal property used for field reference tracking.
131
160
  */
132
- _returning: fieldId,
161
+ _fieldId: fieldId,
133
162
  isAfter: () => ({
134
163
  days: (days) => ({
135
- inPast: () => defineConditional(
164
+ inPast: () => defineFormConditional(
136
165
  getDateRange(getDateFromNow(days), "formatMinimum")
137
166
  ),
138
- inFuture: () => defineConditional(
167
+ inFuture: () => defineFormConditional(
139
168
  getDateRange(getDateFromNow(-days), "formatMinimum")
140
169
  )
141
170
  }),
142
- date: (date) => defineConditional(getDateRange(date, "formatMinimum")),
143
- now: () => defineConditional(getDateRange(getDateFromNow(0), "formatMinimum"))
171
+ date: (date) => {
172
+ if (isFieldReference(date)) {
173
+ const comparedFieldId = date._fieldId;
174
+ return defineFormConditional(
175
+ getDateRangeToFieldReference(
176
+ fieldId,
177
+ comparedFieldId,
178
+ "formatMinimum"
179
+ )
180
+ );
181
+ }
182
+ return defineFormConditional(getDateRange(date, "formatMinimum"));
183
+ },
184
+ now: () => defineFormConditional(getDateRange(getDateFromNow(0), "formatMinimum"))
144
185
  }),
145
186
  isBefore: () => ({
146
187
  days: (days) => ({
147
- inPast: () => defineConditional(
188
+ inPast: () => defineFormConditional(
148
189
  getDateRange(getDateFromNow(days), "formatMaximum")
149
190
  ),
150
- inFuture: () => defineConditional(
191
+ inFuture: () => defineFormConditional(
151
192
  getDateRange(getDateFromNow(-days), "formatMaximum")
152
193
  )
153
194
  }),
154
- date: (date) => defineConditional(getDateRange(date, "formatMaximum")),
155
- now: () => defineConditional(getDateRange(getDateFromNow(0), "formatMaximum"))
195
+ date: (date) => {
196
+ if (isFieldReference(date)) {
197
+ const comparedFieldId = date._fieldId;
198
+ return defineFormConditional(
199
+ getDateRangeToFieldReference(
200
+ fieldId,
201
+ comparedFieldId,
202
+ "formatMaximum"
203
+ )
204
+ );
205
+ }
206
+ return defineFormConditional(getDateRange(date, "formatMaximum"));
207
+ },
208
+ now: () => defineFormConditional(getDateRange(getDateFromNow(0), "formatMaximum"))
156
209
  }),
157
- // TODO CIHAN: typing
158
210
  isEqualTo: (value) => {
159
- if (typeof value === "object" && value._returning) {
160
- const comparedFieldId = value._returning;
161
- return defineConditional({
211
+ if (isFieldReference(value)) {
212
+ const comparedFieldId = value._fieldId;
213
+ return defineFormConditional({
162
214
  type: "object",
163
215
  properties: {
164
- $form: {
165
- type: "object",
166
- properties: {
167
- [fieldId]: { type: ["string", "boolean"] },
168
- [comparedFieldId]: { type: ["string", "boolean"] }
169
- },
170
- required: [fieldId, comparedFieldId],
171
- allOf: [
172
- {
173
- properties: {
174
- [fieldId]: {
175
- const: { $data: `1/${comparedFieldId}` }
176
- }
177
- }
178
- }
179
- ]
180
- }
216
+ [fieldId]: {
217
+ type: ["string", "boolean"],
218
+ const: { $data: `1/${comparedFieldId}` }
219
+ },
220
+ [comparedFieldId]: { type: ["string", "boolean"] }
181
221
  },
182
- required: ["$form"]
222
+ required: [fieldId, comparedFieldId]
183
223
  });
184
224
  }
185
- return defineConditional({
225
+ return defineFormConditional({
186
226
  type: "object",
187
227
  properties: {
188
- $form: {
189
- type: "object",
190
- properties: {
191
- [fieldId]: {
192
- oneOf: [
193
- { type: "string", const: value },
194
- { type: "boolean", const: value }
195
- ],
196
- const: value
197
- }
198
- },
199
- required: [fieldId]
228
+ [fieldId]: {
229
+ oneOf: [
230
+ { type: "string", const: value },
231
+ { type: "boolean", const: value }
232
+ ],
233
+ const: value
200
234
  }
201
235
  },
202
- required: ["$form"]
236
+ required: [fieldId]
203
237
  });
204
238
  },
205
239
  /**
@@ -210,140 +244,135 @@ function field(fieldId) {
210
244
  * NOTE: For now, this only works with string, boolean, and null types. 0 is still allowed.
211
245
  *
212
246
  */
213
- isFalsy: () => defineConditional({
247
+ isFalsy: () => defineFormConditional({
214
248
  type: "object",
215
249
  properties: {
216
- $form: {
217
- type: "object",
218
- properties: {
219
- [fieldId]: {
220
- anyOf: [
221
- { const: "undefined" },
222
- { const: false },
223
- { const: null },
224
- { const: "" }
225
- ]
226
- }
227
- },
250
+ [fieldId]: {
228
251
  anyOf: [
229
- {
230
- required: [fieldId]
231
- },
232
- {
233
- not: {
234
- required: [fieldId]
235
- }
236
- }
252
+ { const: "undefined" },
253
+ { const: false },
254
+ { const: null },
255
+ { const: "" }
237
256
  ]
238
257
  }
239
258
  },
240
- required: ["$form"]
241
- }),
242
- isUndefined: () => defineConditional({
243
- type: "object",
244
- properties: {
245
- $form: {
246
- type: "object",
247
- properties: {
248
- [fieldId]: {
249
- type: "string",
250
- enum: ["undefined"]
251
- }
252
- },
259
+ anyOf: [
260
+ {
261
+ required: [fieldId]
262
+ },
263
+ {
253
264
  not: {
254
265
  required: [fieldId]
255
266
  }
256
267
  }
257
- },
258
- required: ["$form"]
268
+ ]
259
269
  }),
260
- inArray: (values) => defineConditional({
270
+ isUndefined: () => defineFormConditional({
261
271
  type: "object",
262
272
  properties: {
263
- $form: {
264
- type: "object",
265
- properties: {
266
- [fieldId]: {
267
- type: "string",
268
- enum: values
269
- }
270
- },
271
- required: [fieldId]
273
+ [fieldId]: {
274
+ type: "string",
275
+ enum: ["undefined"]
272
276
  }
273
277
  },
274
- required: ["$form"]
278
+ not: {
279
+ required: [fieldId]
280
+ }
275
281
  }),
276
- isValidEnglishName: () => defineConditional({
282
+ inArray: (values) => defineFormConditional({
277
283
  type: "object",
278
284
  properties: {
279
- $form: {
280
- type: "object",
281
- properties: {
282
- [fieldId]: {
283
- type: "string",
284
- pattern: "^[\\p{Script=Latin}0-9'._-]*(\\([\\p{Script=Latin}0-9'._-]+\\))?[\\p{Script=Latin}0-9'._-]*( [\\p{Script=Latin}0-9'._-]*(\\([\\p{Script=Latin}0-9'._-]+\\))?[\\p{Script=Latin}0-9'._-]*)*$",
285
- description: "Name must contain only letters, numbers, and allowed special characters ('._-). No double spaces."
286
- }
287
- },
288
- required: [fieldId]
285
+ [fieldId]: {
286
+ type: "string",
287
+ enum: values
289
288
  }
290
289
  },
291
- required: ["$form"]
290
+ required: [fieldId]
292
291
  }),
293
- isValidNationalId: () => defineConditional({
292
+ isValidEnglishName: () => defineFormConditional({
294
293
  type: "object",
295
294
  properties: {
296
- $form: {
297
- type: "object",
298
- properties: {
299
- [fieldId]: {
300
- type: "string",
301
- pattern: "^[0-9]{9}$",
302
- description: "The National ID can only be numeric and must be 9 digits long."
303
- }
304
- },
305
- required: [fieldId]
295
+ [fieldId]: {
296
+ type: "string",
297
+ pattern: "^[\\p{Script=Latin}0-9'._-]*(\\([\\p{Script=Latin}0-9'._-]+\\))?[\\p{Script=Latin}0-9'._-]*( [\\p{Script=Latin}0-9'._-]*(\\([\\p{Script=Latin}0-9'._-]+\\))?[\\p{Script=Latin}0-9'._-]*)*$",
298
+ description: "Name must contain only letters, numbers, and allowed special characters ('._-). No double spaces."
306
299
  }
307
300
  },
308
- required: ["$form"]
301
+ required: [fieldId]
309
302
  }),
310
303
  /**
311
304
  * Checks if the field value matches a given regular expression pattern.
312
305
  * @param pattern - The regular expression pattern to match the field value against.
313
306
  * @returns A JSONSchema conditional that validates the field value against the pattern.
314
307
  */
315
- matches: (pattern) => defineConditional({
308
+ matches: (pattern) => defineFormConditional({
316
309
  type: "object",
317
310
  properties: {
318
- $form: {
319
- type: "object",
320
- properties: {
321
- [fieldId]: {
322
- type: "string",
323
- pattern
324
- }
325
- },
326
- required: [fieldId]
311
+ [fieldId]: {
312
+ type: "string",
313
+ pattern
327
314
  }
328
315
  },
329
- required: ["$form"]
316
+ required: [fieldId]
330
317
  }),
331
- isBetween: (min, max) => defineConditional({
318
+ isBetween: (min, max) => defineFormConditional({
332
319
  type: "object",
333
320
  properties: {
334
- $form: {
335
- type: "object",
336
- properties: {
337
- [fieldId]: {
338
- type: "number",
339
- minimum: min,
340
- maximum: max
341
- }
342
- },
343
- required: [fieldId]
321
+ [fieldId]: {
322
+ type: "number",
323
+ minimum: min,
324
+ maximum: max
344
325
  }
345
326
  },
346
- required: ["$form"]
327
+ required: [fieldId]
328
+ }),
329
+ /**
330
+ * Creates a range configuration for the specified field.
331
+ *
332
+ * @returns An object containing the field ID and a configuration object with a type of 'RANGE'.
333
+ *
334
+ * @example field('age').range()
335
+ * // {
336
+ * // fieldId: 'age',
337
+ * // config: { type: 'RANGE' }
338
+ * // }
339
+ */
340
+ range: () => ({
341
+ fieldId,
342
+ config: { type: "RANGE" }
343
+ }),
344
+ /**
345
+ * Creates a configuration for exact matching of the specified field.
346
+ * @returns An object containing the field ID and a configuration object with a type of 'EXACT'.
347
+ * @example field('dob').exact()
348
+ * // {
349
+ * // fieldId: 'dob',
350
+ * // config: { type: 'EXACT' }
351
+ * // }
352
+ */
353
+ exact: () => ({
354
+ fieldId,
355
+ config: { type: "EXACT" }
356
+ }),
357
+ /**
358
+ * Creates a configuration for fuzzy matching of the specified field.
359
+ * @returns An object containing the field ID and a configuration object with a type of 'EXACT'.
360
+ * @example field('name').fuzzy()
361
+ * // {
362
+ * // fieldId: 'name',
363
+ * // config: { type: 'FUZZY' }
364
+ * // }
365
+ */
366
+ fuzzy: () => ({
367
+ fieldId,
368
+ config: { type: "FUZZY" }
347
369
  })
348
370
  };
349
371
  }
372
+ function eventField(fieldId, options) {
373
+ return {
374
+ fieldId,
375
+ options,
376
+ config: { type: "EXACT" }
377
+ };
378
+ }