Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

Preserve falsy RDF literals in Python SHACL reports

Rudy Celekli7f8ea188bba0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6646495d..c364cfe5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- Preserve zero and false RDF literal values and focus nodes in Python SHACL violation reports instead of rendering them as missing fields.
+
- Keep the last active ontology intact when parsing a replacement source, cache, or refresh fails; publish successful replacements in one graph transaction.
- Bound wildcard repository filtering to pattern/filename pairs so repeated stars cannot stall `onto_repo_list`.
- Sanitize quotes and ASCII control bytes in mapped IRI components so valid structured data does not fail RDF ingestion.
diff --git a/python/src/open_ontologies_lite/shacl.py b/python/src/open_ontologies_lite/shacl.py
index 10a564d2..237c2d0b 100644
--- a/python/src/open_ontologies_lite/shacl.py
+++ b/python/src/open_ontologies_lite/shacl.py
@@ -94,6 +94,11 @@ def _focus_nodes(data_graph, shapes_graph) -> tuple[int, list[dict]]:
return total, unmatched
+def _term_text(term) -> str:
+ """A missing RDF term is empty; falsy literal values are still present."""
+ return "" if term is None else str(term)
+
+
def shacl_validate(
data: str,
shapes: str,
@@ -131,13 +136,13 @@ def shacl_validate(
severity = get(sh.resultSeverity)
violations.append(
{
- "focus_node": str(get(sh.focusNode) or ""),
- "path": str(get(sh.resultPath) or ""),
- "value": str(get(sh.value) or ""),
- "message": str(get(sh.resultMessage) or ""),
+ "focus_node": _term_text(get(sh.focusNode)),
+ "path": _term_text(get(sh.resultPath)),
+ "value": _term_text(get(sh.value)),
+ "message": _term_text(get(sh.resultMessage)),
"severity": _local(str(severity)) if severity else "Violation",
- "source_shape": str(get(sh.sourceShape) or ""),
- "constraint": _local(str(get(sh.sourceConstraintComponent) or "")),
+ "source_shape": _term_text(get(sh.sourceShape)),
+ "constraint": _local(_term_text(get(sh.sourceConstraintComponent))),
}
)
diff --git a/python/tests/test_shacl_falsy_terms.py b/python/tests/test_shacl_falsy_terms.py
new file mode 100644
index 00000000..fa49cb98
--- /dev/null
+++ b/python/tests/test_shacl_falsy_terms.py
@@ -0,0 +1,33 @@
+"""Report RDF literals by presence rather than Python truthiness."""
+import pytest
+pytest.importorskip("pyshacl")
+from open_ontologies_lite.shacl import shacl_validate
+
+PREFIX = "@prefix ex:<http://example.org/>. @prefix sh:<http://www.w3.org/ns/shacl#>. @prefix xsd:<http://www.w3.org/2001/XMLSchema#>. "
+
[email protected]("value, expected", [("0", "0"), ("false", "false"), ("1", "1"), ("true", "true")])
+def test_violation_retains_literal_value(value, expected):
+ report = shacl_validate(PREFIX + f"ex:n ex:p {value} .", PREFIX + "ex:S a sh:NodeShape; sh:targetNode ex:n; sh:property [sh:path ex:p; sh:datatype xsd:string].")
+ assert report["conforms"] is False
+ assert len(report["violations"]) == 1
+ violation = report["violations"][0]
+ assert violation["value"] == expected
+ assert violation["focus_node"] == "http://example.org/n"
+ assert violation["path"] == "http://example.org/p"
+
[email protected]("value, expected", [("0", "0"), ("false", "false"), ("1", "1"), ("true", "true")])
+def test_literal_focus_node_retains_lexical_value(value, expected):
+ report = shacl_validate("", PREFIX + f"ex:S a sh:NodeShape; sh:targetNode {value}; sh:datatype xsd:string.")
+ assert report["conforms"] is False
+ assert len(report["violations"]) == 1
+ assert report["violations"][0]["focus_node"] == expected
+ assert report["violations"][0]["value"] == expected
+
+def test_absent_violation_value_stays_empty():
+ report = shacl_validate("", PREFIX + "ex:S a sh:NodeShape; sh:targetNode ex:n; sh:property [sh:path ex:p; sh:minCount 1].")
+ assert report["conforms"] is False
+ assert report["violations"][0]["value"] == ""
+
+def test_valid_string_produces_no_violation():
+ report = shacl_validate(PREFIX + 'ex:n ex:p "hello".', PREFIX + "ex:S a sh:NodeShape; sh:targetNode ex:n; sh:property [sh:path ex:p; sh:datatype xsd:string].")
+ assert report["conforms"] is True and report["violations"] == []