File tree Expand file tree Collapse file tree
cmem_plugin_base/dataintegration/parameter Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11"""Knowledge Graph Parameter Type."""
22
3+ import re
34from typing import Any
45
56from cmem .cmempy .dp .proxy .graph import get_graphs_list
89from cmem_plugin_base .dataintegration .types import Autocompletion , StringParameterType
910from 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
1215class 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 } '" )
Original file line number Diff line number Diff line change @@ -21,11 +21,12 @@ keywords = [
2121homepage = " 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"
2626pydantic = " ^2.12.2"
2727python-ulid = " ^3.1.0"
2828
29+
2930[tool .poetry .group .dev .dependencies ]
3031deptry = " ^0.24.0"
3132genbadge = {extras = [" coverage" ], version = " ^1.1.3" }
Original file line number Diff line number Diff line change 11"""graph parameter type tests"""
22
3+ import pytest
4+
35from cmem_plugin_base .dataintegration .parameter .graph import GraphParameterType
46from cmem_plugin_base .testing import TestPluginContext
57from 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
You can’t perform that action at this time.
0 commit comments