Skip to content

Commit f0554c1

Browse files
eesa456axelkrastek1-nhs
authored andcommitted
NRL-820 validate the type has the correct category code
1 parent 7aff483 commit f0554c1

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

layer/nrlf/core/tests/test_validators.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,42 @@ def test_validate_category_coding_display_mismatch_observations():
485485
}
486486

487487

488+
def test_validate_category_coding_and_type_mismatch():
489+
validator = DocumentReferenceValidator()
490+
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")
491+
492+
document_ref_data["category"][0] = {
493+
"coding": [
494+
{
495+
"system": "http://snomed.info/sct",
496+
"code": "1102421000000108",
497+
"display": "Observations",
498+
}
499+
]
500+
}
501+
502+
result = validator.validate(document_ref_data)
503+
504+
assert result.is_valid is False
505+
assert result.resource.id == "Y05868-99999-99999-999999"
506+
assert len(result.issues) == 1
507+
assert result.issues[0].dict(exclude_none=True) == {
508+
"severity": "error",
509+
"code": "value",
510+
"details": {
511+
"coding": [
512+
{
513+
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
514+
"code": "INVALID_RESOURCE",
515+
"display": "Invalid validation of resource",
516+
}
517+
]
518+
},
519+
"diagnostics": "category code '1102421000000108' must match the allowed category for pointer type 736253002 with a category value of '734163000'",
520+
"expression": ["category[0].coding[0].code"],
521+
}
522+
523+
488524
def test_validate_category_coding_invalid_code():
489525
validator = DocumentReferenceValidator()
490526
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")

layer/nrlf/core/validators.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pydantic import ValidationError
66

77
from nrlf.core.codes import SpineErrorConcept
8-
from nrlf.core.constants import CATEGORIES, REQUIRED_CREATE_FIELDS
8+
from nrlf.core.constants import CATEGORIES, REQUIRED_CREATE_FIELDS, TYPE_CATEGORIES
99
from nrlf.core.errors import ParseError
1010
from nrlf.core.logger import LogReference, logger
1111
from nrlf.core.types import DocumentReference, OperationOutcomeIssue, RequestQueryType
@@ -390,6 +390,19 @@ def _validate_category(self, model: DocumentReference):
390390
diagnostics=f"category code '{coding.code}' must have a display value of '{CATEGORIES.get(coding.code)}'",
391391
field=f"category[0].coding[{0}].display",
392392
)
393+
return
394+
395+
pointer_type = model.type.coding[0].code
396+
type_category = TYPE_CATEGORIES[f"http://snomed.info/sct|{pointer_type}"].split(
397+
"|"
398+
)[1]
399+
if type_category != coding.code:
400+
self.result.add_error(
401+
issue_code="value",
402+
error_code="INVALID_RESOURCE",
403+
diagnostics=f"category code '{coding.code}' must match the allowed category for pointer type {pointer_type} with a category value of '{type_category}'",
404+
field=f"category[0].coding[{0}].code",
405+
)
393406

394407
def _validate_content_extension(self, model: DocumentReference):
395408
"""

0 commit comments

Comments
 (0)