2017-02-03 78 views
1

我试图创建以下列方式为JSON模式:JSON模式要求节点

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

我确认以下JSON反对:

{ 
    "layoutItem": { 
    "stdItem": { 
     "stdType": "CONTAINER", 
     "stdAttributes": [], 
     "children": [] 
    } 
    } 
} 

问题是当我运行java验证程序时出现错误,因为我按照“stdItem”的要求指定了“stdAtrributes”节点,验证程序无法对其进行鉴别。

我试图在属性中定义所需的数组,但模式被取消。

如果我把 “stdAttributes” 外 “stdItem”,它的工作原理。

有谁知道我该如何定义这个要求“stdItem”?

回答

0

您的模式中的问题是,您希望layoutItem的值根据模式#/definitions/stdItem有效。

但这种模式不符合,只要你想(查看数据)一个stdItem属性来定义一个对象,它定义了带属性stdTypestdAttributeschildren对象,并要求物业stdAttributes存在。换句话说,它是下列数据的模式:

{ 
    "layoutItem": { 
    "stdType": "CONTAINER", 
    "stdAttributes": [], 
    "children": [] 
    } 
} 

为您的数据的模式应该是:

{ 
    ... 
    "definitions": { 
    ... 
    "stdItem": { 
     "type": "object", 
     "required" : ["stdItem"], 
     "properties": { 
     "stdItem": { 
      "type": "object", 
      "required" : ["stdAttributes"], 
      "properties": { 
      "stdType": { ... }, 
      "stdAttributes": { ... }, 
      "children": { ... } 
      } 
     } 
     } 
    } 
    }, 
    ... 
} 
+0

它的工作原理!非常感谢你。 – Jeyvison