shacl

Use SHACL to validate a date within a certain range


How can I use SHACL to validate if a date lies within a certain range? I tried using minInclusive, maxInclusive, minExclusive, maxExcluse and lessThan, but nothing seems to work. I am using the SHACL Playground with this data.

Shapes Graph

@prefix dash: <http://datashapes.org/dash#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.com/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:QueryParameterShape
    a sh:NodeShape ;
    sh:targetClass ex:QueryParameter ;
    sh:property [
        sh:path ex:peildatum ;
        sh:datatype xsd:date ;
        sh:lessThan "2022-01-01"^^xsd:date  ;
        sh:maxCount 0 ;
        sh:name "peildatum" ;
    ] .

Data Graph

[
    {
        "@context": { "@vocab": "http://example.com/" },
        "@id": "http://example.com/query_variable_1",
        "@type": "QueryParameter",
        "peildatum": [
            {
                "@value": "2022-05-01",
                "@type": "http://www.w3.org/2001/XMLSchema#date"
            }
        ]
    },
    {
        "@context": { "@vocab": "http://example.com/" },
        "@id": "http://example.com/query_variable_2",
        "@type": "QueryParameter",
        "peildatum": [
            {
                "@value": "2021-05-01",
                "@type": "http://www.w3.org/2001/XMLSchema#date"
            }
        ]
    }
]

The validation report states:

[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:MaxCountConstraintComponent ;
    sh:sourceShape _:n3790 ;
    sh:focusNode ex:query_variable_1 ;
    sh:resultPath ex:peildatum ;
    sh:resultMessage "More than 0 values" ;
] .
[
    a sh:ValidationResult ;
    sh:resultSeverity sh:Violation ;
    sh:sourceConstraintComponent sh:MaxCountConstraintComponent ;
    sh:sourceShape _:n3790 ;
    sh:focusNode ex:query_variable_2 ;
    sh:resultPath ex:peildatum ;
    sh:resultMessage "More than 0 values" ;
] .

I added the maxCount 0 restriction to see if the validation report works at all. And yes, it does. But the restriction on the date does not work.

Any ideas?


Solution

  • sh:lessThan is used between a pair of properties, e.g. ex:birthDate sh:lessThan sh:deathDate. Use sh:minInclusive etc to compare with specific values.

    https://www.w3.org/TR/shacl/#core-components-range

    Having said this, the SHACL Playground hasn't been maintained for 5 years and has known limitations with the handling of < operations including sh:minInclusive, so you should not rely on it. There are plenty of other open source libraries available.