Skip to content

Commit cdcc3f3

Browse files
Merge pull request #35 from eccenca/feature/graphValidation-CMEM-7273
Add graph string validation and upgrade template
2 parents 781a0f5 + ebb7b0e commit cdcc3f3

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

cmem_plugin_base/dataintegration/parameter/graph.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Knowledge Graph Parameter Type."""
22

3+
import re
34
from typing import Any
45

56
from cmem.cmempy.dp.proxy.graph import get_graphs_list
@@ -8,6 +9,8 @@
89
from cmem_plugin_base.dataintegration.types import Autocompletion, StringParameterType
910
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
1011

12+
IRI_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:.+$")
13+
1114

1215
class GraphParameterType(StringParameterType):
1316
"""Knowledge Graph parameter type."""
@@ -34,6 +37,8 @@ def __init__(
3437
- if None -> defaults to di:Dataset, void:Dataset and shui:QueryCatalog
3538
:param allow_only_autocompleted_values: allow entering new graph URLs
3639
"""
40+
self.name = "scheme:string"
41+
self._validate_graph()
3742
self.show_di_graphs = show_di_graphs
3843
self.show_system_graphs = show_system_graphs
3944
self.show_graphs_without_class = show_graphs_without_class
@@ -91,3 +96,9 @@ def autocomplete(
9196
continue
9297
result.sort(key=lambda x: x.label) # type: ignore[return-value, arg-type]
9398
return list(set(result))
99+
100+
def _validate_graph(self) -> None:
101+
"""Verify that graph name is valid aka it has at least a scheme and something after it"""
102+
is_valid = bool(IRI_PATTERN.match(self.name))
103+
if not is_valid:
104+
raise ValueError(f"Could not validate graph IRI '{self.name}'")

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ keywords = [
2121
homepage = "https://github.com/eccenca/cmem-plugin-base"
2222

2323
[tool.poetry.dependencies]
24-
python = "^3.13"
25-
cmem-cmempy = ">=25.2.0"
24+
python = ">=3.13, ^3"
25+
cmem-cmempy = "^25.4.0"
2626
pydantic = "^2.12.2"
2727
python-ulid = "^3.1.0"
2828

29+
2930
[tool.poetry.group.dev.dependencies]
3031
deptry = "^0.24.0"
3132
genbadge = {extras = ["coverage"], version = "^1.1.3"}

tests/parameter_types/test_graph.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""graph parameter type tests"""
22

3+
import pytest
4+
35
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
46
from cmem_plugin_base.testing import TestPluginContext
57
from tests.utils import needs_cmem
@@ -28,3 +30,29 @@ def test_graph_parameter_type_completion() -> None:
2830
)
2931
== 0
3032
)
33+
34+
35+
def test_graph_validation() -> None:
36+
"""Test graph parameter string validation"""
37+
parameter = GraphParameterType(show_system_graphs=True)
38+
39+
parameter.name = "urn:ISBN:3-8273-7019-1"
40+
parameter._validate_graph() # noqa: SLF001
41+
42+
parameter.name = "http://test/data"
43+
parameter._validate_graph() # noqa: SLF001
44+
45+
parameter.name = "https://test/data"
46+
parameter._validate_graph() # noqa: SLF001
47+
48+
parameter.name = "test :test"
49+
with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"):
50+
parameter._validate_graph() # noqa: SLF001
51+
52+
parameter.name = ":ttt"
53+
with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"):
54+
parameter._validate_graph() # noqa: SLF001
55+
56+
parameter.name = ""
57+
with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"):
58+
parameter._validate_graph() # noqa: SLF001

0 commit comments

Comments
 (0)