简体   繁体   English

相当于 XSD maxOccurs 的 JSON Schema

[英]JSON Schema equivalent of XSD maxOccurs

I have been searching around for a few hours now but I can't find examples for the query I have.我已经搜索了几个小时,但我找不到我所拥有的查询的示例。 I have an XSD schema (this is a really cut down version)我有一个 XSD 架构(这是一个非常精简的版本)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Transaction-Header">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Origin" type="xs:string" minOccurs="1" maxOccurs="1">
        </xs:element>
        <xs:element name="Type" type="xs:string" minOccurs="0" maxOccurs="1">
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Transaction">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Transaction-Header" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I am converting it to JSONSchema (it's a massive xsd) with a lot of success but what I can't seem to figure out is how to incorporate the minOccurs and maxOccurs restrictions.我正在将它转换为 JSONSchema(它是一个巨大的 xsd)并取得了很大的成功,但我似乎无法弄清楚如何合并 minOccurs 和 maxOccurs 限制。

Essentially, there should always only ever be 1 Transaction-Header ie从本质上讲,应该总是只有 1 个 Transaction-Header 即

{
  "Transaction": {
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    }
  }
}

Invalid:无效的:

{
  "Transaction": {
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    },
    "Transaction-Header": {
      "Origin": "Origin",
      "Type": "Type"
    }
  }
}

I understand this can be done by using the array minItems / maxItems functionality, but these are really elements and not arrays and I don't really want to have to define them as such if possible.我知道这可以通过使用数组 minItems / maxItems 功能来完成,但这些实际上是元素而不是数组,如果可能的话,我真的不想这样定义它们。

Anyone know how (or even if) this is possible please?请问有谁知道(或者即使)这是可能的吗?

JSON Schema uses the JSON document model, and your second example is not valid JSON -- a property name cannot appear twice. JSON Schema 使用 JSON 文档模型,您的第二个示例不是有效的 JSON - 属性名称不能出现两次。 You'll have to rename your properties so they are unique, or use an array.您必须重命名您的属性,使它们是唯一的,或者使用一个数组。

If you use an array, you can use contains and minContains / maxContains to specify limitations on items in the array.如果使用数组,可以使用containsminContains / maxContains来指定对数组中项目的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM