How do I use the `If` `then` `else` condition in json schema?

后端 未结 2 1454
孤独总比滥情好
孤独总比滥情好 2020-12-09 18:44

A relatively new addition to JSON Schema (draft-07) adds the if, then and else keywords. I cannot work out how to use these new key words correctly. Here is my JSON Schema s

2条回答
  •  暖寄归人
    2020-12-09 19:26

    Can't you just use the "else" property ?

    {
        "type": "object",
        "properties": {
            "foo": { "type": "string" },
            "bar": { "type": "string" }
         },
         "if": {
            "properties": {
                "foo": { 
                  "enum": ["bar"] 
                }
            }
        },
        "then": { 
          "required": ["bar"]
        },
        "else": {
          "required": [] 
        }
    }
    

提交回复
热议问题