Схема JSON — установка статических имен

Мой пример JSON, который я пытаюсь преобразовать в схему:

{
          "nodeID": "5f9f5dbe0c3ab520c2b44bb0",
          "type": "block",
          "coords": [
            517.2814214277396,
            769.7697271579176
          ],
          "data": {
            "name": "Block",
            "color": "standard",
            "steps": [
              "5f9f5dbe0c3ab520c2b44bb1"
            ]
          }
        }

и я преобразовал его в эту схему

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "nodeID": {
      "type": "string"
    },
    "type": {
      "type": "string"
    },
    "data": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "intent": {
          "type": "string"
        },
        "diagramID": {
          "type": "string"
        },
        "mappings": {
          "type": "array",
          "items": {}
        },
        "next": {
          "type": "null"
        },
        "ports": {
          "type": "array",
          "items": {}
        }
      },
      "required": [
        "name",
        "intent",
        "diagramID",
        "mappings",
        "next",
        "ports"
      ]
    }
  },
  "required": [
    "nodeID",
    "type",
    "data"
  ]
}

В примере JSON вы можете увидеть тип = блок. Как в схеме я могу убедиться, что при проверке JSON обязательно проверяется, что ключ типа == block?

Спасибо! :)


person darkrai    schedule 24.02.2021    source источник


Ответы (1)


Вам нужно использовать перечисление для https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "nodeID": {
      "type": "string"
    },
    "type": {
      "type": "string",
      "enum": ["block"]
    },
    "data": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "intent": {
          "type": "string"
        },
        "diagramID": {
          "type": "string"
        },
        "mappings": {
          "type": "array",
          "items": {}
        },
        "next": {
          "type": "null"
        },
        "ports": {
          "type": "array",
          "items": {}
        }
      },
      "required": [
        "name",
        "intent",
        "diagramID",
        "mappings",
        "next",
        "ports"
      ]
    }
  },
  "required": [
    "nodeID",
    "type",
    "data"
  ]
}
person spacether    schedule 24.02.2021
comment
Или в более поздних версиях схемы JSON одно значение перечисления также можно указать с помощью const. - person Ether; 24.02.2021