首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SHACL能验证包含对象列表的JSON吗?

SHACL能验证包含对象列表的JSON吗?
EN

Stack Overflow用户
提问于 2019-10-29 18:12:28
回答 1查看 464关注 0票数 2

我希望使用SHACL验证以下JSON-LD:

代码语言:javascript
复制
{
  "@context" : {
    "day" : {
      "@id" : "test:day"
    },
    "month" : {
      "@id" : "test:month"
    },
    "myList" : {
      "@id" : "test:myList"
    },
    "year" : {
      "@id" : "test:year"
    },
    "schema" : "http://schema.org/",
    "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd" : "http://www.w3.org/2001/XMLSchema#",
    "test" : "http://www.test.com/ns#"
  },
  "@graph" : [ {
    "@id" : "test:MyNode",
    "@type" : "test:MyTargetClass",
    "myList" : [
      {
        "year" : "2019",
        "month" : "October",
        "day" : "29"
      },
      {
        "year" : "2018",
        "month" : "January",
        "day" : "17"
      }
    ]
  } ]
}

在上面的示例中,myList是一个对象列表,这些对象必须至少包含一个元素,每个元素必须包含所有三个字段:yearmonthday。正在使用以下TTL对其进行验证:

代码语言:javascript
复制
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix test: <http://www.test.com/ns#> .

test:MyListShape
    a sh:NodeShape ;
    sh:closed true ;
    sh:ignoredProperties ( rdf:type ) ;
    sh:property [
        sh:path test:year ;
        sh:datatype xsd:string ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] ;
    sh:property [
        sh:path test:month ;
        sh:datatype xsd:string ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] ;
    sh:property [
        sh:path test:day ;
        sh:datatype xsd:string ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] .

test:MyShape
    a sh:NodeShape ;
    sh:closed true ;
    sh:ignoredProperties ( rdf:type ) ;
    sh:targetClass test:MyTargetClass ;
    sh:property [
        sh:path test:myList ;
        sh:node test:MyListShape ;
        sh:minCount 1 ;
    ] .

试图验证JSON返回一个包含以下代码段的响应,说明为什么不符合:

代码语言:javascript
复制
{
  "@id" : "_:b3",
  "@type" : "http://www.w3.org/ns/shacl#ValidationResult",
  "focusNode" : "test:MyNode",
  "resultMessage" : "Property may only have 1 value, but found 2",
  "resultPath" : "test:myList",
  "resultSeverity" : "http://www.w3.org/ns/shacl#Violation",
  "sourceConstraintComponent" : "http://www.w3.org/ns/shacl#MaxCountConstraintComponent",
  "sourceShape" : "_:b4"
}

为什么test:myList属性必须只有一个值,即使我没有指定一个sh:maxCount

我还尝试将@context中的@context更改为:

代码语言:javascript
复制
"myList" : {
  "@id" : "test:myList",
  "@container" : "@list"
}

但是,这也使不符合,并返回包含以下代码段的响应:

代码语言:javascript
复制
{
  "@id" : "_:b0",
  "@type" : "http://www.w3.org/ns/shacl#ValidationResult",
  "focusNode" : "test:MyNode",
  "resultMessage" : "Value does not have shape test:MyListShape",
  "resultPath" : "test:myList",
  "resultSeverity" : "http://www.w3.org/ns/shacl#Violation",
  "sourceConstraintComponent" : "http://www.w3.org/ns/shacl#NodeConstraintComponent",
  "sourceShape" : "_:b2",
  "value" : "_:b1"
}

我遇到的另一种解决方案是将myList存储在@graph中的一个单独的节点中,但这对我的用例来说并不理想:

代码语言:javascript
复制
{
  "@id" : "test:myListNode",
  "@type" : "test:myListNode",
  "year" : [ "2019", "2018" ],
  "month" : [ "October", "January" ],
  "day" : [ "29", "17" ]
}

因此,是否可以使用SHACL来验证包含对象列表的JSON-LD,而不必使用此替代解决方案?

EN

回答 1

Stack Overflow用户

发布于 2019-10-31 15:41:05

是的,SHACL可以验证包含对象列表的JSON。

PEBCAK错误导致故障。(见评论)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58613222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档