From d3a1231cb24edc04f1b64c9856af97415d0ea9fe Mon Sep 17 00:00:00 2001 From: Charles Tapley Hoyt Date: Mon, 4 Nov 2024 11:33:02 +0100 Subject: [PATCH] Improve model validation --- src/pyobo/struct/reference.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pyobo/struct/reference.py b/src/pyobo/struct/reference.py index e0229838..746b8b9b 100644 --- a/src/pyobo/struct/reference.py +++ b/src/pyobo/struct/reference.py @@ -45,13 +45,13 @@ def validate_identifier(cls, values): # noqa prefix, identifier = values.get("prefix"), values.get("identifier") if not prefix or not identifier: return values - norm_prefix = bioregistry.normalize_prefix(prefix) - if norm_prefix is None: + resource = bioregistry.get_resource(prefix) + if resource is None: raise ExpansionError(f"Unknown prefix: {prefix}") - values["prefix"] = norm_prefix - values["identifier"] = bioregistry.standardize_identifier(norm_prefix, identifier).strip() - # if not bioregistry.is_valid_identifier(norm_prefix, values["identifier"]): - # raise ValueError(f"non-standard identifier: {norm_prefix}:{norm_identifier}") + values["prefix"] = resource.prefix + values["identifier"] = resource.standardize_identifier(identifier) + # if not resource.is_valid_identifier(values["identifier"]): + # raise ValueError(f"non-standard identifier: {resource.prefix}:{values['identifier']}") return values @classmethod @@ -60,7 +60,7 @@ def auto(cls, prefix: str, identifier: str) -> "Reference": from ..api import get_name name = get_name(prefix, identifier) - return cls(prefix=prefix, identifier=identifier, name=name) + return cls.model_validate({"prefix": prefix, "identifier": identifier, "name": name}) @property def bioregistry_link(self) -> str: @@ -116,7 +116,7 @@ def _materialize( return None if name is None and auto: return cls.auto(prefix=prefix, identifier=identifier) - return cls(prefix=prefix, identifier=identifier, name=name) + return cls.model_validate({"prefix": prefix, "identifier": identifier, "name": name}) @property def _escaped_identifier(self):