Correct way to define array of enums in JSON schema

后端 未结 2 687
醉话见心
醉话见心 2021-02-11 12:02

I want to describe with JSON schema array, which should consist of zero or more predefined values. To make it simple, let\'s have these possible values: one,

相关标签:
2条回答
  • 2021-02-11 12:39

    According to json-schema documentation, the enumerated values of an array must be included in the "items" field:

    {
        "type": "array",
        "items": {
            "type": "string",
            "enum": ["one", "two", "three"]
        }
    }
    

    If you have an array that can hold e.g. items of different type, then your schema should look like the one below:

    {
      "type": "array",
      "items": [
        {
          "type": "string",
          "enum": ["one", "two", "three"]
        },
        {
          "type": "integer",
          "enum": [1, 2, 3]
        }
      ]
    }
    
    0 讨论(0)
  • 2021-02-11 12:42

    Option A is correct and satisfy your requirements.

    {
        "type": "array",
        "items": {
            "type": "string",
            "enum": ["one", "two", "three"]
        }
    }
    
    0 讨论(0)
提交回复
热议问题