簡體   English   中英

使用 SHACL 驗證特定范圍內的日期

[英]Use SHACL to validate a date within a certain range

如何使用 SHACL 驗證日期是否在特定范圍內? 我嘗試使用 minInclusive、maxInclusive、minExclusive、maxExcluse 和 lessThan,但似乎沒有任何效果。 我正在使用帶有這些數據的 SHACL Playground。

形狀圖

@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" ;
    ] .

數據圖

[
    {
        "@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"
            }
        ]
    }
]

驗證報告指出:

[
    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" ;
] .

我添加了 maxCount 0 限制以查看驗證報告是否有效。 是的,確實如此。 但是對日期的限制不起作用。

有任何想法嗎?

sh:lessThan 用於一對屬性之間,例如 ex:birthDate sh:lessThan sh:deathDate。 使用 sh:minInclusive 等與特定值進行比較。

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

話雖如此,SHACL Playground 已經有 5 年沒有維護了,並且在處理 < 操作(包括 sh:minInclusive)方面存在已知限制,因此您不應依賴它。 還有很多其他可用的開源庫。

為了完整起見,正確的形狀圖應該是:

@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:maxInclusive "2021-12-31"^^xsd:date  ;
        sh:name "peildatum" ;
    ] .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM