Python jsonschema,不允许任何内容,或者只需要两个字段之一

Python jsonschema, allowing nothing, or require one and only one of two fields

我正在为 Python 使用 jsonschema,我正在尝试允许以下任何一项:

{}
# or
{
    'id': 'd6a4def3-4a6a-4fb4-a38e-f98ea48f708a'
}
# or
{
    'status': 'Done'
}

但我不想允许以下内容,因为提供了两个字段:

{
    'id': 'd6a4def3-4a6a-4fb4-a38e-f98ea48f708a',
    'status': 'Done'
}

这是我目前所拥有的,它允许按照我的意愿提供一个或另一个字段,但它不允许任何内容。

GET_CALL = {
    "type": "object",
    "additionalProperties": False,
    "properties": {
        "id": {
            "type": "string",
            "pattern": REGEX_EXPRESSIONS['UUID4'], # Matches a UUID4 format
        },
        "status": {
            "type": "string",
            "enum": ["Done", "Ready"],
        },
    },
    "oneOf": [ # This makes it so I can only have one or the other but it doesn't allow nothing.
        {"required": ["id"]},
        {"required": ["status"]}
    ],
}

您需要 oneOf 来应对所有情况。幸运的是这并不太复杂。

这是您指定的架构所需的架构。您可以将其应用到现有架构中。

{
  "oneOf": [
    {
      "additionalProperties": false
    },
    {
      "required": [
        "id"
      ]
    },
    {
      "required": [
        "status"
      ]
    }
  ]
}

(草稿 7 JSON 架构)

您可以在 jsonschema.dev 对其进行测试(link 预加载了此模式和测试实例)

为了澄清,您仍然需要 type: object