Невозможно проверить дочернюю json-схему

Я работаю над такой схемой json:

{
  "$schema": "http://json-schema.org/schema#",
  "title": "Layout",
  "description": "The layout created by the user",
  "type": "object",
  "definitions": {
    "stdAttribute": {
      "type": "object",
      "required": ["attributeName","attributeValue"],
      "properties": {
        "attributeValue": {
          "type": "string"
        },
        "attributeName": {
          "type": "string"
        }
      }
    },
    "stdItem": {
      "type": "object",
      "required" : ["stdType","stdAttributes"],
      "properties": {
        "stdType": {
          "enum": [
            "CONTAINER",
            "TEXT",
            "TEXTAREA",
            "BUTTON",
            "LABEL",
            "IMAGE",
            "MARCIMAGE",
            "DATA",
            "SELECT",
            "TABLE"
          ]
        },
        "stdAttributes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdAttribute"
          },
          "minItems": 1
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdItem"
          }
        }
      }
    }
  },
  "properties":{
    "stdItem":{ "$ref": "#/definitions/stdItem" }
  }
}

Я пытаюсь проверить следующий json с помощью приведенной выше схемы:

{
  "stdItem": {
    "stdType": "CONTAINER",
    "stdAttributes": [
      {
        "attributeName": "ola",
        "attributeValue": "teste"
      }
    ],
    "children": [
      {
        "stdItem": {
          "stdType": "TEXT",
          "stdAttributes": [
            {
              "attributeName": "ola",
              "attributeValue": "teste"
            }
          ],
          "children": []
        }
      }
    ]
  }
}

Я получаю сообщение об ошибке, сообщающее мне, что требуемые атрибуты stdType и stdAttributes отсутствуют для пути stdItem / children / 0. Как видите, атрибуты есть, они не пропущены.

Я попытался изменить порядок атрибутов, но все равно не работает. Я получаю следующую ошибку:

--- НАЧАЛЬНЫЕ СООБЩЕНИЯ --- ошибка: у объекта отсутствуют необходимые свойства (["stdAttributes", "stdType"]) level: "error" schema: {"loadingURI": "#", "pointer": "/ definitions / stdItem "} экземпляр: {" указатель ":" / stdItem / children / 0 "} домен:" проверка "ключевое слово:" требуется "требуется: [" stdAttributes "," stdType "] отсутствует: [" stdAttributes "," stdType " ] --- КОНЕЦ СООБЩЕНИЙ ---

Может ли кто-нибудь указать мне на то, что я делаю неправильно?


person Jeyvison    schedule 21.03.2017    source источник


Ответы (1)


Когда вы объявляете свойство «children», вы говорите, что это «stdItem», поэтому он ожидает, что там будут свойства stdAttributes и stdType. Вместо этого в вашем json есть свойство stdItem типа stdItem. Итак, в вашей схеме отсутствует объявление этого свойства (stdItem).

Эта схема проверит ваш json:

{
  "$schema": "http://json-schema.org/schema#",
  "title": "Layout",
  "description": "The layout created by the user",
  "type": "object",
  "definitions": {
    "stdAttribute": {
      "type": "object",
      "required": ["attributeName","attributeValue"],
      "properties": {
        "attributeValue": {
          "type": "string"
        },
        "attributeName": {
          "type": "string"
        }
      }
    },
    "stdItem": {
      "type": "object",
      "required" : ["stdType","stdAttributes"],
      "properties": {
        "stdType": {
          "enum": [
            "CONTAINER",
            "TEXT",
            "TEXTAREA",
            "BUTTON",
            "LABEL",
            "IMAGE",
            "MARCIMAGE",
            "DATA",
            "SELECT",
            "TABLE"
          ]
        },
        "stdAttributes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdAttribute"
          },
          "minItems": 1
        },
        "children": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "stdItem": { "$ref": "#/definitions/stdItem" }
            }
          }
        }
      }
    }
  },
  "properties":{
    "stdItem": { "$ref": "#/definitions/stdItem" }
  }    
}

Обратите внимание, что я добавляю объект в спецификацию item для «дочерних элементов», у которого есть свойство «stdItem». (Я не заявлял, что это необходимо, но вы можете добавить это)

person Pedro    schedule 21.03.2017