Skip to content

Commit da4802c

Browse files
committed
NRL-502 validate format code for attachment types
1 parent 4d1220a commit da4802c

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

layer/nrlf/core/tests/test_validators.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,3 +1522,63 @@ def test_validate_ssp_content_with_multiple_asids():
15221522
"diagnostics": "Multiple ASID identifiers provided. Only a single valid ASID identifier can be provided in the context.related.",
15231523
"expression": ["context.related"],
15241524
}
1525+
1526+
1527+
def test_validate_content_format_invalid_code_for_unstructured_document():
1528+
validator = DocumentReferenceValidator()
1529+
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")
1530+
1531+
document_ref_data["content"][0]["format"] = {
1532+
"system": "https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode",
1533+
"code": "urn:nhs-ic:contact",
1534+
"display": "Contact details",
1535+
}
1536+
1537+
result = validator.validate(document_ref_data)
1538+
1539+
assert result.is_valid is False
1540+
assert result.resource.id == "Y05868-99999-99999-999999"
1541+
assert len(result.issues) == 1
1542+
assert result.issues[0].model_dump(exclude_none=True) == {
1543+
"severity": "error",
1544+
"code": "value",
1545+
"details": {
1546+
"coding": [
1547+
{
1548+
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
1549+
"code": "INVALID_RESOURCE",
1550+
"display": "Invalid validation of resource",
1551+
}
1552+
]
1553+
},
1554+
"diagnostics": "Invalid content format code: urn:nhs-ic:contact format code must be 'urn:nhs-ic:unstructured' for Unstructured Document attachments.",
1555+
"expression": ["content[0].format.code"],
1556+
}
1557+
1558+
1559+
def test_validate_content_format_invalid_code_for_contact_details():
1560+
validator = DocumentReferenceValidator()
1561+
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")
1562+
1563+
document_ref_data["content"][0]["attachment"]["contentType"] = "text/html"
1564+
1565+
result = validator.validate(document_ref_data)
1566+
1567+
assert result.is_valid is False
1568+
assert result.resource.id == "Y05868-99999-99999-999999"
1569+
assert len(result.issues) == 1
1570+
assert result.issues[0].model_dump(exclude_none=True) == {
1571+
"severity": "error",
1572+
"code": "value",
1573+
"details": {
1574+
"coding": [
1575+
{
1576+
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
1577+
"code": "INVALID_RESOURCE",
1578+
"display": "Invalid validation of resource",
1579+
}
1580+
]
1581+
},
1582+
"diagnostics": "Invalid content format code: urn:nhs-ic:unstructured format code must be 'urn:nhs-ic:record-contact' for Contact details attachments.",
1583+
"expression": ["content[0].format.code"],
1584+
}

layer/nrlf/core/validators.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def validate(self, data: Dict[str, Any] | DocumentReference):
141141
self._validate_category(resource)
142142
self._validate_author(resource)
143143
self._validate_type_category_mapping(resource)
144+
self._validate_content_format(resource)
144145
if resource.content[0].extension:
145146
self._validate_content_extension(resource)
146147

@@ -481,6 +482,35 @@ def _validate_type_category_mapping(self, model: DocumentReference):
481482
field="category.coding[0].code",
482483
)
483484

485+
def _validate_content_format(self, model: DocumentReference):
486+
"""
487+
Validate the content.format field contains an appropriate coding.
488+
"""
489+
logger.log(LogReference.VALIDATOR001, step="content_format")
490+
491+
logger.debug("Validating format")
492+
for i, content in enumerate(model.content):
493+
if (
494+
content.attachment.contentType == "text/html"
495+
and content.format.code != "urn:nhs-ic:record-contact"
496+
):
497+
self.result.add_error(
498+
issue_code="value",
499+
error_code="INVALID_RESOURCE",
500+
diagnostics=f"Invalid content format code: {content.format.code} format code must be 'urn:nhs-ic:record-contact' for Contact details attachments.",
501+
field=f"content[{i}].format.code",
502+
)
503+
elif (
504+
content.attachment.contentType == "application/pdf"
505+
and content.format.code != "urn:nhs-ic:unstructured"
506+
):
507+
self.result.add_error(
508+
issue_code="value",
509+
error_code="INVALID_RESOURCE",
510+
diagnostics=f"Invalid content format code: {content.format.code} format code must be 'urn:nhs-ic:unstructured' for Unstructured Document attachments.",
511+
field=f"content[{i}].format.code",
512+
)
513+
484514
def _validate_content_extension(self, model: DocumentReference):
485515
"""
486516
Validate the content.extension field contains an appropriate coding.

tests/features/producer/createDocumentReference-failure.feature

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,52 @@ Feature: Producer - createDocumentReference - Failure Scenarios
407407
}
408408
"""
409409

410+
Scenario: Invalid format code for attachment type
411+
Given the application 'DataShare' (ID 'z00z-y11y-x22x') is registered to access the API
412+
And the organisation 'X26' is authorised to access pointer types:
413+
| system | value |
414+
| http://snomed.info/sct | 1363501000000100 |
415+
| http://snomed.info/sct | 736253002 |
416+
When producer 'TSTCUS' requests creation of a DocumentReference with default test values except 'content' is:
417+
"""
418+
"content": [
419+
{
420+
"attachment": {
421+
"contentType": "application/pdf",
422+
"language": "en-US",
423+
"url": "https://spine-proxy.national.ncrs.nhs.uk/https%3A%2F%2Fp1.nhs.uk%2FMentalhealthCrisisPlanReport.pdf",
424+
"size": 3654,
425+
"hash": "2jmj7l5rSw0yVb/vlWAYkK/YBwk=",
426+
"title": "Mental health crisis plan report",
427+
"creation": "2022-12-21T10:45:41+11:00"
428+
},
429+
"format": {
430+
"system": "https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode",
431+
"code": "urn:nhs-ic:record-contact",
432+
"display": "Contact details"
433+
}
434+
"""
435+
Then the response status code is 400
436+
And the response is an OperationOutcome with 1 issue
437+
And the OperationOutcome contains the issue:
438+
"""
439+
{
440+
"severity": "error",
441+
"code": "value",
442+
"details": {
443+
"coding": [
444+
{
445+
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
446+
"code": "INVALID_RESOURCE",
447+
"display": "Invalid validation of resource",
448+
}
449+
]
450+
},
451+
"diagnostics": "Invalid content format code: urn:nhs-ic:unstructured format code must be 'urn:nhs-ic:record-contact' for Contact details attachments.",
452+
"expression": ["content[0].format.code"]
453+
}
454+
"""
455+
410456
# Invalid document reference - invalid Type
411457
# NRL-769 Known issue: Type display is not validated
412458
# Scenario: Invalid type (valid code but wrong display value)

0 commit comments

Comments
 (0)