【问题标题】:How to write "required" for "pattern properties" of json schema如何为json模式的“模式属性”编写“必需”
【发布时间】:2019-09-20 18:00:48
【问题描述】:

我有这个 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
                        ]
                    }
                }
            }
        }
    }
}

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

【问题讨论】:

  • 我很困惑这与python 有什么关系?什么代码生成了这个json 或者它是如何动态的?

标签: python jsonschema


【解决方案1】:

你想要minProperties关键字https://json-schema.org/understanding-json-schema/reference/object.html#size

例如,

{
  "type": "object",
  "patternProperties": {
    "[A-Za-z:0-9]": { "type": "number" }
  },
  "minProperties": 1
}

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 2012-06-04
    • 2021-02-19
    相关资源
    最近更新 更多