JSON模式对值的条件依赖

wes*_*eyy 3 json jsonschema

我知道这里有一个类似的问题,但是并没有真正解决我的问题。简而言之,我希望我的一个字段依赖于另一个字段的值。但是对于某些值,我不要求任何字段。这是一个例子:

架构图

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是这样的:

  • 如果颜色是“红色”,则需要“ redQuote”(而不是“ blackQuote”):很好
  • 如果颜色是“黑色”,则需要“ blackQuote”(而不是“ redQuote”):也可以
  • 但是如果我在JSON中放入颜色“ blue”,则验证程序会说缺少“ redQuote”和“ blackQuote”属性...我不希望那样,我只希望依赖于“ red”和“ black” ”,但是如果颜色是“蓝色”,则不需要任何颜色。如何实现呢?

Spr*_*tty 5

如果你将你想要的细节移到 OneOf 中,那么你可以保持它非常简单,特别是如果你最终得到了大量其他颜色值。

在此处输入图片说明

{
    "$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": {}
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*ers 5

您可以使用称为蕴含(!A或B)的布尔逻辑概念来完成此操作。它可以像“ if-then”语句一样使用。例如,“ color”不是“ red”或“ redQuote”是必需的。每当我需要使用它时,我都会对其进行分解,definitions以使其读得尽可能好。

{
  "$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"] }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

草案 04 中最简单的答案(如 Ganesh 在评论中指出的):

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)