Как написать «требуется» для «свойств шаблона» схемы JSON - PullRequest
0 голосов
/ 20 сентября 2019

У меня есть вывод 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 голосов
/ 25 сентября 2019

Требуется ключевое слово minProperties https://json -schema.org / понимание-json-схема / reference / object.html # size

Например,

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