1010
1111from dve .core_engine .backends .exceptions import MessageBearingError , ReaderLacksEntityTypeSupport
1212from dve .core_engine .backends .types import EntityName , EntityType
13+ from dve .core_engine .configuration .v1 import AllowedAdditionalChecks , _ReaderAdditionalChecksConfig
1314from dve .core_engine .message import FeedbackMessage
1415from dve .core_engine .type_hints import URI , ArbitraryFunction , WrapDecorator
1516from dve .parser .file_handling .service import open_stream
@@ -109,29 +110,58 @@ def read_to_entity_type(
109110 entity_name : EntityName ,
110111 schema : type [BaseModel ],
111112 all_model_fields : Optional [set [str ]] = None ,
112- ) -> EntityType :
113+ additional_checks : Optional [
114+ dict [AllowedAdditionalChecks , _ReaderAdditionalChecksConfig ]
115+ ] = None ,
116+ ):
113117 """Read to the specified entity type, if supported.
114118
115119 NOTE: Simple types should either be returned as strings (if present) or
116120 `None`. Format validation, casting, and parsing should be done in the
117121 data contract.
118122
119123 """
120- if entity_name == Iterator [dict [str , Any ]]:
121- return self .read_to_py_iterator (
124+ additional_checks = additional_checks or {}
125+
126+ self .raise_if_not_sensible_file (resource , entity_name )
127+
128+ if entity_type == Iterator [dict [str , Any ]]:
129+ entity = self .read_to_py_iterator (
122130 resource , entity_name , schema , all_model_fields # type: ignore
123131 )
124132
125- self . raise_if_not_sensible_file ( resource , entity_name )
133+ else :
126134
127- try :
128- reader_func = self .__read_methods__ [entity_type ]
129- except KeyError as err :
130- raise ReaderLacksEntityTypeSupport (entity_type = entity_type ) from err
135+ try :
136+ reader_func = self .__read_methods__ [entity_type ]
137+ except KeyError as err :
138+ raise ReaderLacksEntityTypeSupport (entity_type = entity_type ) from err
131139
132- return reader_func (
133- self , resource , entity_name , schema , all_model_fields = all_model_fields # type: ignore
134- )
140+ entity = reader_func (
141+ self ,
142+ resource ,
143+ entity_name ,
144+ schema ,
145+ all_model_fields = all_model_fields , # type: ignore
146+ )
147+
148+ if config := additional_checks .get ("check_empty" ):
149+ if self .check_entity_empty (entity ):
150+ raise MessageBearingError (
151+ f"The mandatory entity { entity_name } is empty" ,
152+ messages = [
153+ FeedbackMessage (
154+ entity = entity_name ,
155+ record = None ,
156+ failure_type = "submission" ,
157+ error_location = entity_name ,
158+ error_code = config .error_code ,
159+ error_message = config .error_message ,
160+ )
161+ ],
162+ )
163+
164+ return entity
135165
136166 def add_record_index (self , entity : EntityType , ** kwargs ) -> EntityType :
137167 """Add a record index to the entity"""
@@ -141,6 +171,10 @@ def drop_record_index(self, entity: EntityType, **kwargs) -> EntityType:
141171 """Drop a record index to the entity"""
142172 raise NotImplementedError (f"drop_record_index not implemented in { self .__class__ } " )
143173
174+ def check_entity_empty (self , entity : EntityType ) -> bool :
175+ """Determine if the entity supplied is empty"""
176+ raise NotImplementedError (f"check_entity_empty not implemented in { self .__class__ } " )
177+
144178 def write_parquet (
145179 self ,
146180 entity : EntityType ,
0 commit comments