2016-01-24 120 views
0

当试图验证使用http://www.jsonschemavalidator.net/下面的架构,JSONSchema V4 - 解决方案引用时错误 - 定义和引用

{ 
    "id": "http://some.site.somewhere/entry-schema#", 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "schema for the FormularSpecification", 
    "definitions": { 
     "elementId": { 
      "id": "http://jsonschema.net/elementId", 
      "type": "string" 
     }, 
     "mappingKey": { 
      "id": "http://jsonschema.net/mappingKey", 
      "type": "string" 
     }, 
     "elementType": { 
      "id": "http://jsonschema.net/elementType", 
      "type": "string" 
     }, 
     "length": { 
      "id": "http://jsonschema.net/length", 
      "type": "integer" 
     }, 
     "label": { 
      "id": "http://jsonschema.net/label", 
      "type": "string" 
     }, 
     "content": { 
      "id": "http://jsonschema.net/content", 
      "type": "string" 
     }, 
     "placeholder": { 
      "id": "http://jsonschema.net/placeholder", 
      "type": "string" 
     }, 
     "date": { 
      "id": "http://jsonschema.net/date", 
      "type": "string" 
     }, 
     "option": { 
      "id": "http://jsonschema.net/option", 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" }, 
       "label": { "$ref": "#/definitions/label" } 
      }, 
      "required": ["elementId", "label"] 
     }, 
     "options": { 
      "id": "http://jsonschema.net/options", 
      "type": "array", 
      "items": { "$ref": "#/definitions/option" }, 
      "minItems": 1, 
      "uniqueItems": true 
     }, 
     "textfield": { 
      "id": "http://jsonschema.net/textfield", 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" }, 
       "length": { "$ref": "#/definitions/length" }, 
       "label": { "$ref": "#/definitions/label" }, 
       "placeholder": { "$ref": "#/definitions/placeholder" }, 
       "textfieldType": { 
        "enum": [ "text", "ext4", "btrfs" ] 
       } 
      }, 
      "required": ["elementId", "length", "label", "placeholder", "textfieldType"] 
     }, 
     "checkbox": { 
      "id": "http://jsonschema.net/checkbox", 
      "type": "object", 
      "properties": { 
       "label": { "$ref": "#/definitions/label" } 
      }, 
      "required": ["label"] 
     }, 
     "radio": { 
      "id": "http://jsonschema.net/radio", 
      "type": "object", 
      "properties": { 
       "label": { "$ref": "#/definitions/label" }, 
       "options": { "$ref": "#/definitions/options" } 
      }, 
      "required": ["label", "options"] 
     }, 
     "dropdown": { 
      "id": "http://jsonschema.net/dropdown", 
      "type": "object", 
      "properties": { 
       "label": { "$ref": "#/definitions/label" }, 
       "options": { "$ref": "#/definitions/options" } 
      }, 
      "required": ["label", "options"] 
     }, 
     "validator": { 
      "id": "http://jsonschema.net/validator", 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" } 
      } 
     }, 
     "validators": { 
      "id": "http://jsonschema.net/validators", 
      "type": "array", 
      "items": { "$ref": "#/definitions/validator" } 
     }, 
     "interactiveDetails": { 
      "type": "object", 
      "oneOf": [ 
       { "textfield": { "$ref": "#/definitions/textfield" } }, 
       { "checkbox": { "$ref": "#/definitions/checkbox" } }, 
       { "radio": { "$ref": "#/definitions/radio" } }, 
       { "dropdown": { "$ref": "#/definitions/dropdown" } }, 
       { "date": { "$ref": "#/definitions/date" } } 
      ] 
     }, 
     "interactive": { 
      "id": "http://jsonschema.net/interactive", 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" }, 
       "elementType": { "$ref": "#/definitions/elementType" }, 
       "mappingKey": { "$ref": "#/definitions/mappingKey" }, 
       "validators": { "$ref": "#/definitions/validators" }, 
       "interactiveDetails" : { "$ref": "#/definitions/interactiveDetails" } 
      }, 
      "required": ["elementId", "elementType", "mappingKey", "validators"] 
     }, 
     "interactives": { 
      "id": "http://jsonschema.net/interactives", 
      "type": "array", 
      "items": { "$ref": "#/definitions/interactive" } 
     }, 
     "description": { 
      "id": "http://jsonschema.net/description", 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" }, 
       "elementType": { "$ref": "#/definitions/elementType" }, 
       "content": { "$ref": "#/definitions/content" } 
      }, 
      "required": ["elementId", "elementType", "content"] 
     }, 
     "descriptions": { 
      "items": { "$ref": "#/definitions/description" } 
     }, 
     "children": { 
      "items": { 
       "anyOf": [ 
        { "$ref": "#/definitions/group" }, 
        { "$ref": "#/definitions/question" } 
       ] 
      }, 
      "minItems": 1 
     }, 
     "question": { 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" }, 
       "descriptions": { "$ref": "#/definitions/descriptions" }, 
       "interactives": { "$ref": "#/definitions/interactives" } 
      }, 
      "required": ["elementId", "descriptions", "interactives"] 
     }, 
     "group": { 
      "type": "object", 
      "properties": { 
       "elementId": { "$ref": "#/definitions/elementId" }, 
       "descriptions": { "$ref": "#/definitions/descriptions" }, 
       "children": { "$ref": "#/definitions/children"} 
      }, 
      "required": ["elementId", "descriptions", "children"] 
     } 
    }, 
    "type": "object", 
    "properties": { 
     "elementId": { "$ref": "#/definitions/elementId" }, 
     "description": { "$ref": "#/definitions/descriptions" }, 
     "children": { "$ref": "#/definitions/children" } 
    }, 
    "required": [ 
     "elementId", 
     "descriptions", 
     "children" 
    ] 
} 

,我发现了以下错误:

Error when resolving schema reference '#/definitions/elementId'. Path 'definitions.description.properties.elementId', line 135, position 30. 

我想不通解决问题是什么。我多次扫描文档并查看了教程,但我没有任何线索。

回答

1

id关键字的语义有点混乱。我不确定自己完全理解它。一般来说,将id包括在您的模式的根目录以外的地方几乎不是一个好主意。

The "id" keyword (or "id", for short) is used to alter the resolution scope. When an id is encountered, an implementation MUST resolve this id against the most immediate parent scope. The resolved URI will be the new resolution scope for this subschema and all its children, until another id is encountered.

考虑以下摘自你的模式。由于您包含关键字id,因此您的“elementId”和“label”$ref不像您期望的那样根据文档根目录解析,而是从最近的父模式id解析。

"option": { 
    "id": "http://jsonschema.net/option", 
    "type": "object", 
    "properties": { 
     "elementId": { "$ref": "#/definitions/elementId" }, 
     "label": { "$ref": "#/definitions/label" } 
    }, 
    "required": ["elementId", "label"], 
    "definitions": { ... } <-- your $refs expect values here 
    } 
}, 

我已经看到,在某些圈子里,人们写有id S代表每个子模式模式。我不确定他们认为他们从中获得什么好处,但我怀疑他们认为id仅仅是一个标签,并不了解它如何改变解析范围。

如果您确实有充分的理由在任何地方使用id并希望将其留在,您可以在发生冲突时明确引用根id

{ "$ref": "http://some.site.somewhere/entry-schema#definitions/elementId" } 
+0

仅供参考,我创建了一个新的答案,而不是编辑原件,因为它是一个完全不同的答案,而不是更新。 – Jason

+0

非常感谢!现在根据http://www.jsonschemavalidator.net/,JSON-Schema是有效的。 – Ahnde

相关问题