Skip to content

Commit

Permalink
fix ValidatedString after validator
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Dec 4, 2024
1 parent bf3ebf2 commit 0b14f52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 7 additions & 5 deletions bioimageio/spec/_internal/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ class HttpUrl(RootHttpUrl):
root_model: ClassVar[Type[RootModel[Any]]] = RootModel[pydantic.HttpUrl]
_exists: Optional[bool] = None

@model_validator(mode="after")
def _validate(self):
url = self._validated
def _after_validator(self):
super()._after_validator()
context = validation_context_var.get()
if context.perform_io_checks and str(url) not in context.known_files:
self._validated = _validate_url(url)
if (
context.perform_io_checks
and str(self._validated) not in context.known_files
):
self._validated = _validate_url(self._validated)
self._exists = True

return self
Expand Down
5 changes: 5 additions & 0 deletions bioimageio/spec/_internal/validated_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CoreSchema,
no_info_after_validator_function,
)
from typing_extensions import Self


class ValidatedString(str):
Expand All @@ -18,6 +19,10 @@ class ValidatedString(str):
def __new__(cls, object: object):
self = super().__new__(cls, object)
self._validated = cls.root_model.model_validate(str(self)).root
return self._after_validator()

def _after_validator(self) -> Self:
"""add validation after the `root_model`"""
return self

@classmethod
Expand Down

0 comments on commit 0b14f52

Please sign in to comment.