File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -560,6 +560,17 @@ class Person(ContributorBaseModel):
560560
561561 # helper methods
562562
563+ @field_validator ("orcid" , mode = "before" )
564+ @classmethod
565+ def orcid_from_string (cls , orcid : str ) -> Optional [HttpUrlStr ]:
566+ """Convert orcid id string to HttpUrlStr."""
567+ # orcid regex without https://orcid.org/ prefix
568+ orcid_regex = r"^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$"
569+ if orcid is not None and isinstance (orcid , str ):
570+ if re .match (orcid_regex , orcid ):
571+ return f"https://orcid.org/{ orcid } "
572+ return orcid
573+
563574 @property
564575 def full_name (self ) -> str :
565576 """Return the full name of the person."""
Original file line number Diff line number Diff line change @@ -165,3 +165,20 @@ def test_custom_key_order():
165165
166166 # key order also preserved by copy
167167 assert p .model_copy ()._key_order == p ._key_order
168+
169+
170+ def test_orcid_from_string ():
171+ person_dict = {
172+ "given-names" : "Jane" ,
173+ "family-names" : "Doe" ,
174+ "email" : "mail@example.com" ,
175+ "orcid" : "0000-0003-2637-0432" ,
176+ }
177+ orcid_url = "https://orcid.org/0000-0003-2637-0432"
178+ p = Person (** person_dict )
179+ assert str (p .orcid ) == orcid_url
180+
181+ # with http:// prefix
182+ person_dict ["orcid" ] = orcid_url
183+ p = Person (** person_dict )
184+ assert str (p .orcid ) == orcid_url
You can’t perform that action at this time.
0 commit comments