Shacl.js: Verbose report for errors occurred in parent shapes


#1

I am using shacl.js for structured data validation and recently I encountered an obstacle:
Suppose we have shapes CreativeWork and Recipe with the property author, Recipe is extending CreativeWork:
:CreativeWork a sh:NodeShape;
sh:targetClass schema:CreativeWork;
sh:property [
sh:path schema:author;
sh:or (
[sh:class schema:Person]
[sh:class schema:Organization]
);
].

:Recipe a sh:NodeShape;
sh:targetClass schema:Recipe;
sh:node :CreativeWork;
sh:property [
sh:path schema:author;
sh:minCount 1;
].

When the Recipe data contains author property with an error (e.g. author is specified in Text format), shacl.js gives an error like “Value does not have shape http://example.org/CreativeWork”, but it would be nice to know exactly which property caused the failure and the reason of it.
Can I somehow find out what is the exact cause of each failure in these specific cases?
Thank you in advance for your help :slight_smile:


#2

I’m not sure which library you are using for SHACL validation but in theory you should be getting a more detailed report as shown here: https://shacl.org/playground/

Something like:

[
a sh:ValidationResult ;
sh:resultSeverity sh:Violation ;
sh:sourceConstraintComponent sh:LessThanConstraintComponent ;
sh:sourceShape _:n2 ;
sh:focusNode http://example.org/ns#Bob ;
sh:resultPath schema:birthDate ;
sh:value “1971-07-07” ;
sh:resultMessage “Value is not < value of schema:deathDate” ;
] .

That error has a focus node and a result path that would pin-point the exact cause of the failed validation.


#3

I am using the library from this repo: zazuko/rdf-validate-shacl.
You are right, I can identify the cause of the error if it happens when validating directly against the shape with the corresponding sh:targetClass. For example, if I validate some Recipe markup without an author field against shapes specified above in the text of my question I can extract the failing property with sh:resultPath. But the problem occurs when I validate the markup, which has the author field, but the type of provided node is not right (eg. Text instead of Person or Organization, which doesn’t correspond the constraint in the parent shape CreativeWork) - the validation report doesn’t have a value for sh:resultPath.
To illustrate this, I validated this data against the shapes I specified above in the SHACL playground tool:

{
“context”: { “[at]vocab”:“http://schema.org/”},
“[at]id”: “http://example.org/recipe”,
“[at]type”: “Recipe”,
“name”: “Mom’s World Famous Banana Bread”,
“author” : “sdfdsf”
} (here [at] is @, I can’t use it in the post)

This is what I got:


#4

Hey there!

Your use of sh:node in a sh:NodeShape is interesting. I have to do some research to see if people use it that way.

For your use case, I think defining schema:Recipe rdfs:subClassOf schema:CreativeWork would make more sense and would give you more meaningful validation results:

@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix schema: <http://schema.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

schema:Recipe rdfs:subClassOf schema:CreativeWork .

<CreativeWorkShape> a sh:NodeShape;
  sh:targetClass schema:CreativeWork;
  sh:property [
    sh:path schema:author;
    sh:or (
      [sh:class schema:Person]
      [sh:class schema:Organization]
    );
  ] ;
.

<RecipeShape> a sh:NodeShape;
  sh:targetClass schema:Recipe;
  sh:property [
    sh:path schema:author;
    sh:minCount 1;
  ] ;
.

<recipe> a schema:Recipe ;
  schema:name "Mom’s World Famous Banana Bread" ;
  schema:author "sdfdsf" ;
.

gives me the following validation report:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix schema: <http://schema.org/> .

_:b7
   rdf:type sh:ValidationResult ;
   sh:focusNode <recipe> ;
   sh:resultPath schema:author ;
   sh:resultSeverity sh:Violation ;
   sh:sourceConstraintComponent sh:OrConstraintComponent ;
   sh:sourceShape _:b1 ;
   sh:value "sdfdsf" .

_:b1
   sh:or _:b2 ;
   sh:path schema:author .

_:b2
   rdf:first _:b3 ;
   rdf:rest _:b4 .

_:b3
   sh:class schema:Person .

_:b4
   rdf:first _:b5 ;
   rdf:rest rdf:nil .

_:b5
   sh:class schema:Organization .

_:report
   rdf:type sh:ValidationReport ;
   sh:conforms false ;
   sh:result _:b7 .

I need to investigate why the result doesn’t have a human readable sh:validationMessage, but at least it has a sh:resultPath pointing to schema:author, which should make it easier to detect what’s wrong with your data.

[UPDATE] There is no default validation message defined for sh:OrConstraintComponent. In a way it makes sense. I couldn’t come up with anything generic enough for an “or” and still useful for someone…


#5

Thanks a lot for your answer, this makes a lot more sense now :grinning: