Skip to content

Commit 1267876

Browse files
committed
recorded date validation
1 parent 912bab4 commit 1267876

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

delta_backend/src/ConversionChecker.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,37 @@ def _convertToDate(self, expressionRule, fieldName, fieldValue, summarise, repor
114114
if report_unexpected_exception:
115115
self._log_error(fieldName, fieldValue, "Value is not a string")
116116
return ""
117-
118-
# Reject partial dates like "2024" or "2024-05"
119-
if re.match(r"^\d{4}(-\d{2})?$", fieldValue):
117+
118+
# Normalize expression rule
119+
format_str = expressionRule.replace("format:", "").strip()
120+
121+
# Reject partial ISO dates like "2024" or "2024-05"
122+
if format_str == "%Y%m%d" and re.match(r"^\d{4}(-\d{2})?$", fieldValue):
120123
if report_unexpected_exception:
121124
self._log_error(fieldName, fieldValue, "Partial date not accepted")
122125
return ""
123126

124-
try:
125-
dt = datetime.fromisoformat(fieldValue)
126-
127-
# Reject future dates if the field is BirthDate
128-
if fieldName == "contained|#:Patient|birthDate" and dt.date() > datetime.now(timezone.utc).date():
127+
# Pre-process if expecting no delimiters
128+
if format_str == "%Y%m%d":
129+
fieldValue = fieldValue.replace("-", "").replace("/", "")
130+
# Validate expected raw input format if using %Y%m%d
131+
if not re.match(r"^\d{8}$", fieldValue):
129132
if report_unexpected_exception:
130-
self._log_error(fieldName, fieldValue, "BirthDate cannot be in the future")
133+
self._log_error(fieldName, fieldValue, "Date must be in YYYYMMDD format")
131134
return ""
132135

133-
format_str = expressionRule.replace("format:", "")
134-
return dt.strftime(format_str)
135136

137+
try:
138+
dt = datetime.strptime(fieldValue, format_str)
139+
140+
# Reject future dates if the field is BirthDate
141+
if fieldName in ["contained|#:Patient|birthDate", "recorded"]:
142+
if dt.date() > datetime.now(timezone.utc).date():
143+
if report_unexpected_exception:
144+
self._log_error(fieldName, fieldValue, "Date cannot be in the future")
145+
return ""
146+
147+
return dt.strftime(expressionRule)
136148
except ValueError as e:
137149
if report_unexpected_exception:
138150
self._log_error(fieldName, fieldValue, e)

delta_backend/src/ConversionLayout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
"expression": {
141141
"expressionName": "Date Convert",
142142
"expressionType": "DATECONVERT",
143-
"expressionRule": "format:%Y%m%d"
143+
"expressionRule": "%Y%m%d"
144144
}
145145
},
146146
{
@@ -221,7 +221,7 @@
221221
"expression": {
222222
"expressionName": "Date Convert",
223223
"expressionType": "DATECONVERT",
224-
"expressionRule": "format:%Y%m%d"
224+
"expressionRule": "%Y%m%d"
225225
}
226226
},
227227
{

delta_backend/tests/sample_data/fhir_sample.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"coding": [
4747
{
4848
"system": "http://snomed.info/sct",
49-
"code": "13246810000001tgit ",
49+
"code": "13246810000001",
5050
"display": "Administration of first dose of severe acute respiratory syndrome coronavirus 2 vaccine (procedure)"
5151
}
5252
]
@@ -73,7 +73,7 @@
7373
"reference": "#Pat1"
7474
},
7575
"occurrenceDateTime": "2021-02-07T13:28:17+00:00",
76-
"recorded": "2021-02-07T13:28:17+00:00",
76+
"recorded": "2025-04-06",
7777
"primarySource": true,
7878
"manufacturer": {
7979
"display": "AstraZeneca Ltd"

0 commit comments

Comments
 (0)