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

Validate a property value against another property value

采用以下架构:

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

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

if StageHEP = ACUTE, complications property cannot be CIRR

json-schema draft v4 是否可行?

您可以使用 "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
      }
    }
  ]
}