Skip to content

Commit 60e70fb

Browse files
committed
VED-350: generic validation for require system when code is present
1 parent be12721 commit 60e70fb

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

backend/src/models/fhir_immunization_pre_validators.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,6 @@ def pre_validate_route_coding_display(self, values: dict) -> dict:
799799
except (KeyError, IndexError):
800800
pass
801801

802-
# TODO: need to validate that doseQuantity.system is "http://unitsofmeasure.org"?
803-
# Check with Martin
804-
805802
def pre_validate_dose_quantity_value(self, values: dict) -> dict:
806803
"""
807804
Pre-validate that, if doseQuantity.value (legacy CSV field name: DOSE_AMOUNT) exists,
@@ -817,7 +814,28 @@ def pre_validate_dose_quantity_value(self, values: dict) -> dict:
817814
PreValidation.for_integer_or_decimal(field_value, "doseQuantity.value")
818815
except KeyError:
819816
pass
817+
818+
def pre_validate_dose_quantity_system_and_code(self, values: dict) -> dict:
819+
"""
820+
Pre-validate doseQuantity.code and doseQuantity.system:
821+
1. If code exists, it must be a non-empty string (legacy CSV: DOSE_UNIT_CODE).
822+
2. If system exists, it must be a non-empty string.
823+
3. If code exists, system MUST also exist (FHIR SimpleQuantity rule).
824+
"""
825+
field_value = values.get("doseQuantity", {})
826+
code = field_value.get("code")
827+
system = field_value.get("system")
820828

829+
if code is not None:
830+
PreValidation.for_string(code, "doseQuantity.code")
831+
832+
if system is not None:
833+
PreValidation.for_string(system, "doseQuantity.system")
834+
835+
PreValidation.require_system_when_code_present(
836+
code, system, "doseQuantity.code", "doseQuantity.system"
837+
)
838+
821839
def pre_validate_dose_quantity_code(self, values: dict) -> dict:
822840
"""
823841
Pre-validate that, if doseQuantity.code (legacy CSV field name: DOSE_UNIT_CODE) exists,

backend/src/models/utils/pre_validator_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ def for_integer_or_decimal(field_value: Union[int, Decimal], field_location: str
194194
or type(field_value) is Decimal # pylint: disable=unidiomatic-typecheck
195195
):
196196
raise TypeError(f"{field_location} must be a number")
197+
198+
@staticmethod
199+
def require_system_when_code_present(
200+
code_value:str,
201+
system_value:str
202+
):
203+
"""
204+
If code is present (non-empty), system must also be present (non-empty).
205+
"""
206+
if code_value is not None and system_value is None:
207+
raise ValueError(f"If {code_location} is present, {system_location} must also be present")
197208

198209
@staticmethod
199210
def for_unique_list(

0 commit comments

Comments
 (0)