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

Commit

Permalink
feat: make reference checking more restrictive and abide by FHIR (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmayda authored Feb 8, 2021
1 parent 9dba1bd commit ca3d574
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 334 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
'@typescript-eslint/no-useless-constructor': 'error',
'no-empty-function': 'off',
'@typescript-eslint/no-empty-function': 'error',
'import/no-extraneous-dependencies': ['error', { devDependencies: ['**/*.test.ts'] }],
'import/no-extraneous-dependencies': ['error', { devDependencies: ['**/*.test.ts', 'scripts/*.ts'] }],
},
settings: {
'import/resolver': {
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
!/.gitignore
/build
/node_modules
/scripts/*.json
# Ignore any zip files
*.zip

Expand Down
100 changes: 100 additions & 0 deletions scripts/generateResourceReferenceMatrixFile.ts
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);
1 change: 1 addition & 0 deletions src/schema/fhirResourceReferencesMatrix.v3.0.1.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/schema/fhirResourceReferencesMatrix.v4.0.1.json

Large diffs are not rendered by default.

Loading

0 comments on commit ca3d574

Please sign in to comment.