Tivie
Tivie

Reputation: 18923

Validate a property value against another property value

Take the following schema:

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

I want to create a validation rule (within the schema) that states that:

if StageHEP = ACUTE, complications property cannot be CIRR

Is it possible with json-schema draft v4?

Upvotes: 0

Views: 83

Answers (1)

Pedro
Pedro

Reputation: 1965

You can do it using "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
      }
    }
  ]
}

Upvotes: 1

Related Questions