如何确保两个布尔属性的值绝不会同时为真?

How to ensure value of both boolean properties should never be true simultaneously?

鲍勃先生可以高兴也可以悲伤,但不能两者兼而有之。他可能没有具体说明他的心情。因此,悲伤和快乐这两个属性都是可选的。

仅拒绝以下 json

的 json 模式是什么
{
  "name": "Bob",
  "sad": true,
  "happy": true
}

这是失败的尝试:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "happy": {
      "type": "boolean",
      "default": false
    },
    "sad": {
      "type": "boolean",
      "default": false
    }
  },
  "not": {
    "allOf": [
      {
        "properties": {
          "happy": {
            "enum": [
              true
            ]
          }
        }
      },
      {
        "properties": {
          "sad": {
            "enum": [
              true
            ]
          }
        }
      }
    ]
  }
}

如果 属性 "sad" 为真且 属性 "happy" 缺失,则上述架构失败。

你很接近。我所做的只是简化你所拥有的并添加 required.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "happy": {
      "type": "boolean",
      "default": false
    },
    "sad": {
      "type": "boolean",
      "default": false
    }
  },
  "not": {
    "properties": {
      "happy": { "enum": [true] },
      "sad": { "enum": [true] }
    },
    "required": ["happy", "sad"]
  }
}