Skip to content

Commit dd89e49

Browse files
NRL-1050 Do not map type-category when one invalid, remove later mapping check
1 parent 0f9787c commit dd89e49

File tree

7 files changed

+37
-109
lines changed

7 files changed

+37
-109
lines changed

api/producer/createDocumentReference/create_document_reference.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from nrlf.core.constants import (
55
PERMISSION_AUDIT_DATES_FROM_PAYLOAD,
66
PERMISSION_SUPERSEDE_IGNORE_DELETE_FAIL,
7-
TYPE_CATEGORIES,
87
)
98
from nrlf.core.decorators import request_handler
109
from nrlf.core.dynamodb.repository import DocumentPointer, DocumentPointerRepository
@@ -91,19 +90,6 @@ def _check_permissions(
9190
expression="type.coding[0].code",
9291
)
9392

94-
type_category = TYPE_CATEGORIES.get(core_model.type)
95-
if type_category != core_model.category:
96-
logger.log(
97-
LogReference.PROCREATE005a,
98-
ods_code=metadata.ods_code,
99-
type=core_model.type,
100-
category=core_model.category,
101-
)
102-
return SpineErrorResponse.BAD_REQUEST(
103-
diagnostics=f"The Category code of the provided document '{core_model.category}' must match the allowed category for pointer type '{core_model.type}' with a category value of '{type_category}'",
104-
expression="category.coding[0].code",
105-
)
106-
10793

10894
def _get_document_ids_to_supersede(
10995
resource: DocumentReference,

api/producer/createDocumentReference/tests/test_create_document_reference.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -423,22 +423,7 @@ def test_create_document_reference_invalid_pointer_type():
423423
},
424424
"diagnostics": "Invalid type code: invalid Type must be a member of the England-NRLRecordType value set (https://fhir.nhs.uk/England/CodeSystem/England-NRLRecordType)",
425425
"expression": ["type.coding[0].code"],
426-
},
427-
{
428-
"severity": "error",
429-
"code": "value",
430-
"details": {
431-
"coding": [
432-
{
433-
"code": "INVALID_RESOURCE",
434-
"display": "Invalid validation of resource",
435-
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
436-
}
437-
]
438-
},
439-
"diagnostics": "type (http://snomed.info/sct|invalid) does not map to the category: http://snomed.info/sct|734163000",
440-
"expression": ["type.coding[0]"],
441-
},
426+
}
442427
],
443428
}
444429

@@ -520,12 +505,12 @@ def test_create_document_reference_invalid_category_type():
520505
"issue": [
521506
{
522507
"severity": "error",
523-
"code": "invalid",
508+
"code": "value",
524509
"details": {
525510
"coding": [
526511
{
527-
"code": "BAD_REQUEST",
528-
"display": "Bad request",
512+
"code": "INVALID_RESOURCE",
513+
"display": "Invalid validation of resource",
529514
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
530515
}
531516
]

api/producer/upsertDocumentReference/tests/test_upsert_document_reference.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ def test_upsert_document_reference_invalid_category_type():
222222
"issue": [
223223
{
224224
"severity": "error",
225-
"code": "invalid",
225+
"code": "value",
226226
"details": {
227227
"coding": [
228228
{
229-
"code": "BAD_REQUEST",
230-
"display": "Bad request",
229+
"code": "INVALID_RESOURCE",
230+
"display": "Invalid validation of resource",
231231
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
232232
}
233233
]
@@ -550,21 +550,6 @@ def test_upsert_document_reference_invalid_pointer_type():
550550
"diagnostics": "Invalid type code: invalid Type must be a member of the England-NRLRecordType value set (https://fhir.nhs.uk/England/CodeSystem/England-NRLRecordType)",
551551
"expression": ["type.coding[0].code"],
552552
},
553-
{
554-
"severity": "error",
555-
"code": "value",
556-
"details": {
557-
"coding": [
558-
{
559-
"code": "INVALID_RESOURCE",
560-
"display": "Invalid validation of resource",
561-
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
562-
}
563-
]
564-
},
565-
"diagnostics": "type (http://snomed.info/sct|invalid) does not map to the category: http://snomed.info/sct|734163000",
566-
"expression": ["type.coding[0]"],
567-
},
568553
],
569554
}
570555

api/producer/upsertDocumentReference/upsert_document_reference.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from nrlf.core.constants import (
33
PERMISSION_AUDIT_DATES_FROM_PAYLOAD,
44
PERMISSION_SUPERSEDE_IGNORE_DELETE_FAIL,
5-
TYPE_CATEGORIES,
65
)
76
from nrlf.core.decorators import request_handler
87
from nrlf.core.dynamodb.repository import DocumentPointer, DocumentPointerRepository
@@ -89,19 +88,6 @@ def _check_permissions(
8988
expression="type.coding[0].code",
9089
)
9190

92-
type_category = TYPE_CATEGORIES.get(core_model.type)
93-
if type_category != core_model.category:
94-
logger.log(
95-
LogReference.PROUPSERT005a,
96-
ods_code=metadata.ods_code,
97-
type=core_model.type,
98-
category=core_model.category,
99-
)
100-
return SpineErrorResponse.BAD_REQUEST(
101-
diagnostics=f"The Category code of the provided document '{core_model.category}' must match the allowed category for pointer type '{core_model.type}' with a category value of '{type_category}'",
102-
expression="category.coding[0].code",
103-
)
104-
10591

10692
def _get_document_ids_to_supersede(
10793
resource: DocumentReference,

layer/nrlf/core/log_references.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,7 @@ class LogReference(Enum):
221221
)
222222
PROCREATE005 = _Reference(
223223
"WARN", "Organisation is not allowed to create pointer type"
224-
) #
225-
PROCREATE005a = _Reference(
226-
"WARN",
227-
"Organisation is not allowed to create pointer type with incorrect category",
228-
) #
224+
)
229225
PROCREATE006 = _Reference("DEBUG", "Performing relatesTo validation on resource")
230226
PROCREATE007a = _Reference(
231227
"WARN", "RelatesTo validation failed - no target identifier value"

layer/nrlf/core/tests/test_validators.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,16 @@ def test_validate_category_coding_display_mismatch_observations():
459459
validator = DocumentReferenceValidator()
460460
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")
461461

462+
document_ref_data["type"] = {
463+
"coding": [
464+
{
465+
"system": "http://snomed.info/sct",
466+
"code": "1363501000000100",
467+
"display": "Royal College of Physicians NEWS2 (National Early Warning Score 2) chart",
468+
}
469+
]
470+
}
471+
462472
document_ref_data["category"][0] = {
463473
"coding": [
464474
{
@@ -613,15 +623,19 @@ def test_validate_type_coding_invalid_code():
613623

614624
document_ref_data["type"] = {
615625
"coding": [
616-
{"system": "http://snomed.info/sct", "code": "1234", "display": "Care plan"}
626+
{
627+
"system": "http://snomed.info/sct",
628+
"code": "1234",
629+
"display": "Mental health crisis plan",
630+
}
617631
]
618632
}
619633

620634
result = validator.validate(document_ref_data)
621635

622636
assert result.is_valid is False
623637
assert result.resource.id == "Y05868-99999-99999-999999"
624-
assert len(result.issues) == 2
638+
assert len(result.issues) == 1
625639
assert result.issues[0].model_dump(exclude_none=True) == {
626640
"severity": "error",
627641
"code": "value",
@@ -637,21 +651,6 @@ def test_validate_type_coding_invalid_code():
637651
"diagnostics": "Invalid type code: 1234 Type must be a member of the England-NRLRecordType value set (https://fhir.nhs.uk/England/CodeSystem/England-NRLRecordType)",
638652
"expression": ["type.coding[0].code"],
639653
}
640-
assert result.issues[1].model_dump(exclude_none=True) == {
641-
"severity": "error",
642-
"code": "value",
643-
"details": {
644-
"coding": [
645-
{
646-
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
647-
"code": "INVALID_RESOURCE",
648-
"display": "Invalid validation of resource",
649-
}
650-
]
651-
},
652-
"diagnostics": "type (http://snomed.info/sct|1234) does not map to the category: http://snomed.info/sct|734163000",
653-
"expression": ["type.coding[0]"],
654-
}
655654

656655

657656
def test_validate_type_coding_multiple_codings():
@@ -708,8 +707,8 @@ def test_validate_type_coding_invalid_system():
708707
"coding": [
709708
{
710709
"system": "http://snoooooomed/sctfffffg",
711-
"code": "734163000",
712-
"display": "Care plan",
710+
"code": "736253002",
711+
"display": "Mental health crisis plan",
713712
}
714713
]
715714
}
@@ -718,7 +717,7 @@ def test_validate_type_coding_invalid_system():
718717

719718
assert result.is_valid is False
720719
assert result.resource.id == "Y05868-99999-99999-999999"
721-
assert len(result.issues) == 2
720+
assert len(result.issues) == 1
722721
assert result.issues[0].model_dump(exclude_none=True) == {
723722
"severity": "error",
724723
"code": "value",
@@ -734,21 +733,6 @@ def test_validate_type_coding_invalid_system():
734733
"diagnostics": "Invalid type system: http://snoooooomed/sctfffffg Type system must be either 'http://snomed.info/sct' or 'https://nicip.nhs.uk'",
735734
"expression": ["type.coding[0].system"],
736735
}
737-
assert result.issues[1].model_dump(exclude_none=True) == {
738-
"severity": "error",
739-
"code": "value",
740-
"details": {
741-
"coding": [
742-
{
743-
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
744-
"code": "INVALID_RESOURCE",
745-
"display": "Invalid validation of resource",
746-
}
747-
]
748-
},
749-
"diagnostics": "type (http://snoooooomed/sctfffffg|734163000) does not map to the category: http://snomed.info/sct|734163000",
750-
"expression": ["type.coding[0]"],
751-
}
752736

753737

754738
@pytest.mark.parametrize(

layer/nrlf/core/validators.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
REQUIRED_CREATE_FIELDS,
1111
TYPE_ATTRIBUTES,
1212
TYPE_CATEGORIES,
13+
Categories,
14+
PointerTypes,
1315
)
1416
from nrlf.core.errors import ParseError
1517
from nrlf.core.logger import LogReference, logger
@@ -445,7 +447,7 @@ def _validate_category(self, model: DocumentReference):
445447

446448
def _validate_type_category_mapping(self, model: DocumentReference):
447449
"""
448-
Validate the type field contains an appropriate coding system, code and display.
450+
Validate the type field matches the expected category
449451
"""
450452
logger.log(LogReference.VALIDATOR001, step="type_category_mapping")
451453

@@ -454,12 +456,16 @@ def _validate_type_category_mapping(self, model: DocumentReference):
454456
category_coding = model.category[0].coding[0]
455457
category_id = f"{category_coding.system}|{category_coding.code}"
456458

457-
if not TYPE_CATEGORIES.get(type_id):
459+
if type_id not in PointerTypes.list() or category_id not in Categories.list():
460+
return # No point mapping to an unexisting/unsupported type/category
461+
462+
type_category = TYPE_CATEGORIES.get(type_id)
463+
if type_category != category_id:
458464
self.result.add_error(
459465
issue_code="value",
460466
error_code="INVALID_RESOURCE",
461-
diagnostics=f"type ({type_id}) does not map to the category: {category_id}",
462-
field=f"type.coding[0]",
467+
diagnostics=f"The Category code of the provided document '{category_id}' must match the allowed category for pointer type '{type_id}' with a category value of '{type_category}'",
468+
field=f"category.coding[0].code",
463469
)
464470

465471
def _validate_content_extension(self, model: DocumentReference):

0 commit comments

Comments
 (0)