@@ -83,7 +83,7 @@ def for_list(
8383 raise ValueError (f"{ field_location } must be an array of non-empty objects" )
8484
8585 @staticmethod
86- def for_date (field_value : str , field_location : str ):
86+ def for_date (field_value : str , field_location : str , future_date_allowed : bool = False ):
8787 """
8888 Apply pre-validation to a date field to ensure that it is a string (JSON dates must be
8989 written as strings) containing a valid date in the format "YYYY-MM-DD"
@@ -98,8 +98,9 @@ def for_date(field_value: str, field_location: str):
9898 f'{ field_location } must be a valid date string in the format "YYYY-MM-DD"'
9999 ) from value_error
100100
101- # Enforce not-in-the-future rule using central checker
102- PreValidation .check_if_future_date (parsed_date )
101+ # Enforce not-in-the-future rule using central checker (after successful parse)
102+ if not future_date_allowed and PreValidation .check_if_future_date (parsed_date ):
103+ raise ValueError (f"{ field_location } must not be in the future" )
103104
104105 @staticmethod
105106 def for_date_time (field_value : str , field_location : str , strict_timezone : bool = True ):
@@ -139,13 +140,15 @@ def for_date_time(field_value: str, field_location: str, strict_timezone: bool =
139140 for fmt in formats :
140141 try :
141142 fhir_date = datetime .strptime (field_value , fmt )
142-
143+ # After successful parse, enforce timezone and future-date rules
143144 if strict_timezone and fhir_date .tzinfo is not None :
144- if PreValidation .check_if_future_date (fhir_date ):
145- raise ValueError (error_message )
146- if not any (field_value .endswith (suffix ) for suffix in allowed_suffixes ):
147- raise ValueError (error_message )
148- # Enforce not-in-the-future rule using central checker
145+ if not any (field_value .endswith (suffix ) for suffix in allowed_suffixes ):
146+ raise ValueError (error_message )
147+
148+ # Enforce not-in-the-future rule using central checker (after successful parse)
149+ if PreValidation .check_if_future_date (fhir_date ):
150+ raise ValueError (f"{ field_location } must not be in the future" )
151+
149152 return fhir_date .isoformat ()
150153 except ValueError :
151154 continue
0 commit comments