如何在JSON模式中指定一个属性或另一个属性,但并非两者?

发布于 2025-02-07 17:42:55 字数 703 浏览 2 评论 0原文

我有4个属性:A,B,C和D。

我想拥有A或B。其中一个必须存在。为此,我创建了可以正常工作的模式。

  "oneOf": [
    {
      "required": ["A"]
    },
    {
      "required": ["B"]
    }
  ],

现在,我想包含C和D,但从某种意义上说我要么有C或D,但并非两者兼而有之。 我既不能拥有。换句话说,如果我要包含C,那么我将不包括D,反之亦然。但是,我也可以决定不包括其中任何一个。

我的file.json

{
  "A": "Prop",
  "C": "Prop"
}

{
  "B": "Prop",
  "D": "Prop"
}

{
  "B": "Prop"
}

在我的文件中类似的东西不应允许

您不能在一起a和b。

{
  "A": "Prop",
  "B": "Prop"
}

您不能同时组合C和D。但是,如上所述,您也不必拥有其中之一。

{
  "C": "Prop",
  "D": "Prop"
}

I have 4 properties: A, B, C, and D.

I'd like to have either A or B. One of them must exist. For this I created this schema that works fine.

  "oneOf": [
    {
      "required": ["A"]
    },
    {
      "required": ["B"]
    }
  ],

Now, I'd like to have C and D included but in a sense that I either have C or D but not both. And I can have neither. In other words, if I'm going to include C, then I can't include D and vice versa. However, I can also decide not to include either of them.

Something like this in my file.json

{
  "A": "Prop",
  "C": "Prop"
}

OR

{
  "B": "Prop",
  "D": "Prop"
}

OR

{
  "B": "Prop"
}

The following should not be allowed

You can't have A and B together.

{
  "A": "Prop",
  "B": "Prop"
}

You can't have both C and D together. But, as mentioned above, you don't have to have one of them either.

{
  "C": "Prop",
  "D": "Prop"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里泪两行 2025-02-14 17:42:55

我只是对JSON模式非常熟悉。但是,假设您可以嵌套亚表壳,只需逻辑地写出它,然后将其转换为布尔代数。然后XOR是单一的,或者是任何人,并且不是Allof,也不是。

您希望其中一个或多个是真实的:

  1. A或B,但不是
  2. C或D,但根本不是
  3. 一无所有。或者,“什么都没有”。

那就是:

(A XOR B) OR (C XOR D) OR NOT (A OR B OR C OR D)

是:

anyOf(
    oneOf([A], [B]),
    oneOf([C], [D]),
    not(anyOf([A],[B],[C],[D]))
)

I'm only passingly familiar with JSON schema. But assuming you can nest subschema, just write it out logically and then translate it to Boolean algebra. Then XOR is oneOf, OR is anyOf, AND is allOf, and NOT is not.

You want at one or more of these to be true:

  1. A or B but not both
  2. C or D but not both
  3. Nothing at all. Or, "not anything".

That's:

(A XOR B) OR (C XOR D) OR NOT (A OR B OR C OR D)

That's:

anyOf(
    oneOf([A], [B]),
    oneOf([C], [D]),
    not(anyOf([A],[B],[C],[D]))
)
止于盛夏 2025-02-14 17:42:55

我最终这样做:

  "oneOf": [
    {
      "required": ["A"],
      "dependencies": {
        "C": {
          "not": {
            "required": ["D"]
          }
        },
        "D": {
          "not": {
            "required": ["C"]
          }
        }
      }
    },
    {
      "required": ["B"],
      "dependencies": {
        "C": {
          "not": {
            "required": ["D"]
          }
        },
        "D": {
          "not": {
            "required": ["C"]
          }
        }
      }
    }
  ],

I ended up doing this:

  "oneOf": [
    {
      "required": ["A"],
      "dependencies": {
        "C": {
          "not": {
            "required": ["D"]
          }
        },
        "D": {
          "not": {
            "required": ["C"]
          }
        }
      }
    },
    {
      "required": ["B"],
      "dependencies": {
        "C": {
          "not": {
            "required": ["D"]
          }
        },
        "D": {
          "not": {
            "required": ["C"]
          }
        }
      }
    }
  ],
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文