@ecrvs/opencrvs-toolkit 1.8.1-rc.9775e4e → 1.8.1-rc.b48c48d

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.
@@ -122,6 +122,7 @@ export declare function createFieldConditionals(fieldId: string): {
122
122
  isLessThan: (value: number | FieldReference) => JSONSchema;
123
123
  isEqualTo(value: string | boolean | FieldReference): JSONSchema;
124
124
  isEqualToSumOf(val1: FieldReference, val2: FieldReference): JSONSchema;
125
+ isGreaterThanOrEqualTo(value: number | string | FieldReference): JSONSchema;
125
126
  /**
126
127
  * Use case: Some fields are rendered when selection is not made, or boolean false is explicitly selected.
127
128
  * @example field('recommender.none').isFalsy() vs not(field('recommender.none').isEqualTo(true))
@@ -167,6 +168,7 @@ export declare function createFieldConditionals(fieldId: string): {
167
168
  isLessThan: (value: number | FieldReference) => JSONSchema;
168
169
  isEqualTo(value: string | boolean | FieldReference): JSONSchema;
169
170
  isEqualToSumOf(val1: FieldReference, val2: FieldReference): JSONSchema;
171
+ isGreaterThanOrEqualTo(value: number | string | FieldReference): JSONSchema;
170
172
  /**
171
173
  * Use case: Some fields are rendered when selection is not made, or boolean false is explicitly selected.
172
174
  * @example field('recommender.none').isFalsy() vs not(field('recommender.none').isEqualTo(true))
@@ -100,6 +100,9 @@ export declare function field(fieldId: string, options?: {
100
100
  }, val2: {
101
101
  $$field: string;
102
102
  }): import("../conditionals/conditionals").JSONSchema;
103
+ isGreaterThanOrEqualTo(value: number | string | {
104
+ $$field: string;
105
+ }): import("../conditionals/conditionals").JSONSchema;
103
106
  isFalsy(): import("../conditionals/conditionals").JSONSchema;
104
107
  isUndefined(): import("../conditionals/conditionals").JSONSchema;
105
108
  inArray: (values: string[]) => import("../conditionals/conditionals").JSONSchema;
@@ -146,6 +149,9 @@ export declare function field(fieldId: string, options?: {
146
149
  }, val2: {
147
150
  $$field: string;
148
151
  }): import("../conditionals/conditionals").JSONSchema;
152
+ isGreaterThanOrEqualTo(value: number | string | {
153
+ $$field: string;
154
+ }): import("../conditionals/conditionals").JSONSchema;
149
155
  isFalsy(): import("../conditionals/conditionals").JSONSchema;
150
156
  isUndefined(): import("../conditionals/conditionals").JSONSchema;
151
157
  inArray: (values: string[]) => import("../conditionals/conditionals").JSONSchema;
@@ -366,11 +366,35 @@ function createFieldConditionals(fieldId) {
366
366
  [field2Id]: { type: "number" }
367
367
  },
368
368
  required: [fieldId, field1Id, field2Id],
369
- sumOf: [
370
- { $data: `/$form/${fieldId}` },
371
- { $data: `/$form/${field1Id}` },
372
- { $data: `/$form/${field2Id}` }
373
- ]
369
+ sumOf: {
370
+ sum: { $data: `/$form/${fieldId}` },
371
+ field1: { $data: `/$form/${field1Id}` },
372
+ field2: { $data: `/$form/${field2Id}` }
373
+ }
374
+ });
375
+ },
376
+ isGreaterThanOrEqualTo(value) {
377
+ const fieldSchema = { type: "number" };
378
+ const properties = {};
379
+ const required = [fieldId];
380
+ if (typeof value === "number") {
381
+ fieldSchema.minimum = value;
382
+ } else {
383
+ let refField = "";
384
+ if (isFieldReference(value)) {
385
+ refField = value.$$field;
386
+ } else {
387
+ refField = value;
388
+ }
389
+ fieldSchema.minimum = { $data: `/$form/${refField}` };
390
+ properties[refField] = { type: "number" };
391
+ required.push(refField);
392
+ }
393
+ properties[fieldId] = fieldSchema;
394
+ return defineFormConditional({
395
+ type: "object",
396
+ properties,
397
+ required
374
398
  });
375
399
  },
376
400
  /**
@@ -2543,18 +2543,24 @@ ajv.addKeyword({
2543
2543
  ajv.addKeyword({
2544
2544
  keyword: "sumOf",
2545
2545
  type: "object",
2546
- schemaType: "array",
2546
+ schemaType: "object",
2547
2547
  errors: true,
2548
2548
  $data: true,
2549
- validate(schema) {
2550
- if (!Array.isArray(schema) || schema.length !== 3) {
2551
- return true;
2552
- }
2553
- const [a, b, c] = schema;
2554
- if ([a, b, c].some((v) => typeof v !== "number")) {
2549
+ validate(schema, data) {
2550
+ const { sum, field1, field2: field22 } = schema;
2551
+ console.log(schema, data);
2552
+ const getRefPath = (ref) => typeof ref === "object" && "$data" in ref ? ref.$data : ref;
2553
+ const sumKey = getRefPath(sum);
2554
+ const field1Key = getRefPath(field1);
2555
+ const field2Key = getRefPath(field22);
2556
+ const total = data?.$form?.[sumKey];
2557
+ const num1 = data?.$form?.[field1Key];
2558
+ const num2 = data?.$form?.[field2Key];
2559
+ console.log(total, num1, num2);
2560
+ if (typeof total !== "number" || typeof num1 !== "number" || typeof num2 !== "number") {
2555
2561
  return true;
2556
2562
  }
2557
- const valid = a === b + c;
2563
+ const valid = total === num1 + num2;
2558
2564
  return valid;
2559
2565
  }
2560
2566
  });
@@ -3545,11 +3551,35 @@ function createFieldConditionals(fieldId) {
3545
3551
  [field2Id]: { type: "number" }
3546
3552
  },
3547
3553
  required: [fieldId, field1Id, field2Id],
3548
- sumOf: [
3549
- { $data: `/$form/${fieldId}` },
3550
- { $data: `/$form/${field1Id}` },
3551
- { $data: `/$form/${field2Id}` }
3552
- ]
3554
+ sumOf: {
3555
+ sum: { $data: `/$form/${fieldId}` },
3556
+ field1: { $data: `/$form/${field1Id}` },
3557
+ field2: { $data: `/$form/${field2Id}` }
3558
+ }
3559
+ });
3560
+ },
3561
+ isGreaterThanOrEqualTo(value) {
3562
+ const fieldSchema = { type: "number" };
3563
+ const properties = {};
3564
+ const required = [fieldId];
3565
+ if (typeof value === "number") {
3566
+ fieldSchema.minimum = value;
3567
+ } else {
3568
+ let refField = "";
3569
+ if (isFieldReference(value)) {
3570
+ refField = value.$$field;
3571
+ } else {
3572
+ refField = value;
3573
+ }
3574
+ fieldSchema.minimum = { $data: `/$form/${refField}` };
3575
+ properties[refField] = { type: "number" };
3576
+ required.push(refField);
3577
+ }
3578
+ properties[fieldId] = fieldSchema;
3579
+ return defineFormConditional({
3580
+ type: "object",
3581
+ properties,
3582
+ required
3553
3583
  });
3554
3584
  },
3555
3585
  /**
@@ -1949,18 +1949,24 @@ ajv.addKeyword({
1949
1949
  ajv.addKeyword({
1950
1950
  keyword: "sumOf",
1951
1951
  type: "object",
1952
- schemaType: "array",
1952
+ schemaType: "object",
1953
1953
  errors: true,
1954
1954
  $data: true,
1955
- validate(schema) {
1956
- if (!Array.isArray(schema) || schema.length !== 3) {
1957
- return true;
1958
- }
1959
- const [a, b, c] = schema;
1960
- if ([a, b, c].some((v) => typeof v !== "number")) {
1955
+ validate(schema, data) {
1956
+ const { sum, field1, field2: field22 } = schema;
1957
+ console.log(schema, data);
1958
+ const getRefPath = (ref) => typeof ref === "object" && "$data" in ref ? ref.$data : ref;
1959
+ const sumKey = getRefPath(sum);
1960
+ const field1Key = getRefPath(field1);
1961
+ const field2Key = getRefPath(field22);
1962
+ const total = data?.$form?.[sumKey];
1963
+ const num1 = data?.$form?.[field1Key];
1964
+ const num2 = data?.$form?.[field2Key];
1965
+ console.log(total, num1, num2);
1966
+ if (typeof total !== "number" || typeof num1 !== "number" || typeof num2 !== "number") {
1961
1967
  return true;
1962
1968
  }
1963
- const valid = a === b + c;
1969
+ const valid = total === num1 + num2;
1964
1970
  return valid;
1965
1971
  }
1966
1972
  });
@@ -2364,11 +2370,35 @@ function createFieldConditionals(fieldId) {
2364
2370
  [field2Id]: { type: "number" }
2365
2371
  },
2366
2372
  required: [fieldId, field1Id, field2Id],
2367
- sumOf: [
2368
- { $data: `/$form/${fieldId}` },
2369
- { $data: `/$form/${field1Id}` },
2370
- { $data: `/$form/${field2Id}` }
2371
- ]
2373
+ sumOf: {
2374
+ sum: { $data: `/$form/${fieldId}` },
2375
+ field1: { $data: `/$form/${field1Id}` },
2376
+ field2: { $data: `/$form/${field2Id}` }
2377
+ }
2378
+ });
2379
+ },
2380
+ isGreaterThanOrEqualTo(value) {
2381
+ const fieldSchema = { type: "number" };
2382
+ const properties = {};
2383
+ const required = [fieldId];
2384
+ if (typeof value === "number") {
2385
+ fieldSchema.minimum = value;
2386
+ } else {
2387
+ let refField = "";
2388
+ if (isFieldReference(value)) {
2389
+ refField = value.$$field;
2390
+ } else {
2391
+ refField = value;
2392
+ }
2393
+ fieldSchema.minimum = { $data: `/$form/${refField}` };
2394
+ properties[refField] = { type: "number" };
2395
+ required.push(refField);
2396
+ }
2397
+ properties[fieldId] = fieldSchema;
2398
+ return defineFormConditional({
2399
+ type: "object",
2400
+ properties,
2401
+ required
2372
2402
  });
2373
2403
  },
2374
2404
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecrvs/opencrvs-toolkit",
3
- "version": "1.8.1-rc.9775e4e",
3
+ "version": "1.8.1-rc.b48c48d",
4
4
  "description": "OpenCRVS toolkit for building country configurations",
5
5
  "license": "MPL-2.0",
6
6
  "exports": {