diff --git a/kernelci/api/models.py b/kernelci/api/models.py index 6d2acc6ed2..a227c88cba 100644 --- a/kernelci/api/models.py +++ b/kernelci/api/models.py @@ -22,18 +22,20 @@ from pydantic import ( AnyHttpUrl, AnyUrl, - AfterValidator, BaseModel, + BeforeValidator, Field, - FileUrl, field_validator, StrictInt, + TypeAdapter, ) from .models_base import ( PyObjectId, DatabaseModel, ) +any_url_adapter = TypeAdapter(AnyUrl) +any_http_url_adapter = TypeAdapter(AnyHttpUrl) class StateValues(str, enum.Enum): """Enumeration to declare values to be used for Node.state""" @@ -117,7 +119,8 @@ class Revision(BaseModel): tree: str = Field( description="git tree of the revision" ) - url: Annotated[AnyUrl | FileUrl, AfterValidator(str)] = Field( + url: Annotated[str, BeforeValidator( + lambda value: str(any_url_adapter.validate_python(value)))] = Field( description="git URL of the revision" ) branch: str = Field( @@ -205,7 +208,8 @@ class Node(DatabaseModel): default=None, description="Result of node", ) - artifacts: Optional[Dict[str, Annotated[AnyHttpUrl, AfterValidator(str)]]] = Field( + artifacts: Optional[Dict[str, Annotated[str, BeforeValidator( + lambda value: str(any_http_url_adapter.validate_python(value)))]]] = Field( description="Artifacts associated with the node (binaries, logs...)", default=None ) @@ -506,7 +510,8 @@ class TestData(BaseModel): default=None ) # [TODO] Specify the source code file/function too? - test_source: Optional[Annotated[AnyUrl, AfterValidator(str)]] = Field( + test_source: Optional[Annotated[str, BeforeValidator( + lambda value: str(any_url_adapter.validate_python(value)))]] = Field( description="Repository containing the test source code", default=None )