Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
fix: update IG validation to reject reference params with no target (#…
Browse files Browse the repository at this point in the history
…123)

* fix: update validation to reject reference params with no target

* separate validation from parsing and add test for empty target array
  • Loading branch information
ssvegaraju authored Sep 23, 2021
1 parent ea0d68c commit f789799
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/implementationGuides/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ Array [
],
"description": "test",
"name": "source-reference",
"target": undefined,
"target": Array [
"ValueSet",
],
"type": "reference",
"url": "http://hl7.org/fhir/SearchParameter/Consent-source-reference",
},
Expand Down
35 changes: 35 additions & 0 deletions src/implementationGuides/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('compile', () => {
base: ['Consent'],
expression: 'Consent.source',
xpath: 'f:Consent/f:sourceAttachment | f:Consent/f:sourceReference',
target: ['ValueSet'],
},
]);
await expect(compiled).resolves.toMatchSnapshot();
Expand Down Expand Up @@ -220,4 +221,38 @@ describe('compile', () => {
]);
await expect(compiled).rejects.toThrowError();
});
test(`search param of type reference with no target`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/ConceptMap-source-uri',
name: 'source-uri',
code: 'source-uri',
type: 'reference',
description: 'The source value set that contains the concepts that are being mapped',
base: ['ConceptMap'],
expression: '(ConceptMap.source as uri)',
xpath: 'f:ConceptMap/f:sourceUri',
},
]);
await expect(compiled).rejects.toThrowError();
});

test(`search param of type reference with empty target`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/ConceptMap-source-uri',
name: 'source-uri',
code: 'source-uri',
type: 'reference',
description: 'The source value set that contains the concepts that are being mapped',
base: ['ConceptMap'],
expression: '(ConceptMap.source as uri)',
xpath: 'f:ConceptMap/f:sourceUri',
target: [],
},
]);
await expect(compiled).rejects.toThrowError();
});
});
16 changes: 16 additions & 0 deletions src/implementationGuides/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ const isFhirSearchParam = (x: any): x is FhirSearchParam => {
);
};

// validates any semantic necessities of FHIR Search Parameters
const validateSearchParam = (param: FhirSearchParam) => {
if (param.type === 'reference') {
if (!param.target || param.target?.length === 0) {
throw new Error(
`Search Parameter of type reference must have a specified target. Error in ${JSON.stringify(
param,
null,
2,
)}`,
);
}
}
};

const UNSUPPORTED_SEARCH_PARAMS = [
'http://hl7.org/fhir/SearchParameter/Bundle-composition', // Uses "Bundle.entry[0]". We have no way of searching the nth element of an array
'http://hl7.org/fhir/SearchParameter/Bundle-message', // Uses "Bundle.entry[0]". We have no way of searching the nth element of an array
Expand Down Expand Up @@ -102,6 +117,7 @@ const compile = async (searchParams: any[]): Promise<any> => {
const validFhirSearchParams: FhirSearchParam[] = [];
searchParams.forEach(s => {
if (isFhirSearchParam(s)) {
validateSearchParam(s);
validFhirSearchParams.push(s);
} else {
throw new Error(`The following input is not a search parameter: ${JSON.stringify(s, null, 2)}`);
Expand Down

0 comments on commit f789799

Please sign in to comment.