11from datetime import date
22from typing import Any , Collection , Optional , Sequence
33
4- from pydantic import field_validator , model_validator , ValidationInfo
4+ from pydantic import ValidationInfo , field_validator , model_validator
55
6+ from mavedb .lib .validation import urn_re
67from mavedb .lib .validation .exceptions import ValidationError
78from mavedb .lib .validation .transform import (
89 transform_experiment_set_to_urn ,
9- transform_score_set_list_to_urn_list ,
1010 transform_record_publication_identifiers ,
11+ transform_score_set_list_to_urn_list ,
1112)
12- from mavedb .lib .validation import urn_re
1313from mavedb .lib .validation .utilities import is_null
1414from mavedb .view_models import record_type_validator , set_record_type
1515from mavedb .view_models .base .base import BaseModel
@@ -129,12 +129,11 @@ def publication_identifiers_validator(cls, v: Any, info: ValidationInfo) -> list
129129 return list (v ) # Re-cast into proper list-like type
130130
131131 # These 'synthetic' fields are generated from other model properties. Transform data from other properties as needed, setting
132- # the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created.
132+ # the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created. Only perform these
133+ # transformations if the relevant attributes are present on the input data (i.e., when creating from an ORM object).
133134 @model_validator (mode = "before" )
134135 def generate_primary_and_secondary_publications (cls , data : Any ):
135- if not hasattr (data , "primary_publication_identifiers" ) or not hasattr (
136- data , "secondary_publication_identifiers"
137- ):
136+ if hasattr (data , "publication_identifier_associations" ):
138137 try :
139138 publication_identifiers = transform_record_publication_identifiers (
140139 data .publication_identifier_associations
@@ -145,28 +144,30 @@ def generate_primary_and_secondary_publications(cls, data: Any):
145144 data .__setattr__ (
146145 "secondary_publication_identifiers" , publication_identifiers ["secondary_publication_identifiers" ]
147146 )
148- except AttributeError as exc :
147+ except ( KeyError , AttributeError ) as exc :
149148 raise ValidationError (
150- f"Unable to create { cls .__name__ } without attribute : { exc } ." # type: ignore
149+ f"Unable to coerce publication identifier attributes from ORM for { cls .__name__ } : { exc } ." # type: ignore
151150 )
152151 return data
153152
154153 @model_validator (mode = "before" )
155154 def generate_score_set_urn_list (cls , data : Any ):
156- if not hasattr (data , "score_set_urns " ):
155+ if hasattr (data , "score_sets " ):
157156 try :
158157 data .__setattr__ ("score_set_urns" , transform_score_set_list_to_urn_list (data .score_sets ))
159- except AttributeError as exc :
160- raise ValidationError (f"Unable to create { cls .__name__ } without attribute : { exc } ." ) # type: ignore
158+ except ( KeyError , AttributeError ) as exc :
159+ raise ValidationError (f"Unable to coerce associated score set URNs from ORM for { cls .__name__ } : { exc } ." ) # type: ignore
161160 return data
162161
163162 @model_validator (mode = "before" )
164163 def generate_experiment_set_urn (cls , data : Any ):
165- if not hasattr (data , "experiment_set_urn " ):
164+ if hasattr (data , "experiment_set " ):
166165 try :
167166 data .__setattr__ ("experiment_set_urn" , transform_experiment_set_to_urn (data .experiment_set ))
168- except AttributeError as exc :
169- raise ValidationError (f"Unable to create { cls .__name__ } without attribute: { exc } ." ) # type: ignore
167+ except (KeyError , AttributeError ) as exc :
168+ raise ValidationError (
169+ f"Unable to coerce associated experiment set URN from ORM for { cls .__name__ } : { exc } ."
170+ ) # type: ignore
170171 return data
171172
172173
0 commit comments