首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法验证子程序json-schema

无法验证子程序json-schema
EN

Stack Overflow用户
提问于 2017-03-21 17:45:07
回答 1查看 717关注 0票数 1

我正在编写类似于这样的json模式:

代码语言:javascript
复制
{
  "$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:

代码语言:javascript
复制
{
  "stdItem": {
    "stdType": "CONTAINER",
    "stdAttributes": [
      {
        "attributeName": "ola",
        "attributeValue": "teste"
      }
    ],
    "children": [
      {
        "stdItem": {
          "stdType": "TEXT",
          "stdAttributes": [
            {
              "attributeName": "ola",
              "attributeValue": "teste"
            }
          ],
          "children": []
        }
      }
    ]
  }
}

我收到一个错误,告诉我路径stdItem/children/0缺少require属性、stdType、stdAttributes。正如您可以看到的那样,属性是存在的,它们并不缺少。

我试图更改属性的顺序,但仍然不起作用。我一直收到以下错误:

-开始消息-错误:对象缺少必需的属性("stdAttributes","stdType")级别:“错误”架构:{"loadingURI":"#",“指针”:“/loadingURI/stdItem”}实例:{“指针”:“/stdItem/stdType/0”}域:“验证”关键字:“必需”:"stdAttributes","stdType“缺失:"stdAttributes","stdType“--结束消息

有人能指出我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-21 19:40:33

当您声明“子”属性时,您说它是"stdItem“,因此它希望在那里具有stdAttributes和stdType属性。相反,json中有一个" stdItem“属性,它是stdItem类型的。因此,您在模式中缺少了该属性(stdItem)的声明。

此架构将验证您的json:

代码语言:javascript
复制
{
  "$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" }
  }    
}

注意,我正在向具有"stdItem“属性的”子“项规范中添加一个对象。(我没有按要求声明,但您可能需要添加)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42934529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档