From b2c2f161a4c9cea9cc43666f46262959c1b03369 Mon Sep 17 00:00:00 2001 From: kjefferson Date: Wed, 5 Jun 2024 15:29:23 -0400 Subject: [PATCH] Warn when reference values do not resolve and are not in correct format for a reference. --- src/fhirtypes/common.ts | 7 +++++++ test/export/InstanceExporter.test.ts | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/fhirtypes/common.ts b/src/fhirtypes/common.ts index fa3264849..e09a97cca 100644 --- a/src/fhirtypes/common.ts +++ b/src/fhirtypes/common.ts @@ -980,6 +980,13 @@ export function replaceReferences( (clone.value as FshReference).reference = value.reference.slice(0, firstPipe); clone = replaceReferences(clone, tank, fisher); } + // In the case of a user providing a reference to an entity that is unable to be resolved: log warning if + // the type of the reference cannot be resolved, there is no instance found for that reference value, and it has a reference field value + else if (type == null && !instance && value.reference) { + logger.warn( + `Cannot find the entity referenced at ${value.reference}. The provided reference value will be used, however, this reference does not conform to the FHIR Reference() format.` + ); + } } } else if (value instanceof FshCode) { // the version on a CodeSystem resource is not the same as the system's actual version out in the world. diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts index 3002d29ae..c05cfde49 100644 --- a/test/export/InstanceExporter.test.ts +++ b/test/export/InstanceExporter.test.ts @@ -5486,6 +5486,7 @@ describe('InstanceExporter', () => { expect(exported.managingOrganization).toEqual({ reference: 'Organization/org-id' }); + expect(loggerSpy.getAllMessages('warn')).toHaveLength(0); }); it('should assign a reference while resolving the Instance of a profile being referred to', () => { @@ -5533,6 +5534,23 @@ describe('InstanceExporter', () => { ]); }); + it('should log warning when reference values do not resolve and are not in correct format for a reference', () => { + // * target = Reference(exampleReferenceUnableToBeResolved) + const assignedRefRule = new AssignmentRule('target'); + assignedRefRule.value = new FshReference('exampleReferenceUnableToBeResolved'); + provenanceInstance.rules.push(assignedRefRule); + const exported = exportInstance(provenanceInstance); + expect(exported.target).toEqual([ + { + reference: 'exampleReferenceUnableToBeResolved' + } + ]); + expect(loggerSpy.getAllMessages('warn')).toHaveLength(2); + expect(loggerSpy.getLastMessage('warn')).toMatch( + 'Cannot find the entity referenced at exampleReferenceUnableToBeResolved. The provided reference value will be used, however, this reference does not conform to the FHIR Reference() format.' + ); + }); + it('should assign a reference leaving the full profile URL when it is specified', () => { const assignedRefRule = new AssignmentRule('target'); assignedRefRule.value = new FshReference( @@ -10190,7 +10208,7 @@ describe('InstanceExporter', () => { } ] }); - expect(loggerSpy.getAllLogs('warn')).toBeEmpty(); + expect(loggerSpy.getAllMessages('warn')).toHaveLength(1); expect(loggerSpy.getAllLogs('error')).toBeEmpty(); });