This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make reference checking more restrictive and abide by FHIR (#22)
- Loading branch information
Showing
9 changed files
with
730 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
!/.gitignore | ||
/build | ||
/node_modules | ||
/scripts/*.json | ||
# Ignore any zip files | ||
*.zip | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
This scripts generates the JSON files at src/schema. Before running the script download the JSON FHIR definition package and copy | ||
the profiles-resources.json file into this directory. | ||
It is recommended to install ts-node to execute .ts files in the command line | ||
> npm install -g ts-node | ||
You can download the latest FHIR definition from https://www.hl7.org/fhir/downloads.html or find older FHIR versions at http://hl7.org/fhir/directory.html | ||
Run the script: | ||
> cd to this current directory | ||
> ts-node generateResourceReferenceMatrixFile.ts <fhirVersion> | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
|
||
interface Type { | ||
code: string; | ||
targetProfile: string[]; | ||
} | ||
interface Element { | ||
id: string; | ||
type: Type[]; | ||
} | ||
|
||
interface PathMap { | ||
[sourceResourceType: string]: { [requestorResourceType: string]: string[] }; | ||
} | ||
|
||
const readProfileFile = (path: string): any[] => { | ||
const data = JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })); | ||
return data.entry.map((x: any) => x.resource); | ||
}; | ||
|
||
const compile = (resources: any[]) => { | ||
const filter = resources.filter( | ||
resource => resource.baseDefinition === 'http://hl7.org/fhir/StructureDefinition/DomainResource', | ||
); | ||
|
||
const pathMap: PathMap = {}; | ||
filter.forEach(resource => { | ||
return resource.snapshot.element | ||
.filter((element: Element) => !!element.type) | ||
.forEach((element: Element) => { | ||
return element.type | ||
.filter((type: Type) => type.code === 'Reference' && !!type.targetProfile) | ||
.forEach((type: Type) => { | ||
const sourceType = resource.type; | ||
const path = element.id.replace(`${resource.type}.`, '').replace('[x]', 'Reference'); | ||
// R3; targetProfile == string / R4; targetProfile == array | ||
let profiles = type.targetProfile; | ||
if (!Array.isArray(profiles)) { | ||
profiles = [profiles]; | ||
} | ||
profiles.forEach((target: string) => { | ||
const requestorType = target.replace('http://hl7.org/fhir/StructureDefinition/', ''); | ||
if (!pathMap[sourceType]) { | ||
pathMap[sourceType] = {}; | ||
} | ||
if (!pathMap[sourceType][requestorType]) { | ||
pathMap[sourceType][requestorType] = []; | ||
} | ||
pathMap[sourceType][requestorType].push(path); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
return pathMap; | ||
}; | ||
|
||
const run = async () => { | ||
const args = process.argv.slice(2); | ||
if (!args[0]) { | ||
console.log('Error. Missing fhirVersion parameter'); | ||
console.log('Usage: ts-node run.ts <fhirVersion>'); | ||
} | ||
const fhirVersion = args[0]; | ||
if (!fhirVersion.startsWith('4') || fhirVersion.startsWith('3')) { | ||
console.error('*******************************'); | ||
console.error('this script was only tested with base STU3 & R4 profiles'); | ||
console.error(`you are attempting to use ${fhirVersion} proceed with caution`); | ||
console.error('*******************************'); | ||
} | ||
console.log('reading file'); | ||
const resources = readProfileFile(`${fhirVersion}-profiles-resources.json`); | ||
console.log('compiling file'); | ||
const pathMap = compile(resources); | ||
console.log('writing compiled output'); | ||
fs.writeFileSync(`../src/schema/fhirResourceReferencesMatrix.v${fhirVersion}.json`, JSON.stringify(pathMap)); | ||
}; | ||
|
||
run() | ||
.then(console.log) | ||
.catch(console.error); |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.