最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON Schema for Array of tuples - Stack Overflow

programmeradmin4浏览0评论

Thanks in advance.

I am new to JSON & JSON schema. Tried to generate JSON schema for array of tuples. but it is not validating multiple records like a loop for all similar types of tuples. Below is json sample.

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

I have generated schema using site jsonschema as below

{

  "$schema": "",
  "id": "",
  "type": "object",
  "properties": {
    "Data": {
      "id": "",
      "type": "array",
      "items": [
        {
          "id": "",
          "type": "array",
          "items": [
            {
              "id": "",
              "type": "integer"
            },
            {
              "id": "",
              "type": "string"
            },
            {
              "id": "",
              "type": "number"
            }
          ],
          "required": [
            "0",
            "1",
            "2"
          ]
        },
        {
          "id": "",
          "type": "array",
          "items": [
            {
              "id": "",
              "type": "integer"
            },
            {
              "id": "",
              "type": "string"
            },
            {
              "id": "",
              "type": "number"
            }
          ]
        }
      ],
      "required": [
        "0",
        "1"
      ]
    }
  },
  "required": [
    "Data"
  ]
}

If you see, it is creating schema for every tuple of similar type. Please help me to create a schema to validate each tuple in a generic way. Tuple count may vary.

Thanks in advance.

I am new to JSON & JSON schema. Tried to generate JSON schema for array of tuples. but it is not validating multiple records like a loop for all similar types of tuples. Below is json sample.

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

I have generated schema using site jsonschema as below

{

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

If you see, it is creating schema for every tuple of similar type. Please help me to create a schema to validate each tuple in a generic way. Tuple count may vary.

Share Improve this question edited Dec 17, 2015 at 13:35 NitinK asked Dec 17, 2015 at 12:15 NitinKNitinK 1231 gold badge1 silver badge7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

If you want the inner array to have all items of the same kind you may use an object instead of an array. The following schema validates your example:

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

I have tested it here.

The JSON schema has a new syntax for tuples and the solution previously suggested by jruizaranguren can now be written more precisely in this way:

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

Above is sample json & its schema is as below

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

This schema works for all rows of Data but its required property is not working somehow. Eventhough I am expecting all 3 columns data. It accepts the rows with 1 or 2 columns as well. If anybody has any idea. Please correct me.

[ 101 ], [ 101, "TEST3" ]

are also valid records of data which is not expected.

发布评论

评论列表(0)

  1. 暂无评论