Skip to content

Commit ddcda77

Browse files
NRL-1050 Do not map type-category when one invalid, remove later mapping check
1 parent 23b7327 commit ddcda77

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
@@ -438,22 +438,7 @@ def test_create_document_reference_invalid_pointer_type():
438438
},
439439
"diagnostics": "Invalid type code: invalid Type must be a member of the England-NRLRecordType value set (https://fhir.nhs.uk/England/CodeSystem/England-NRLRecordType)",
440440
"expression": ["type.coding[0].code"],
441-
},
442-
{
443-
"severity": "error",
444-
"code": "value",
445-
"details": {
446-
"coding": [
447-
{
448-
"code": "INVALID_RESOURCE",
449-
"display": "Invalid validation of resource",
450-
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
451-
}
452-
]
453-
},
454-
"diagnostics": "type (http://snomed.info/sct|invalid) does not map to the category: http://snomed.info/sct|734163000",
455-
"expression": ["type.coding[0]"],
456-
},
441+
}
457442
],
458443
}
459444

@@ -535,12 +520,12 @@ def test_create_document_reference_invalid_category_type():
535520
"issue": [
536521
{
537522
"severity": "error",
538-
"code": "invalid",
523+
"code": "value",
539524
"details": {
540525
"coding": [
541526
{
542-
"code": "BAD_REQUEST",
543-
"display": "Bad request",
527+
"code": "INVALID_RESOURCE",
528+
"display": "Invalid validation of resource",
544529
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
545530
}
546531
]

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
]
@@ -565,21 +565,6 @@ def test_upsert_document_reference_invalid_pointer_type():
565565
"diagnostics": "Invalid type code: invalid Type must be a member of the England-NRLRecordType value set (https://fhir.nhs.uk/England/CodeSystem/England-NRLRecordType)",
566566
"expression": ["type.coding[0].code"],
567567
},
568-
{
569-
"severity": "error",
570-
"code": "value",
571-
"details": {
572-
"coding": [
573-
{
574-
"code": "INVALID_RESOURCE",
575-
"display": "Invalid validation of resource",
576-
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
577-
}
578-
]
579-
},
580-
"diagnostics": "type (http://snomed.info/sct|invalid) does not map to the category: http://snomed.info/sct|734163000",
581-
"expression": ["type.coding[0]"],
582-
},
583568
],
584569
}
585570

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
@@ -460,6 +460,16 @@ def test_validate_category_coding_display_mismatch_observations():
460460
validator = DocumentReferenceValidator()
461461
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")
462462

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

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

621635
result = validator.validate(document_ref_data)
622636

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

657656

658657
def test_validate_type_coding_multiple_codings():
@@ -709,8 +708,8 @@ def test_validate_type_coding_invalid_system():
709708
"coding": [
710709
{
711710
"system": "http://snoooooomed/sctfffffg",
712-
"code": "734163000",
713-
"display": "Care plan",
711+
"code": "736253002",
712+
"display": "Mental health crisis plan",
714713
}
715714
]
716715
}
@@ -719,7 +718,7 @@ def test_validate_type_coding_invalid_system():
719718

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

754738

755739
@pytest.mark.parametrize(

layer/nrlf/core/validators.py

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

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

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

459-
if not TYPE_CATEGORIES.get(type_id):
461+
if type_id not in PointerTypes.list() or category_id not in Categories.list():
462+
return # No point mapping to an unexisting/unsupported type/category
463+
464+
type_category = TYPE_CATEGORIES.get(type_id)
465+
if type_category != category_id:
460466
self.result.add_error(
461467
issue_code="value",
462468
error_code="INVALID_RESOURCE",
463-
diagnostics=f"type ({type_id}) does not map to the category: {category_id}",
464-
field=f"type.coding[0]",
469+
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}'",
470+
field=f"category.coding[0].code",
465471
)
466472

467473
def _validate_content_extension(self, model: DocumentReference):

0 commit comments

Comments
 (0)