Home > database >  Conditional statements do not apply in Json Schema
Conditional statements do not apply in Json Schema

Time:12-15

I would like to change the values acceptable to the Value B field according to the values of the Value A field in the following Json.

{"maximumList": [
{
  "ValueA": 232,
  "ValueB": ["aa"]
}]}

To do so, I obtained Schema using the Json Schema Generator and modified it to make it as follows.

  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "maximumList": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "ValueA": {
              "type": "integer"
            },
            "ValueB": {
              "type": "array",
              "items": [
                {
                  "type": "string"
                }
              ]
            }
          },
          "required": [
            "ValueA",
            "ValueB"
          ],
          "if":{
            "properties":{
               "ValueA" :{"const":232} 
            }
          },
          "then":{
             "ValueB":{
               "enum":["bbb"]
             }
          }
        }
      ]
    }
  },
  "required": [
    "maximumList"
  ]
}

definitely set the value of the ValueB field to allow only "bbb", but even if "aaa" is entered in the value of the ValueB field, it passes the validation.

Is there anything I'm missing?

CodePudding user response:

I think your problem is that you're using draft 4 of JSON Schema (identified by the $schema keyword value of http://json-schema.org/draft-04/schema#.

The if/then/else keywords are only supported in draft 7 and later. If you change the $schema value to http://json-schema.org/draft-07/schema#, it should work for you.

  • Related