Skip to content

Commit 2cd581e

Browse files
committed
use model_validator mode=before for MappableConcept
mode=after constructs the full object before validating, which is wasteful when we just need to check if name or primaryCoding exists. switched to mode=before so it checks the raw dict directly. also removes the unused Self import. Fixes #618
1 parent b573565 commit 2cd581e

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/ga4gh/core/models.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
StringConstraints,
1515
model_validator,
1616
)
17-
from typing_extensions import Self
1817

1918
from ga4gh.core.identifiers import GA4GH_IR_REGEXP
2019

@@ -226,13 +225,15 @@ class MappableConcept(Element, BaseModelForbidExtra):
226225
description="A list of mappings to concepts in terminologies or code systems. Each mapping should include a coding and a relation.",
227226
)
228227

229-
@model_validator(mode="after")
230-
def require_name_or_primary_coding(self) -> Self:
228+
@model_validator(mode="before")
229+
@classmethod
230+
def require_name_or_primary_coding(cls, values: Any) -> Any:
231231
"""Ensure that ``name`` or ``primaryCoding`` is provided"""
232-
if self.primaryCoding is None and self.name is None:
233-
err_msg = "One of `name` or `primaryCoding` must be provided."
234-
raise ValueError(err_msg)
235-
return self
232+
if isinstance(values, dict):
233+
if values.get("primaryCoding") is None and values.get("name") is None:
234+
err_msg = "One of `name` or `primaryCoding` must be provided."
235+
raise ValueError(err_msg)
236+
return values
236237

237238

238239
Element.model_rebuild()

0 commit comments

Comments
 (0)