@@ -311,12 +311,11 @@ class Config:
311311 arbitrary_types_allowed = True
312312
313313 # These 'synthetic' fields are generated from other model properties. Transform data from other properties as needed, setting
314- # the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created.
314+ # the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created. Only perform these
315+ # transformations if the relevant attributes are present on the input data (i.e., when creating from an ORM object).
315316 @model_validator (mode = "before" )
316317 def generate_primary_and_secondary_publications (cls , data : Any ):
317- if not hasattr (data , "primary_publication_identifiers" ) or not hasattr (
318- data , "secondary_publication_identifiers"
319- ):
318+ if hasattr (data , "publication_identifier_associations" ):
320319 try :
321320 publication_identifiers = transform_record_publication_identifiers (
322321 data .publication_identifier_associations
@@ -327,9 +326,9 @@ def generate_primary_and_secondary_publications(cls, data: Any):
327326 data .__setattr__ (
328327 "secondary_publication_identifiers" , publication_identifiers ["secondary_publication_identifiers" ]
329328 )
330- except AttributeError as exc :
329+ except ( AttributeError , KeyError ) as exc :
331330 raise ValidationError (
332- f"Unable to create { cls .__name__ } without attribute : { exc } ." # type: ignore
331+ f"Unable to coerce publication identifier attributes for { cls .__name__ } : { exc } ." # type: ignore
333332 )
334333 return data
335334
@@ -384,12 +383,11 @@ def publication_identifiers_validator(cls, value: Any) -> list[PublicationIdenti
384383 return list (value ) # Re-cast into proper list-like type
385384
386385 # These 'synthetic' fields are generated from other model properties. Transform data from other properties as needed, setting
387- # the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created.
386+ # the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created. Only perform these
387+ # transformations if the relevant attributes are present on the input data (i.e., when creating from an ORM object).
388388 @model_validator (mode = "before" )
389389 def generate_primary_and_secondary_publications (cls , data : Any ):
390- if not hasattr (data , "primary_publication_identifiers" ) or not hasattr (
391- data , "secondary_publication_identifiers"
392- ):
390+ if hasattr (data , "publication_identifier_associations" ):
393391 try :
394392 publication_identifiers = transform_record_publication_identifiers (
395393 data .publication_identifier_associations
@@ -400,33 +398,35 @@ def generate_primary_and_secondary_publications(cls, data: Any):
400398 data .__setattr__ (
401399 "secondary_publication_identifiers" , publication_identifiers ["secondary_publication_identifiers" ]
402400 )
403- except AttributeError as exc :
404- raise ValidationError (
405- f"Unable to create { cls .__name__ } without attribute: { exc } ." # type: ignore
406- )
401+ except (AttributeError , KeyError ) as exc :
402+ raise ValidationError (f"Unable to coerce publication identifier attributes for { cls .__name__ } : { exc } ." )
407403 return data
408404
409405 @model_validator (mode = "before" )
410406 def transform_meta_analysis_objects_to_urns (cls , data : Any ):
411- if not hasattr (data , "meta_analyzes_score_set_urns " ):
407+ if hasattr (data , "meta_analyzes_score_sets " ):
412408 try :
413409 data .__setattr__ (
414410 "meta_analyzes_score_set_urns" , transform_score_set_list_to_urn_list (data .meta_analyzes_score_sets )
415411 )
416- except AttributeError as exc :
417- raise ValidationError (f"Unable to create { cls .__name__ } without attribute: { exc } ." ) # type: ignore
412+ except (AttributeError , KeyError ) as exc :
413+ raise ValidationError (
414+ f"Unable to coerce meta analyzes score set urn attribute for { cls .__name__ } : { exc } ."
415+ )
418416 return data
419417
420418 @model_validator (mode = "before" )
421419 def transform_meta_analyzed_objects_to_urns (cls , data : Any ):
422- if not hasattr (data , "meta_analyzed_by_score_set_urns " ):
420+ if hasattr (data , "meta_analyzed_by_score_sets " ):
423421 try :
424422 data .__setattr__ (
425423 "meta_analyzed_by_score_set_urns" ,
426424 transform_score_set_list_to_urn_list (data .meta_analyzed_by_score_sets ),
427425 )
428- except AttributeError as exc :
429- raise ValidationError (f"Unable to create { cls .__name__ } without attribute: { exc } ." ) # type: ignore
426+ except (AttributeError , KeyError ) as exc :
427+ raise ValidationError (
428+ f"Unable to coerce meta analyzed by score set urn attribute for { cls .__name__ } : { exc } ."
429+ )
430430 return data
431431
432432
0 commit comments