Skip to content

Commit

Permalink
Warn when reference values do not resolve and have incorrect format (#…
Browse files Browse the repository at this point in the history
…1468)

* Warn when reference values do not resolve and are not in correct format for a reference.

* additional check for fhir reference format, log when reference is not of type/id format or when too/many/parts

* update check for absolute url

* Removing conditional for logging reference value with more/than/two/parts since acceptable reference format

* update reference warn message, account for reference case of uuid or oid

* combining conditional statements
  • Loading branch information
KaelynJefferson committed Jun 27, 2024
1 parent ea80c60 commit 34105af
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/fhirtypes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,14 @@ export function replaceReferences<T extends AssignmentRule | CaretValueRule>(
clone = cloneDeep(rule);
(clone.value as FshReference).reference = value.reference.slice(0, firstPipe);
clone = replaceReferences(clone, tank, fisher);
} else if (type == null && id == null && value.reference) {
// if the reference to an entity is provided but is unable to be resolved
if (!(value.reference.startsWith('urn:') || value.reference.includes('/'))) {
logger.warn(
`Cannot find the entity referenced at ${value.reference}. The provided reference value will be used, but this reference does not conform to the FHIR Reference format.`,
rule.sourceInfo
);
}
}
}
} else if (value instanceof FshCode) {
Expand Down
79 changes: 78 additions & 1 deletion test/export/InstanceExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -5533,6 +5534,82 @@ describe('InstanceExporter', () => {
]);
});

it('should log warning when reference values do not resolve and is not an absolute or relative URL', () => {
// * 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(1);
expect(loggerSpy.getLastMessage('warn')).toMatch(
'Cannot find the entity referenced at exampleReferenceUnableToBeResolved. The provided reference value will be used, but this reference does not conform to the FHIR Reference format.'
);
});
it('should not log warning when reference values do not resolve and is a UUID or OID', () => {
// * target = Reference(urn:uuid:exampleReference)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference('urn:uuid:exampleReference');
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'urn:uuid:exampleReference'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should not log warning when reference values do not resolve and is a relative URL with correct number of parts', () => {
// * target = Reference(UnresolvableType/exampleReferenceUnableToBeResolved)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference(
'UnresolvableType/exampleReferenceUnableToBeResolved'
);
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'UnresolvableType/exampleReferenceUnableToBeResolved'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should not log warning when reference values do not resolve and is a relative URL but has more than two parts', () => {
// * target = Reference(More/Than/Two/Parts/exampleReferenceUnableToBeResolved)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference(
'More/Than/Two/Parts/exampleReferenceUnableToBeResolved'
);
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'More/Than/Two/Parts/exampleReferenceUnableToBeResolved'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should not log warning when reference values are an absolute URL', () => {
// * target = Reference(http://foo.org/fhir/Patient/abc)
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference('http://foo.org/fhir/Patient/abc');
provenanceInstance.rules.push(assignedRefRule);
const exported = exportInstance(provenanceInstance);
expect(exported.target).toEqual([
{
reference: 'http://foo.org/fhir/Patient/abc'
}
]);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
});

it('should assign a reference leaving the full profile URL when it is specified', () => {
const assignedRefRule = new AssignmentRule('target');
assignedRefRule.value = new FshReference(
Expand Down Expand Up @@ -10190,7 +10267,7 @@ describe('InstanceExporter', () => {
}
]
});
expect(loggerSpy.getAllLogs('warn')).toBeEmpty();
expect(loggerSpy.getAllMessages('warn')).toHaveLength(0);
expect(loggerSpy.getAllLogs('error')).toBeEmpty();
});

Expand Down

0 comments on commit 34105af

Please sign in to comment.