@@ -88,8 +88,40 @@ def validate_empty_fields(cls, values):
8888 Iteratively check every field in the model for emptiness.
8989 If a field is empty, add it to the error list with its full location.
9090 """
91- allowed_classes = [
91+ allowed_classes = cls .get_allowed_classes ()
92+ if cls .__name__ not in allowed_classes or not values :
93+ return values
94+
95+ stack = [(None , values )]
96+ empty_fields = []
97+
98+ while stack :
99+ path , current_value = stack .pop ()
100+
101+ if isinstance (current_value , dict ):
102+ cls .handle_dict (current_value , path , stack , empty_fields )
103+ elif isinstance (current_value , list ):
104+ cls .handle_list (current_value , path , stack , empty_fields )
105+ elif isinstance (current_value , Parent ):
106+ cls .handle_nested_model (current_value , path , stack )
107+ else :
108+ cls .handle_scalar (current_value , path , empty_fields )
109+
110+ if empty_fields :
111+ raise ValueError (
112+ f"The following fields are empty: { ', ' .join (empty_fields )} "
113+ )
114+
115+ return values
116+
117+ @staticmethod
118+ def get_allowed_classes ():
119+ """
120+ Return the list of allowed classes for validation.
121+ """
122+ return [
92123 "DocumentReference" ,
124+ "Meta" ,
93125 "Narrative" ,
94126 "Identifier" ,
95127 "NRLCodeableConcept" ,
@@ -107,57 +139,50 @@ def validate_empty_fields(cls, values):
107139 "DocumentReferenceContext" ,
108140 "Period" ,
109141 ]
110- if cls .__name__ not in allowed_classes or not values :
111- return values
112142
113- stack = [(None , values )]
114- empty_fields = []
115-
116- while stack :
117- path , current_value = stack .pop ()
118-
119- if isinstance (current_value , dict ):
120- for key , value in current_value .items ():
121- full_path = f"{ path } .{ key } " if path else key
122- if (
123- value is None
124- or value == ""
125- or (isinstance (value , list ) and not value )
126- ):
127- empty_fields .append (full_path )
128- else :
129- stack .append ((full_path , value ))
130- if not current_value :
131- empty_fields .append (path )
132-
133- elif isinstance (current_value , list ):
134- for index , item in enumerate (current_value ):
135- full_path = f"{ path } [{ index } ]" if path else f"[{ index } ]"
136- if (
137- item is None
138- or item == ""
139- or (isinstance (item , dict ) and not item )
140- ):
141- empty_fields .append (full_path )
142- else :
143- stack .append ((full_path , item ))
144-
145- elif isinstance (current_value , Parent ):
146- nested_values = current_value .model_dump (exclude_none = True )
147- for nested_field , nested_value in nested_values .items ():
148- full_path = f"{ path } .{ nested_field } " if path else nested_field
149- stack .append ((full_path , nested_value ))
143+ @staticmethod
144+ def handle_dict (current_value , path , stack , empty_fields ):
145+ """
146+ Handle validation for dictionary fields.
147+ """
148+ for key , value in current_value .items ():
149+ full_path = f"{ path } .{ key } " if path else key
150+ if value is None or value == "" or (isinstance (value , list ) and not value ):
151+ empty_fields .append (full_path )
152+ else :
153+ stack .append ((full_path , value ))
154+ if not current_value :
155+ empty_fields .append (path )
150156
157+ @staticmethod
158+ def handle_list (current_value , path , stack , empty_fields ):
159+ """
160+ Handle validation for list fields.
161+ """
162+ for index , item in enumerate (current_value ):
163+ full_path = f"{ path } [{ index } ]" if path else f"[{ index } ]"
164+ if item is None or item == "" or (isinstance (item , dict ) and not item ):
165+ empty_fields .append (full_path )
151166 else :
152- if current_value is None or current_value == "" :
153- empty_fields .append (path )
167+ stack .append ((full_path , item ))
154168
155- if empty_fields :
156- raise ValueError (
157- f"The following fields are empty: { ', ' .join (empty_fields )} "
158- )
169+ @staticmethod
170+ def handle_nested_model (current_value , path , stack ):
171+ """
172+ Handle validation for nested Pydantic models.
173+ """
174+ nested_values = current_value .model_dump (exclude_none = True )
175+ for nested_field , nested_value in nested_values .items ():
176+ full_path = f"{ path } .{ nested_field } " if path else nested_field
177+ stack .append ((full_path , nested_value ))
159178
160- return values
179+ @staticmethod
180+ def handle_scalar (current_value , path , empty_fields ):
181+ """
182+ Handle validation for scalar fields.
183+ """
184+ if current_value is None or current_value == "" :
185+ empty_fields .append (path )
161186
162187 model_config = ConfigDict (regex_engine = "python-re" , extra = "forbid" )
163188 extension : Annotated [
0 commit comments