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.

shacl: stop reporting a pass for constraints and targets we do not evaluate

Fabio Rovai5e5fc91219e4
diff --git a/src/shacl.rs b/src/shacl.rs
index ebae90fa..2ac37749 100644
--- a/src/shacl.rs
+++ b/src/shacl.rs
@@ -36,15 +36,47 @@ impl ShaclValidator {
&shapes_store,
r#"
PREFIX sh: <http://www.w3.org/ns/shacl#>
- SELECT ?shape ?targetClass WHERE {
- ?shape a sh:NodeShape ;
- sh:targetClass ?targetClass .
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
+ PREFIX owl: <http://www.w3.org/2002/07/owl#>
+ SELECT DISTINCT ?shape ?targetClass WHERE {
+ { ?shape a sh:NodeShape ; sh:targetClass ?targetClass . }
+ UNION
+ { ?shape a sh:NodeShape, rdfs:Class . BIND(?shape AS ?targetClass) }
+ UNION
+ { ?shape a sh:NodeShape, owl:Class . BIND(?shape AS ?targetClass) }
}
"#,
)?;
let mut violations: Vec<serde_json::Value> = Vec::new();
let mut skipped: Vec<serde_json::Value> = Vec::new();
+
+ // Target forms this implementation does not support. Previously a shapes
+ // graph using only these produced no shapes at all, the validation loop
+ // never ran, and the report said `conforms: true` having checked nothing.
+ // Recording them as skipped suppresses the verdict instead.
+ for (pred, label) in [
+ ("sh:targetNode", "sh:targetNode"),
+ ("sh:targetSubjectsOf", "sh:targetSubjectsOf"),
+ ("sh:targetObjectsOf", "sh:targetObjectsOf"),
+ ] {
+ let q = format!(
+ r#"PREFIX sh: <http://www.w3.org/ns/shacl#>
+ SELECT ?shape WHERE {{ ?shape {} ?t . }}"#,
+ pred
+ );
+ if let Ok(rows) = query_solutions(&shapes_store, &q) {
+ for row in &rows {
+ skipped.push(serde_json::json!({
+ "shape": strip_angle_brackets(
+ row.get("shape").map(|s| s.as_str()).unwrap_or_default()
+ ),
+ "constraint": label,
+ "reason": "target form not implemented; focus nodes could not be selected",
+ }));
+ }
+ }
+ }
let mut unmatched: Vec<serde_json::Value> = Vec::new();
let mut focus_nodes_total: u64 = 0;
@@ -98,6 +130,40 @@ impl ShaclValidator {
),
)?;
+ // Any constraint predicate on a property shape that this implementation
+ // does not evaluate must be reported, not ignored. Before this check,
+ // `sh:not` was invisible: it was never collected, never evaluated, and
+ // never recorded, so a shape whose only constraint was `sh:not` returned
+ // `conforms: true` over data that violated it.
+ let unknown = query_solutions(
+ &shapes_store,
+ &format!(
+ r#"
+ PREFIX sh: <http://www.w3.org/ns/shacl#>
+ SELECT DISTINCT ?pred WHERE {{
+ {} sh:property ?prop .
+ ?prop ?pred ?o .
+ FILTER(?pred NOT IN (
+ sh:path, sh:minCount, sh:maxCount, sh:datatype,
+ sh:pattern, sh:hasValue, sh:message, sh:severity,
+ sh:name, sh:description, sh:order, sh:group,
+ <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
+ ))
+ }}
+ "#,
+ shape_iri
+ ),
+ )?;
+ for row in &unknown {
+ if let Some(pred) = row.get("pred") {
+ skipped.push(serde_json::json!({
+ "shape": strip_angle_brackets(&shape_iri),
+ "constraint": strip_angle_brackets(pred),
+ "reason": "constraint not implemented; it was not evaluated",
+ }));
+ }
+ }
+
// 4. For each constraint, run SPARQL queries against the main graph
for prop in &props {
let raw_path = match prop.get("path") {
@@ -430,7 +496,18 @@ impl ShaclValidator {
}
}
- let nothing_matched = !shapes.is_empty() && focus_nodes_total == 0;
+ // A shapes graph that declared shapes we could not discover must not be
+ // reported as a pass. `shapes.is_empty()` used to fall through to
+ // `conforms: violations.is_empty()`, which is `true` for an empty run.
+ let declared_any_shape = query_solutions(
+ &shapes_store,
+ r#"PREFIX sh: <http://www.w3.org/ns/shacl#>
+ SELECT ?s WHERE { ?s a sh:NodeShape . }"#,
+ )
+ .map(|r| !r.is_empty())
+ .unwrap_or(false);
+ let nothing_matched =
+ (!shapes.is_empty() && focus_nodes_total == 0) || (shapes.is_empty() && declared_any_shape);
let mut report = serde_json::json!({
"violation_count": violations.len(),
diff --git a/tests/shacl_test.rs b/tests/shacl_test.rs
index c04ab7d4..eff5d8ff 100644
--- a/tests/shacl_test.rs
+++ b/tests/shacl_test.rs
@@ -243,3 +243,91 @@ fn test_shacl_unsupported_path_skipped_not_fatal() {
}));
assert!(parsed["skipped_constraints"].as_array().unwrap().len() == 1);
}
+
+// Regression tests for four false-clean defects found while validating the
+// Scotland land register ontology. In every case the validator previously
+// returned `conforms: true` over data that violated the shape, or over a shapes
+// graph it could not evaluate at all. A false clean is worse than an error,
+// because the caller has no signal that anything was missed.
+
+fn store_with(ttl: &str) -> Arc<GraphStore> {
+ let store = Arc::new(GraphStore::new());
+ store.load_turtle(ttl, None).unwrap();
+ store
+}
+
+const NOT_DATA: &str = r#"
+ @prefix ex: <http://example.org/> .
+ ex:a a ex:Thing ; ex:mech ex:bad .
+"#;
+
+#[test]
+fn test_sh_not_is_not_silently_ignored() {
+ // `sh:not` is not implemented. It must be reported as skipped, which
+ // suppresses the verdict, rather than passing silently.
+ let store = store_with(NOT_DATA);
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix ex: <http://example.org/> .
+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;
+ sh:property [ sh:path ex:mech ; sh:not [ sh:hasValue ex:bad ] ] .
+ "#;
+ let report = ShaclValidator::validate(&store, shapes).unwrap();
+ let v: serde_json::Value = serde_json::from_str(&report).unwrap();
+ assert!(v["conforms"].is_null(), "must not claim a pass: {v}");
+ assert!(
+ v["skipped_constraints"].as_array().map_or(false, |a| !a.is_empty()),
+ "sh:not must be recorded as skipped: {v}"
+ );
+}
+
+#[test]
+fn test_target_node_shape_does_not_report_a_pass() {
+ // `sh:targetNode` selects no focus nodes here, so the shapes graph yields
+ // nothing evaluable. Previously this reported conforms: true.
+ let store = store_with("@prefix ex: <http://example.org/> . ex:b a ex:Thing .");
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix ex: <http://example.org/> .
+ ex:S a sh:NodeShape ; sh:targetNode ex:b ;
+ sh:property [ sh:path ex:p ; sh:minCount 1 ] .
+ "#;
+ let report = ShaclValidator::validate(&store, shapes).unwrap();
+ let v: serde_json::Value = serde_json::from_str(&report).unwrap();
+ assert!(v["conforms"].is_null(), "must not claim a pass: {v}");
+}
+
+#[test]
+fn test_implicit_class_target_is_evaluated() {
+ // A node that is both sh:NodeShape and rdfs:Class targets its instances.
+ // This is now implemented, so it must find the real violation.
+ let store = store_with("@prefix ex: <http://example.org/> . ex:b a ex:Thing .");
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+ @prefix ex: <http://example.org/> .
+ ex:Thing a sh:NodeShape, rdfs:Class ;
+ sh:property [ sh:path ex:p ; sh:minCount 1 ] .
+ "#;
+ let report = ShaclValidator::validate(&store, shapes).unwrap();
+ let v: serde_json::Value = serde_json::from_str(&report).unwrap();
+ assert_eq!(v["conforms"], serde_json::Value::Bool(false), "report: {v}");
+ assert_eq!(v["violation_count"], 1, "report: {v}");
+}
+
+#[test]
+fn test_target_class_still_works() {
+ // Guard against the fixes above regressing the one target form that always
+ // worked correctly.
+ let store = store_with("@prefix ex: <http://example.org/> . ex:b a ex:Thing .");
+ let shapes = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix ex: <http://example.org/> .
+ ex:S a sh:NodeShape ; sh:targetClass ex:Thing ;
+ sh:property [ sh:path ex:p ; sh:minCount 1 ] .
+ "#;
+ let report = ShaclValidator::validate(&store, shapes).unwrap();
+ let v: serde_json::Value = serde_json::from_str(&report).unwrap();
+ assert_eq!(v["conforms"], serde_json::Value::Bool(false), "report: {v}");
+ assert_eq!(v["violation_count"], 1, "report: {v}");
+}