元组数组的JSON架构

JSON Schema for Array of tuples

本文关键字:架构 JSON 数组 元组      更新时间:2023-09-26

提前感谢。

我是JSON&JSON架构。尝试为元组数组生成JSON模式。但它并不是像对所有相似类型的元组进行循环那样验证多个记录。下面是json示例。

{
  "Data":
   [
      [ 100, "Test", 2.5 ],
      [ 101, "Test1", 3.5]
   ]
}

我已经使用站点jsonschema.net生成了如下的模式

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net",
  "type": "object",
  "properties": {
    "Data": {
      "id": "http://jsonschema.net/Data",
      "type": "array",
      "items": [
        {
          "id": "http://jsonschema.net/Data/0",
          "type": "array",
          "items": [
            {
              "id": "http://jsonschema.net/Data/0/0",
              "type": "integer"
            },
            {
              "id": "http://jsonschema.net/Data/0/1",
              "type": "string"
            },
            {
              "id": "http://jsonschema.net/Data/0/2",
              "type": "number"
            }
          ],
          "required": [
            "0",
            "1",
            "2"
          ]
        },
        {
          "id": "http://jsonschema.net/Data/1",
          "type": "array",
          "items": [
            {
              "id": "http://jsonschema.net/Data/1/0",
              "type": "integer"
            },
            {
              "id": "http://jsonschema.net/Data/1/1",
              "type": "string"
            },
            {
              "id": "http://jsonschema.net/Data/1/2",
              "type": "number"
            }
          ]
        }
      ],
      "required": [
        "0",
        "1"
      ]
    }
  },
  "required": [
    "Data"
  ]
}

如果您看到了,它正在为每一个相似类型的元组创建模式。请帮助我创建一个模式,以通用的方式验证每个元组。元组计数可能有所不同。

如果您希望内部数组具有相同种类的所有项,则可以使用对象而不是数组。以下模式验证了您的示例:

{
    "type" : "object",
    "properties" : {
        "Data" : {
            "type" : "array",
            "items" : {
                "type" : "array",
                "items" : [{
                        "type" : "integer"
                    }, {
                        "type" : "string"
                    }, {
                        "type" : "number"
                    }
                ]
            }
        }
    }
}

我在这里测试过。

JSON模式为元组提供了一种新的语法,jruizaranguren之前提出的解决方案现在可以用这种方式更精确地编写:

{
  "type": "object",
  "properties": {
    "Data": {
      "type": "array",
      "items": [
        {
          "type": "array",
          "prefixItems": [
            { "type": "integer" },
            { "type": "string" },
            { "type": "integer" }
          ],
          "minItems": 3,
          "items": false    
        }
      ]
    }
  },
  "required": [
    "Data"
  ]
}
  {
  "Table1": {
    "Data": [
      [
        100,
        "Test",
        2.5
      ],
      [
        101,
        "Test1",
        5.5
      ]
    ]
  }
}

上面是示例json&其模式如下

    {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net",
  "type": "object",
  "properties": {
    "Table1": {
      "id": "http://jsonschema.net/Table1",
      "type": "object",
      "properties": {
        "Data": {
          "id": "http://jsonschema.net/Table1/Data",
          "type": "array",
          "items": {
            "type": "array",
            "items": [
              {
                "id": "http://jsonschema.net/Table1/Data/0/0",
                "type": "integer"
              },
              {
                "id": "http://jsonschema.net/Table1/Data/0/1",
                "type": "string"
              },
              {
                "id": "http://jsonschema.net/Table1/Data/0/2",
                "type": "number"
              }
            ],
            "additionalItems": false,
            "required": [
              "0",
              "1",
              "2"
            ]
          }
        }
      },
      "required": [
        "Data"
      ]
    }
  }
}

此模式适用于所有数据行,但其必需的属性无法正常工作。尽管我期待所有3列的数据。它也接受具有1或2列的行。如果有人知道的话。请纠正我。

[ 101 ], [ 101, "TEST3" ]

也是不期望的数据的有效记录。