diff --git a/cmem_plugin_base/dataintegration/parameter/graph.py b/cmem_plugin_base/dataintegration/parameter/graph.py index 8dd6575..b849c8a 100644 --- a/cmem_plugin_base/dataintegration/parameter/graph.py +++ b/cmem_plugin_base/dataintegration/parameter/graph.py @@ -1,5 +1,6 @@ """Knowledge Graph Parameter Type.""" +import re from typing import Any from cmem.cmempy.dp.proxy.graph import get_graphs_list @@ -8,6 +9,8 @@ from cmem_plugin_base.dataintegration.types import Autocompletion, StringParameterType from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access +IRI_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:.+$") + class GraphParameterType(StringParameterType): """Knowledge Graph parameter type.""" @@ -34,6 +37,8 @@ def __init__( - if None -> defaults to di:Dataset, void:Dataset and shui:QueryCatalog :param allow_only_autocompleted_values: allow entering new graph URLs """ + self.name = "scheme:string" + self._validate_graph() self.show_di_graphs = show_di_graphs self.show_system_graphs = show_system_graphs self.show_graphs_without_class = show_graphs_without_class @@ -91,3 +96,9 @@ def autocomplete( continue result.sort(key=lambda x: x.label) # type: ignore[return-value, arg-type] return list(set(result)) + + def _validate_graph(self) -> None: + """Verify that graph name is valid aka it has at least a scheme and something after it""" + is_valid = bool(IRI_PATTERN.match(self.name)) + if not is_valid: + raise ValueError(f"Could not validate graph IRI '{self.name}'") diff --git a/poetry.lock b/poetry.lock index 8e99d9b..2205fc8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1628,5 +1628,5 @@ zstd = ["backports-zstd (>=1.0.0) ; python_version < \"3.14\""] [metadata] lock-version = "2.1" -python-versions = "^3.13" -content-hash = "ad71c832dd08706907ae16d19feb22a8aeb39277e5fc0dc3f66a8819dccb9576" +python-versions = ">=3.13, ^3" +content-hash = "6d03f2e04417f3632167c17d1c930d62d1ee61b1512d80e79dc9cff291757777" diff --git a/pyproject.toml b/pyproject.toml index 39c5033..30980ca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,11 +21,12 @@ keywords = [ homepage = "https://github.com/eccenca/cmem-plugin-base" [tool.poetry.dependencies] -python = "^3.13" -cmem-cmempy = ">=25.2.0" +python = ">=3.13, ^3" +cmem-cmempy = "^25.4.0" pydantic = "^2.12.2" python-ulid = "^3.1.0" + [tool.poetry.group.dev.dependencies] deptry = "^0.24.0" genbadge = {extras = ["coverage"], version = "^1.1.3"} diff --git a/tests/parameter_types/test_graph.py b/tests/parameter_types/test_graph.py index 0a29bf9..5432957 100644 --- a/tests/parameter_types/test_graph.py +++ b/tests/parameter_types/test_graph.py @@ -1,5 +1,7 @@ """graph parameter type tests""" +import pytest + from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType from cmem_plugin_base.testing import TestPluginContext from tests.utils import needs_cmem @@ -28,3 +30,29 @@ def test_graph_parameter_type_completion() -> None: ) == 0 ) + + +def test_graph_validation() -> None: + """Test graph parameter string validation""" + parameter = GraphParameterType(show_system_graphs=True) + + parameter.name = "urn:ISBN:3-8273-7019-1" + parameter._validate_graph() # noqa: SLF001 + + parameter.name = "http://test/data" + parameter._validate_graph() # noqa: SLF001 + + parameter.name = "https://test/data" + parameter._validate_graph() # noqa: SLF001 + + parameter.name = "test :test" + with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"): + parameter._validate_graph() # noqa: SLF001 + + parameter.name = ":ttt" + with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"): + parameter._validate_graph() # noqa: SLF001 + + parameter.name = "" + with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"): + parameter._validate_graph() # noqa: SLF001