Skip to content

Commit 3b6144d

Browse files
committed
somesy metadata orcid id number is acceptable
1 parent 3a51cb4 commit 3b6144d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/somesy/core/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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."""

tests/processing/test_core_models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)