2015-12-27 111 views
0

當試圖驗證使用http://www.jsonschemavalidator.net/下面的架構,JSONSchema - 錯誤解決方案參考

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "id": "http://test.com/unified-ingest", 
    "type": "object", 
    "definitions" : { 

    "test-params" : { 
     "id": "http://test.com/test-params", 
     "type": "object", 
     "properties" : { 

     "operation-type": { 
      "id" : "http://test.com/test-params/operation-type", 
      "enum": [ "create", "update" ] 
     }, 

     "required" : { 
      "id" : "http://test.com/test-params/required", 
      "type" : "array", 
      "items" : { 
      "type" : "string", 
      "pattern" : "^[\\w,\\s-\\p{L}\\p{M}]+\\.(jpg|png|xml)$" 
      } 
     } 

     }, 
     "required" : ["operation-type"] 
    } 

    }, 

    "properties": { 

    "root" : { 
     "id": "http://test.com/unified-ingest/root", 
     "type": "object", 
     "properties" : { 

     "$" : { 
      "id" : "http://test.com/unified-ingest/root/attributes", 
      "type" : "object", 
      "properties" : { 
      "xmlns:xsi" : { 
       "type" : "string" 
      }, 
      "xmlns" : { 
       "type" : "string" 
      }, 
      "xsi:schemaLocation" : { 
       "type" : "string", 
       "pattern" : "^[a-z]*$" 
      } 
      } 
     }, 

     "test-params" : { 
      "$ref" : "#/definitions/test-params" 
     } 

     }, 
     "required" : ["test-params"] 
    } 

    }, 
    "required" : ["root"] 
} 

我收到以下錯誤時:

Error when resolving schema reference '#/definitions/test-params'. Path 'properties.root.properties.test-params', line 56, position 25. 

我不知道是什麼導致這錯誤。我多次嘗試掃描文檔,但對於我的生活,我無法弄清楚什麼是錯的。我甚至嘗試刪除id屬性,認爲它引用了參考文獻,但這也沒有幫助。請幫忙!

回答

1

您的問題是您的root對象中定義的idThe documentation指出以下,約id

But be aware of the second purpose of the id property: that it declares a base URL for relative $ref URLs elsewhere in the file.

所以,你要麼必須從root對象中刪除id所以你$ref指當前模式(如下),或確保該架構在id引用確實包含了test-params的定義。

你的模式就變成了:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "id": "http://test.com/unified-ingest", 
    "type": "object", 
    "definitions" : { 

    "test-params" : { 
     "id": "http://test.com/test-params", 
     "type": "object", 
     "properties" : { 

     "operation-type": { 
      "id" : "http://test.com/test-params/operation-type", 
      "enum": [ "create", "update" ] 
     }, 

     "required" : { 
      "id" : "http://test.com/test-params/required", 
      "type" : "array", 
      "items" : { 
      "type" : "string", 
      "pattern" : "^[\\w,\\s-\\p{L}\\p{M}]+\\.(jpg|png|xml)$" 
      } 
     } 

     }, 
     "required" : ["operation-type"] 
    } 

    }, 

    "properties": { 

    "root" : { 
     "type": "object", 
     "properties" : { 

     "$" : { 
      "id" : "http://test.com/unified-ingest/root/attributes", 
      "type" : "object", 
      "properties" : { 
      "xmlns:xsi" : { 
       "type" : "string" 
      }, 
      "xmlns" : { 
       "type" : "string" 
      }, 
      "xsi:schemaLocation" : { 
       "type" : "string", 
       "pattern" : "^[a-z]*$" 
      } 
      } 
     }, 

     "test-params" : { 
      "$ref" : "#/definitions/test-params" 
     } 

     }, 
     "required" : ["test-params"] 
    } 

    }, 
    "required" : ["root"] 
} 

...這是有效的。

+1

完美的作品!謝謝您的幫助。 – user1452030