Home > Mobile >  JSON Schema equivalent of XSD maxOccurs
JSON Schema equivalent of XSD maxOccurs

Time:05-13

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)

<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.

Essentially, there should always only ever be 1 Transaction-Header i.e.

{
  "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.

Anyone know how (or even if) this is possible please?

CodePudding user response:

JSON Schema uses the JSON document model, and your second example is not valid JSON -- a property name cannot appear twice. 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.

  • Related