0
votes

I have mentioned two scenarios below, for which i want to generate a jsonschema. If the color value is red then redQuote will be the required filed in the jsonschema. If it is black then blackQuote be the required filed. So required filed of the jsonschema is depend on color value. how we can use if'/'then'/'else' keywords, or 'dependencies' in draft-07 and 'dependentRequired' in draft2019-09 in this case?.

Example-1

{
 "color": "red",
  "quote": {
                "is_apply": True,
               "redQuote": "something"
               }
}

Example-2

{
 "color": "black",
  "quote": {
                 "is_apply": True,
               "blackQuote": "something"
               }
}
1

1 Answers

1
votes
{
    "anyOf": [
        {
            "type": "object",
            "properties": {
                "color": {"const": "red"},
                "quote":{
                    "properties":{
                        "is_apply": {"type": "boolean"}
                    },
                    "required": ["redQuote"]
                }
            }
        },
        {
            "type": "object",
            "properties": {
                "color": {"const": "black"},
                "quote":{
                    "properties":{
                        "is_apply": {"type": "boolean"}
                    },
                    "required": ["blackQuote"]
                }
            }
        }
    ]
}

It worked for me. Feel free to comment if there are any other way to do.