Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当JSON模式中的键未知时,如何将子模式应用于值?_Python_Json_Data Structures_Jsonschema - Fatal编程技术网

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

Python 当JSON模式中的键未知时,如何将子模式应用于值?,python,json,data-structures,jsonschema,Python,Json,Data Structures,Jsonschema,这里有一个小问题,我正在尝试构建一个在我的python应用程序中使用的模式,但我不知道如何使这个we字段既是必需的,又包含一个随机字符串ex:QWERT1 { "we": [ { "finished": "01.23.2020 12:56:31", "run": "02611", "scenarios": [ { "name": "name", "status": "failed",

这里有一个小问题,我正在尝试构建一个在我的python应用程序中使用的模式,但我不知道如何使这个we字段既是必需的,又包含一个随机字符串ex: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添加为必填字段,则不能在此处使用任何其他词语:/ 这是我的模式,我们必须:

{
  "$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": {}
}

您可以在这里用您的模式和实例来测试它:

如果您使用draft-7 JSON模式,这将更容易构建。你有什么理由必须使用draft-4吗?没有,一点也没有。它可以是Draft7。你已经展示了we字段是一个数组。包含随机字符串是什么意思?我们不能既是数组又是字符串。请提供您希望通过验证的数据和您希望验证失败的数据的示例,并解释每个示例的原因?当然,抱歉,如果不清楚-我确实理解它可以是一个或另一个我的意思是我希望这个数组对象名称是一个随机字符串,而不是我现在得到的固定字符串,例如,这个数组可以命名为qwert1或其他任何东西,这个数组也应该标记为仍然需要,所以我们实际上可以是任何东西?好啊