根据另一个属性值验证属性值

时间:2017-09-14 11:40:53

标签: jsonschema json-schema-validator

采取以下架构:

{
  "properties": {
    "StageHEP": { 
      "description": "The stage of hepatitis",
      "type": "string",
      "enum": ["ACUTE", "CHRONIC", "UNK"]
    },
    "complications": {
      "description": "Disease complications", 
      "type": "string",
      "enum: ["CIRR", "CANCER", "NONE", "UNK"]
    }
  }
}

我想创建一个验证规则(在架构中),声明:

  

如果StageHEP = ACUTEcomplications属性不能为CIRR

是否可以使用json-schema草案v4?

1 个答案:

答案 0 :(得分:1)

您可以使用"oneOf"执行此操作:

{
  "oneOf": [
    {
      "properties": {
        "StageHEP": {
          "description": "The stage of hepatitis",
          "type": "string",
          "enum": [
            "CHRONIC",
            "UNK"
          ]
        },
        "complications": {
          "description": "Disease complications",
          "type": "string",
          "enum": [
            "CIRR",
            "CANCER",
            "NONE",
            "UNK"
          ]
        },
        "additionalProperties": false
      }
    },
    {
      "properties": {
        "StageHEP": {
          "description": "The stage of hepatitis",
          "type": "string",
          "enum": [
            "ACUTE"
          ]
        },
        "complications": {
          "description": "Disease complications",
          "type": "string",
          "enum": [
            "CANCER",
            "NONE",
            "UNK"
          ]
        },
        "additionalProperties": false
      }
    }
  ]
}