Skip to content

Commit 912bab4

Browse files
committed
birthdate validation
1 parent ca7a884 commit 912bab4

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

delta_backend/src/ConversionChecker.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Handles the transformation logic for each field based on the schema
33
# Root and base type expression checker functions
44
import ExceptionMessages
5-
from datetime import datetime,timedelta
5+
from datetime import datetime,timezone
66
from zoneinfo import ZoneInfo
77
import re
88
from LookUpData import LookUpData
@@ -104,9 +104,9 @@ def _log_error(self, fieldName, fieldValue, e, code=ExceptionMessages.RECORD_CHE
104104
"value": fieldValue,
105105
"message": message
106106
})
107-
108-
# Convert ISO date string to a specific format (e.g. YYYYMMDD)
107+
109108
def _convertToDate(self, expressionRule, fieldName, fieldValue, summarise, report_unexpected_exception):
109+
print("FIELD:", fieldName)
110110
if not fieldValue:
111111
return ""
112112

@@ -120,13 +120,24 @@ def _convertToDate(self, expressionRule, fieldName, fieldValue, summarise, repor
120120
if report_unexpected_exception:
121121
self._log_error(fieldName, fieldValue, "Partial date not accepted")
122122
return ""
123+
123124
try:
124125
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():
129+
if report_unexpected_exception:
130+
self._log_error(fieldName, fieldValue, "BirthDate cannot be in the future")
131+
return ""
132+
125133
format_str = expressionRule.replace("format:", "")
126134
return dt.strftime(format_str)
127-
except ValueError:
135+
136+
except ValueError as e:
128137
if report_unexpected_exception:
129-
return f"Unexpected format: {fieldValue}"
138+
self._log_error(fieldName, fieldValue, e)
139+
return ""
140+
130141

131142
# Convert FHIR datetime into CSV-safe UTC format
132143
def _convertToDateTime(self, expressionRule, fieldName, fieldValue, summarise, report_unexpected_exception):

0 commit comments

Comments
 (0)