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.

A datatype the store cannot tell apart is refused inside sh:or, sh:not and sh:qualifiedValueShape, as it already was on its own

FABIOTESS1b1b9a765b8d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5a4c59f6..9f39e225 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -46,6 +46,16 @@ All notable changes to Open Ontologies are documented here.
### Fixed
+- **A datatype the store cannot tell apart is refused inside `sh:or`,
+ `sh:not` and `sh:qualifiedValueShape`, as it already was on its own.** The
+ store widens xsd:int, xsd:nonNegativeInteger and the other integer-derived
+ types (and xsd:dateTimeStamp) to the type they derive from, and the direct
+ `sh:datatype` constraint refuses to decide them. A member naming one did
+ not: `sh:or` and a qualified minimum reported every conforming value, and
+ `sh:not [ sh:datatype xsd:nonNegativeInteger ]` returned `conforms: true`
+ over values of exactly that type. Each now records the constraint in
+ `skipped_constraints` with the direct path's reason and suppresses the
+ verdict. W3C columns unchanged (61, 53).
- **A custom `sh:severity` keeps its namespace in the JSON report** (#334).
Every path set `severity` to the text after the last `#` of the declared
IRI, and every reader takes a bare name as `sh:`, so W3C
diff --git a/src/shacl.rs b/src/shacl.rs
index 5101b805..34802063 100644
--- a/src/shacl.rs
+++ b/src/shacl.rs
@@ -796,6 +796,10 @@ impl ShaclValidator {
// and the run came back CLEAN. Same defect in sh:in and sh:not.
let mut or_alternatives: HashMap<String, Vec<String>> = HashMap::new();
let mut or_unsupported: HashSet<String> = HashSet::new();
+ // Property shape -> a member datatype the store does not preserve.
+ // The direct sh:datatype path refuses that question; a member
+ // answering it reported every widened value as matching nothing.
+ let mut or_unpreserved: HashMap<String, String> = HashMap::new();
let or_rows = query_solutions_bound(
&shapes_store,
&format!(
@@ -835,7 +839,12 @@ impl ShaclValidator {
continue;
}
let clause = if let Some(dt) = row.get("datatype") {
- datatype_conforms(&strip_angle_brackets(dt), "val")
+ let dt = strip_angle_brackets(dt);
+ if datatype_is_indistinguishable_in_store(&dt) {
+ or_unpreserved.insert(p, dt);
+ continue;
+ }
+ datatype_conforms(&dt, "val")
} else if let Some(c) = row.get("class") {
format!(
"EXISTS {{ ?val <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/<http://www.w3.org/2000/01/rdf-schema#subClassOf>* <{}> }}",
@@ -849,6 +858,13 @@ impl ShaclValidator {
};
or_alternatives.entry(p).or_default().push(clause);
}
+ for (p, dt) in &or_unpreserved {
+ or_alternatives.remove(p);
+ if !or_unsupported.contains(p) {
+ let path = or_paths.get(p).cloned().unwrap_or_default();
+ skipped.push(unpreserved_datatype_skip(&shape_iri, "sh:or", &path, dt));
+ }
+ }
for p in &or_unsupported {
or_alternatives.remove(p);
skipped.push(serde_json::json!({
@@ -876,6 +892,10 @@ impl ShaclValidator {
// and 198 real violations were reported as a clean run.
let mut not_clauses: HashMap<String, Vec<String>> = HashMap::new();
let mut not_unsupported: HashSet<String> = HashSet::new();
+ // As `or_unpreserved`. Here answering anyway was a false clean: a
+ // value of the forbidden datatype read as xsd:integer, did not
+ // match, and satisfied the negation.
+ let mut not_unpreserved: HashMap<String, String> = HashMap::new();
let not_rows = query_solutions_bound(
&shapes_store,
&format!(
@@ -918,7 +938,12 @@ impl ShaclValidator {
continue;
}
let clause = if let Some(dt) = row.get("datatype") {
- datatype_conforms(&strip_angle_brackets(dt), "val")
+ let dt = strip_angle_brackets(dt);
+ if datatype_is_indistinguishable_in_store(&dt) {
+ not_unpreserved.insert(p, dt);
+ continue;
+ }
+ datatype_conforms(&dt, "val")
} else if let Some(c) = row.get("class") {
format!(
"EXISTS {{ ?val <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/<http://www.w3.org/2000/01/rdf-schema#subClassOf>* <{}> }}",
@@ -939,6 +964,13 @@ impl ShaclValidator {
};
not_clauses.entry(p).or_default().push(clause);
}
+ for (p, dt) in &not_unpreserved {
+ not_clauses.remove(p);
+ if !not_unsupported.contains(p) {
+ let path = not_paths.get(p).cloned().unwrap_or_default();
+ skipped.push(unpreserved_datatype_skip(&shape_iri, "sh:not", &path, dt));
+ }
+ }
for p in &not_unsupported {
not_clauses.remove(p);
skipped.push(serde_json::json!({
@@ -1184,6 +1216,17 @@ impl ShaclValidator {
row.get("message").cloned().filter(|_| !q_message.is_empty()),
);
+ if let Some(dt) = row.get("datatype").map(|d| strip_angle_brackets(d))
+ && datatype_is_indistinguishable_in_store(&dt)
+ {
+ skipped.push(unpreserved_datatype_skip(
+ &shape_iri,
+ "sh:qualifiedValueShape",
+ &q_path,
+ &dt,
+ ));
+ continue;
+ }
let clause = if row.get("other").is_some() {
None
} else if let Some(c) = row.get("class") {
@@ -1460,15 +1503,7 @@ impl ShaclValidator {
// for every value that satisfied the shape: nine of them in
// jsonld-escaping-conformance, which is how this was found.
if datatype_is_indistinguishable_in_store(&dt) {
- skipped.push(serde_json::json!({
- "shape": strip_angle_brackets(&shape_iri),
- "constraint": "sh:datatype",
- "path": path,
- "datatype": dt,
- "reason": "the store does not preserve this datatype IRI, so a value \
- carrying it is indistinguishable from one carrying the type \
- it derives from; the constraint was not evaluated",
- }));
+ skipped.push(unpreserved_datatype_skip(&shape_iri, "sh:datatype", &path, &dt));
continue;
}
// One evaluator gave two answers to one constraint. SHACL
@@ -2978,6 +3013,24 @@ fn datatype_is_indistinguishable_in_store(datatype: &str) -> bool {
}
+/// The `skipped_constraints` entry for a constraint that names a datatype
+/// [`datatype_is_indistinguishable_in_store`] says cannot be decided. One
+/// record, so the direct `sh:datatype` constraint and the `sh:or`, `sh:not`
+/// and `sh:qualifiedValueShape` members that name such a datatype refuse in
+/// the same words: the member paths used to answer the question the direct
+/// path refuses, and answered it wrongly.
+fn unpreserved_datatype_skip(shape: &str, constraint: &str, path: &str, datatype: &str) -> serde_json::Value {
+ serde_json::json!({
+ "shape": strip_angle_brackets(shape),
+ "constraint": constraint,
+ "path": path,
+ "datatype": datatype,
+ "reason": "the store does not preserve this datatype IRI, so a value \
+ carrying it is indistinguishable from one carrying the type \
+ it derives from; the constraint was not evaluated",
+ })
+}
+
/// The SPARQL pattern selecting the focus nodes of one target, bound to `var`.
///
/// Note for anyone counting over the result: the class selector binds a focus
diff --git a/tests/shacl_datatype_indistinguishable_test.rs b/tests/shacl_datatype_indistinguishable_test.rs
index e8152f8f..d3a13ee2 100644
--- a/tests/shacl_datatype_indistinguishable_test.rs
+++ b/tests/shacl_datatype_indistinguishable_test.rs
@@ -109,3 +109,92 @@ fn plain_integer_is_still_evaluated() {
let r = report(&store(), shapes);
assert!(!r["conforms"].is_null(), "must give a verdict: {r}");
}
+
+// ── The same guard on every path that tests a datatype ───────────────────────
+//
+// The direct `sh:datatype` constraint (on a property shape and on a node shape)
+// consults `datatype_is_indistinguishable_in_store` and refuses to answer. The
+// paths that test a datatype as a MEMBER of something else did not, so the
+// question the direct path refuses was answered anyway, wrongly, by them: every
+// value the store had widened to `xsd:integer` failed a member of
+// `xsd:nonNegativeInteger`. Each test below names its path, asserts the refusal
+// the direct path gives, and asserts that no violation is reported.
+
+const PREFIXES: &str = r#"
+ @prefix sh: <http://www.w3.org/ns/shacl#> .
+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+ @prefix ex: <http://example.org/> .
+"#;
+
+fn refused(shapes_body: &str, constraint: &str) {
+ let r = report(&store(), &format!("{PREFIXES}{shapes_body}"));
+ assert_eq!(
+ r["violation_count"],
+ serde_json::json!(0),
+ "the data satisfies this shape; reporting a violation is the bug: {r}"
+ );
+ assert!(r["conforms"].is_null(), "the datatype cannot be decided, so there is no verdict: {r}");
+ let skipped = r["skipped_constraints"].as_array().cloned().unwrap_or_default();
+ assert!(
+ skipped.iter().any(|s| s["constraint"] == constraint),
+ "{constraint} must be recorded as not evaluated: {r}"
+ );
+}
+
+/// `sh:or` over a property shape's values, one member naming the datatype.
+#[test]
+fn an_or_member_naming_an_unpreserved_datatype_is_not_answered() {
+ refused(
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:passes ;
+ sh:or ( [ sh:datatype xsd:nonNegativeInteger ] [ sh:datatype xsd:string ] ) ] .",
+ "sh:or",
+ );
+}
+
+/// `sh:not [ sh:datatype D ]`. Here the wrong answer was a FALSE CLEAN: every
+/// value is an xsd:nonNegativeInteger and so violates the negation, but the
+/// store said xsd:integer and the run came back `conforms: true`.
+#[test]
+fn a_not_member_naming_an_unpreserved_datatype_is_not_answered() {
+ refused(
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:passes ; sh:not [ sh:datatype xsd:nonNegativeInteger ] ] .",
+ "sh:not",
+ );
+}
+
+/// `sh:qualifiedValueShape [ sh:datatype D ]`: no value counted as conforming.
+#[test]
+fn a_qualified_shape_naming_an_unpreserved_datatype_is_not_answered() {
+ refused(
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:passes ;
+ sh:qualifiedValueShape [ sh:datatype xsd:nonNegativeInteger ] ; sh:qualifiedMinCount 1 ] .",
+ "sh:qualifiedValueShape",
+ );
+}
+
+/// The guard must not swallow the decidable members: the same four paths over
+/// `xsd:integer` and `xsd:string` still give a verdict and still catch a value
+/// that matches none of them.
+#[test]
+fn members_naming_a_preserved_datatype_are_still_evaluated() {
+ let cases = [
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:name ; sh:or ( [ sh:datatype xsd:integer ] [ sh:datatype xsd:date ] ) ] .",
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:name ; sh:not [ sh:datatype xsd:string ] ] .",
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:name ;
+ sh:qualifiedValueShape [ sh:datatype xsd:integer ] ; sh:qualifiedMinCount 1 ] .",
+ "ex:S a sh:NodeShape ; sh:targetClass ex:Profile ;
+ sh:property [ sh:path ex:name ; sh:node ex:Int ] .
+ ex:Int a sh:NodeShape ; sh:datatype xsd:integer .",
+ ];
+ for body in cases {
+ let r = report(&store(), &format!("{PREFIXES}{body}"));
+ assert_eq!(r["conforms"], serde_json::json!(false), "{body}\n{r}");
+ assert_eq!(r["violation_count"], serde_json::json!(2), "{body}\n{r}");
+ }
+}