Skip to content

Commit

Permalink
Cancel cardinality checks (#78)
Browse files Browse the repository at this point in the history
* feat: added $capabilities() function

* fix: package lock

* fix: removed incorrect cardinality errors
  • Loading branch information
mechanik-daniel authored Jun 1, 2024
1 parent 3fc8762 commit d73118c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
5 changes: 2 additions & 3 deletions src/helpers/parser/toJsonataString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,10 @@ export const toJsonataString = async (inExpr: string): Promise<string | undefine
min: eDef?.min ?? 0,
path: pathForCardinality,
eDefPath,
kind,
mandatory: mandatoryObj
kind
};
// construct line
const line = `${prefix}$__flashInstance := $__flashMerge(${JSON.stringify(mergeOptions)},$__flashInstance,$__castToFhir(${JSON.stringify(castOptions)}, ${contextPart}(${valuePart};`;
const line = `${prefix}$__context := ${contextPart}($);$__flashInstance := $__flashMerge($merge([${JSON.stringify(mergeOptions)},{'mandatory': $count($__context)>0 ? ${JSON.stringify(mandatoryObj)} : undefined }]),$__flashInstance,$__castToFhir(${JSON.stringify(castOptions)}, ${contextPart}(${valuePart};`;
return line;
};
} else if (nodes.length > 1) {
Expand Down
34 changes: 17 additions & 17 deletions src/helpers/runtime/flashMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import _ from 'lodash';

import { isEmpty } from '../objectFunctions';
import thrower from '../thrower';
// import thrower from '../thrower';

export interface FlashMergeOptions {
key: string // name of the element
Expand Down Expand Up @@ -44,22 +44,22 @@ const addToArr = (arr, value) => {
};

export const flashMerge = async (options: FlashMergeOptions, baseObj: any, ruleObj: any): Promise<any> => {
const empty: boolean = await isEmpty(ruleObj);
if (options?.min === 0) {
if (empty) return baseObj;
} else {
let amountOfElements = 1;
if (empty) {
amountOfElements = 0;
} else {
try {
amountOfElements = JSON.parse(ruleObj[options.key]).length;
} catch (e) {}
}
if (amountOfElements < Number(options.min)) {
thrower.throwRuntimeError(`Element '${options.path ?? ''}' has a minimum cardinality of ${options.min ?? ''}, got ${amountOfElements} instead`);
}
}
// const empty: boolean = await isEmpty(ruleObj);
// if (options?.min === 0) {
// if (empty) return baseObj;
// } else {
// let amountOfElements = 1;
// if (empty) {
// amountOfElements = 0;
// } else {
// try {
// amountOfElements = JSON.parse(ruleObj[options.key]).length;
// } catch (e) {}
// }
// // if (amountOfElements < Number(options.min)) {
// // thrower.throwRuntimeError(`Element '${options.path ?? ''}' has a minimum cardinality of ${options.min ?? ''}, got ${amountOfElements} instead`);
// // }
// }
// this function merges key:value into an existing object
// taking into account cardinality and children of primitives
const isBaseArray: boolean = typeof options?.baseMax !== 'undefined' && (options?.baseMax > '1' || options?.baseMax === '*');
Expand Down
18 changes: 15 additions & 3 deletions tests/root/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,19 @@ describe('integration tests', () => {
};

const res = await request(globalThis.app).post('/').send(requestBody);
expect(res.body.message).toBe('Transformation error: Element \'Patient.identifier[il-id].value\' has a minimum cardinality of 1, got 0 instead');
expect(res.body).toStrictEqual({
resourceType: 'Patient',
meta: {
profile: [
'http://fhir.health.gov.il/StructureDefinition/il-core-patient'
]
},
identifier: [
{
system: 'http://fhir.health.gov.il/identifier/il-national-id'
}
]
});
});

test('Case 3 - Slices with fixed values appear even if no children are set', async () => {
Expand Down Expand Up @@ -298,7 +310,7 @@ describe('integration tests', () => {
});
});

test('Case 8 - Throw runtime error if mandatory element is missing', async () => {
test.skip('Case 8 - Throw runtime error if mandatory element is missing', async () => {
const mapping = `
InstanceOf: il-core-patient
* identifier[il-id].value = '000000018'
Expand Down Expand Up @@ -432,7 +444,7 @@ describe('integration tests', () => {
});
});

test('Case 16 - Supply the full path of the element in the cardinality error', async () => {
test.skip('Case 16 - Supply the full path of the element in the cardinality error', async () => {
const mapping = `
InstanceOf: bp
* component[SystolicBP].valueQuantity
Expand Down

0 comments on commit d73118c

Please sign in to comment.