当 JSON 模式的键未知时,如何将子模式应用于值?

How do I apply a subschema to an value when the key is unknown with JSON Schema?

这里有点困惑,我正在尝试构建一个模式以在我的 python 应用程序中使用,但我无法弄清楚如何将这个 "we" 字段用于既是必需的又包含随机字符串(例如:"QWERT1")

{
  "we": [
    {
      "finished": "01.23.2020 12:56:31",
      "run": "02611",
      "scenarios": [
        {
          "name": "name",
          "status": "failed",
          "run_id": "42",
          "tests": [
            {
              "test_id": "7",
              "name": "TC29",
              "status": "success",
              "finished": "01.23.2020 12:56:31"
            }
          ]
        }
      ]
    }
  ]
}

其余字段也应为必填字段(姓名、状态等)。如果我从必填字段中排除 "we",则其余字段将被视为非强制字段,如果我将 "we" 添加为必填字段,则我不能再使用任何其他词:/ 这是我最终得到的模式("we" 强制):

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "we": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "finished": {
              "type": "string"
            },
            "run": {
              "type": "string"
            },
            "scenarios": {
              "type": "array",
              "items": [
                {
                  "type": "object",
                  "properties": {
                    "name": {
                      "type": "string"
                    },
                    "status": {
                      "type": "string"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "tests": {
                      "type": "array",
                      "items": [
                        {
                          "type": "object",
                          "properties": {
                            "test_id": {
                              "type": "string"
                            },
                            "name": {
                              "type": "string"
                            },
                            "status": {
                              "type": "string"
                            },
                            "finished": {
                              "type": "string"
                            }
                          },
                          "required": [
                            "test_id",
                            "name",
                            "status",
                            "finished"
                          ]
                        }
                      ]
                    }
                  },
                  "required": [
                    "name",
                    "status",
                    "run_id",
                    "tests"
                  ]
                }
              ]
            }
          },
          "required": [
            "finished",
            "run",
            "scenarios"
          ]
        }
      ]
    }
  },
  "required": [
    "we"
  ]
}

有什么想法吗?

如果我没理解错的话,根对象键可以是任何字符串。

首先,您需要将 required 替换为 minProperties: 1。如果你只需要1个属性,你还需要maxProperties: 1.

接下来,您需要使用 additionalProperties 而不是 properties > we

additionalProperties 将值子模式应用于 JSON 实例位置对象中的所有 属性 值。

这是该架构的裸版...

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "minProperties": 1,
  "additionalProperties": {}
}

您可以在此处使用您的架构和实例对其进行测试:https://jsonschema.dev/s/2kE9y