JSON条件架构

问题描述 投票:1回答:1

我有以下json架构,需要添加几个条件,如下所示。

if user_type == "human"
   then environment should be "A1 OR A2 OR A3"

if user_type == "batch"
   then environment should be "B1 OR B2 OR B3"

我应该如何将这个条件添加到我的json模式下面。

  {
  "items": {
    "properties": {
      "user_type": {
        "type": "string",
        "pattern": "^(?i)(human|batch)$"
      },
      "user_name": {
        "type": "string",
        "pattern": "^[A-Za-z0-9]{8}$"
      },
      "environment": {
        "type": "string"
      },
      "access_type": {
        "type": "string",
        "pattern": "^(?i)(read|write|power)$"
      }
    },
      "required": [
        "user_type",
        "user_name",
        "environment",
        "access_type"
      ],
      "type": "object"
    },
    "type": "array"
  }
json jsonschema json-schema-validator
1个回答
2
投票

您可以使用anyOf如下:

{
  "items":{
    "properties":{
      "user_name":{
        "type":"string",
        "pattern":"^[A-Za-z0-9]{8}$"
      },
      "access_type":{
        "type":"string",
        "pattern":"^(?i)(read|write|power)$"
      }
    },
    "required":[
      "user_type",
      "user_name",
      "environment",
      "access_type"
    ],
    "anyOf":[
      {
        "properties":{
          "user_type":{
            "const":"human"
          },
          "environment":{
            "enum":["A1","A2","A3"]
          }
        }
      },
      {
        "properties":{
          "user_type":{
            "const":"batch"
          },
          "environment":{
            "enum":["B1","B2","B3"]
          }
        }
      }
    ],
    "type":"object"
  },
  "type":"array"
}
© www.soinside.com 2019 - 2024. All rights reserved.