JSON schema conditional dependency on value

我的未来我决定 提交于 2019-12-22 08:04:25

问题


I know there is a similar question here, but it didn't really address my issue. In short, I want one my fields to be dependent on the other field's value. But for some values, I don't want any field to be required. Here is an example:

Schema

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {

    "colour": {
      "type": "string",
      "enum": ["red", "black", "blue"]
    },

    "blackQuote": {
      "type": "string",
      "maxLength": 11
    },

    "redQuote": {
      "type": "string",
      "maxLength": 11
    }
  },

  "oneOf": [
      {
        "properties": {
          "colour": {"enum": ["red"]}
        },
        "required": ["redQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["black"]}
        },
        "required": ["blackQuote"]
      }
  ],

  "required": [
    "colour"
  ]
}

This works like this:

  • IF the colour is "red" THEN "redQuote" (but not "blackQuote") is required: this is fine
  • IF the colour is "black" THEN "blackQuote" (but not "redQuote") is required: this is also fine
  • BUT if I put the colour "blue" in my JSON, then the validator says that the properties "redQuote" and "blackQuote" are missing... I don't want that, I only want dependecies for "red" and "black", but if the colour is "blue" I don't want anything required. How to achieve this?

回答1:


You can do this with a boolean logic concept called implication (!A or B). It can be used like an "if-then" statement. For example, either "color" is not "red" or "redQuote" is required. Any time I need to use this, I break it down with definitions so it reads as nice as possible.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "colour": { "enum": ["red", "black", "blue"] },
    "blackQuote": { "type": "string", "maxLength": 11 },
    "redQuote": { "type": "string", "maxLength": 11 }
  },
  "allOf": [
    { "$ref": "#/definitions/red-requires-redQuote" },
    { "$ref": "#/definitions/black-requires-blackQuote" }
  ],
  "required": ["colour"],
  "definitions": {
    "red-requires-redQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-red" } },
        { "required": ["redQuote"] }
      ]
    },
    "black-requires-blackQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-black" } },
        { "required": ["blackQuote"] }
      ]
    },
    "is-red": {
      "properties": {
        "colour": { "enum": ["red"] }
      }
    },
    "is-black": {
      "properties": {
        "colour": { "enum": ["black"] }
      }
    }
  }
}



回答2:


Simplest answer in draft-04 (as noted by Ganesh in a comment):

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {

    "colour": {
      "type": "string",
      "enum": ["red", "black", "blue"]
    },

    "blackQuote": {
      "type": "string",
      "maxLength": 11
    },

    "redQuote": {
      "type": "string",
      "maxLength": 11
    }
  },

  "oneOf": [
      {
        "properties": {
          "colour": {"enum": ["red"]}
        },
        "required": ["redQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["black"]}
        },
        "required": ["blackQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["blue"]}
        }
      }
  ],

  "required": [
    "colour"
  ]
}



回答3:


If you move the specifics of what you want into the OneOf's then you can keep it all pretty simple, especially if you end up with a load of other values for color.

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "oneOf": [
        {
            "type": "object",
            "properties": {
                "colour": {
                    "type": "string",
                    "enum": [
                        "red"
                    ]
                },
                "redQuote": {
                    "type": "string",
                    "maxLength": 11
                }
            },
            "required": [
                "redQuote"
            ]
        },
        {
            "type": "object",
            "properties": {
                "colour": {
                    "type": "string",
                    "enum": [
                        "black"
                    ]
                },
                "blackQuote": {
                    "type": "string",
                    "maxLength": 11
                }
            },
            "required": [
                "blackQuote"
            ]
        },
        {
            "type": "object",
            "properties": {
                "colour": {
                    "type": "string",
                    "enum": [
                        "blue"
                    ]
                }
            }
        }
    ],
    "definitions": {}
}


来源:https://stackoverflow.com/questions/47649038/json-schema-conditional-dependency-on-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!