zazuko / rdf-validate-shacl

Validate RDF data purely in JavaScript. An implementation of the W3C SHACL specification on top of the RDFJS stack.
MIT License
95 stars 12 forks source link

Value does not have datatype error with sh:datatype xsd:anyURI #103

Closed Abrom8 closed 1 year ago

Abrom8 commented 1 year ago

Properties with sh:datatype xsd:anyURI always produce Value does not have datatype <http://www.w3.org/2001/XMLSchema#anyURI> error.

I created a minimal example for the https://shacl-playground.zazuko.com/ playground.

Shapes Graph:

@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix g-validation:  <http://localhost:3001/shapes/validation#> .

@prefix g-framework: <http://localhost:3001/shapes/g-framework#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

g-validation:ServiceShape
    a sh:NodeShape ;
    sh:targetClass g-framework:Service ;
    sh:property [ sh:path g-framework:content ;
                           sh:datatype xsd:anyURI ] ;
.

Data Graph:

{
    "@context": {
        "g-framework": "http://localhost:3001/shapes/g-framework#",
        "g-validation": "http://localhost:3001/shapes/validation#",
        "sh": "http://www.w3.org/ns/shacl#",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
  "@type": "g-framework:Service",
  "g-framework:content": "http://example.org/"

}
tpluscode commented 1 year ago

Thank you for reporting.

In your case, the error is in fact correct because the value is a plain literal. The validation sh:datatype is strict in that it requires a literal to have an exact matching datatype. Compare with other SHACL implementations, all of which yield same result.

If you need your JSON-LD to keep its idiomatic form, you can modify the @context to have it assert the values of the content property:

{
  "@context": {
    "g-framework": "http://localhost:3001/shapes/g-framework#",
    "g-validation": "http://localhost:3001/shapes/validation#",
    "sh": "http://www.w3.org/ns/shacl#",
-   "xsd": "http://www.w3.org/2001/XMLSchema#"
+   "xsd": "http://www.w3.org/2001/XMLSchema#",
+   "g-framework:content": {
+     "@type": "xsd:anyURI"
+   }
  },
  "@type": "g-framework:Service",
  "g-framework:content": "http://example.org/"
}
tpluscode commented 1 year ago

On a related note, the actual URI is not actually validated for correctness. One might argue that it should (we do that for numeric values and dates) but none of the other implementations available on https://rdfshape.weso.es/shaclValidate do that either...

Abrom8 commented 1 year ago

Thank you! The example with the @context helped a lot! 👏