如何为json模式的“模式属性”编写“必需”

时间:2019-09-20 18:00:48

标签: python jsonschema

我有这个json输出,我想确保top_properties不为空。
在top_properties中,键值是动态的,它们不是静态的。那就是我坚持的地方。

  {
  "id": "test",
  "name": "name",
  "cake_name": "test",
  "metric": 0.5,
  "anticipations": [
    {
      "time": "2018-01-01 00:00:00",
      "points": 0.49128797804879504,
      "top_properties": {
        "LA:TB2341": 0.23,
        "LA:TB2342": 0.23,
        "LA:TB2343": 0.23
      },
      "status": 0,
      "alert": false
    }

我具有以下架构,但是当top_properties为空时它不会失败。我想确保它为空时失败。

{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "required": [
        "id",
        "name",
        "cake_name",
        "metric",
        "anticipations"
    ],
    "properties": {
        "id": {
            "$id": "#/properties/id",
            "type": "string",
            "title": "The Id Schema",
            "default": "",
            "examples": [
                "test"
            ],
            "pattern": "^(.*)$"
        },
        "name": {
            "$id": "#/properties/name",
            "type": "string",
            "title": "The Name Schema",
            "default": "",
            "examples": [
                "name"
            ],
            "pattern": "^(.*)$"
        },
        "cake_name": {
            "$id": "#/properties/cake_name",
            "type": "string",
            "title": "The Cake_name Schema",
            "default": "",
            "examples": [
                "test"
            ],
            "pattern": "^(.*)$"
        },
        "metric": {
            "$id": "#/properties/metric",
            "type": "number",
            "title": "The Metric Schema",
            "default": 0.0,
            "examples": [
                0.5
            ]
        },
        "anticipations": {
            "$id": "#/properties/anticipations",
            "type": "array",
            "title": "The Anticipations Schema",
            "items": {
                "$id": "#/properties/anticipations/items",
                "type": "object",
                "title": "The Items Schema",
                "required": [
                    "time",
                    "points",
                    "top_properties",
                    "status",
                    "alert"
                ],
                "properties": {
                    "time": {
                        "$id": "#/properties/anticipations/items/properties/time",
                        "type": "string",
                        "title": "The Time Schema",
                        "default": "",
                        "examples": [
                            "2018-01-01 00:00:00"
                        ],
                        "pattern": "^(.*)$"
                    },
                    "points": {
                        "$id": "#/properties/anticipations/items/properties/points",
                        "type": "number",
                        "title": "The Points Schema",
                        "default": 0.0,
                        "examples": [
                            0.49128797804879504
                        ]
                    },
                    "top_properties": {
                        "$id": "#/properties/anticipations/items/properties/top_properties",
                        "type": "object",
                        "title": "The Top_properties Schema",
                        "patternProperties": {
                            "[A-Za-z:0-9]": {
                                "type": "number"
                            }
                        },
                        "additionalProperties": false
                    },
                    "status": {
                        "$id": "#/properties/anticipations/items/properties/status",
                        "type": "integer",
                        "title": "The Status Schema",
                        "default": 0,
                        "examples": [
                            0
                        ]
                    },
                    "alert": {
                        "$id": "#/properties/anticipations/items/properties/alert",
                        "type": "boolean",
                        "title": "The Alert Schema",
                        "default": false,
                        "examples": [
                            false
                        ]
                    }
                }
            }
        }
    }
}

由于我没有静态值,因此如何使用模式属性必需的值,如果遇到这种情况,如何实现它。

1 个答案:

答案 0 :(得分:0)

您想要from matplotlib import pyplot as plt import numpy as np sz=100 x = np.linspace(-1, 1, sz) mean = -np.sign(x) noise = np.random.randn(*x.shape) K = -2 y_true = K*x y = y_true + mean + noise plt.scatter(x, y, label="Data with error") plt.plot(x, y_true, "-", label="True line") plt.grid() 关键字https://json-schema.org/understanding-json-schema/reference/object.html#size

例如,

minProperties