【问题标题】:Does JSON Schema validation in common-js utils support references?common-js utils 中的 JSON Schema 验证是否支持引用?
【发布时间】:2012-01-25 14:52:31
【问题描述】:

common-js utils 中的 JSON Schema 验证是否支持引用?我不能用 https://github.com/kriszyp/commonjs-utils/blob/master/json-schema.js 做到这一点

我尝试了以下代码:

{
  "type" : "object",
  "required" : true,
  "properties" : {
    "id" : {
      "type" : "number",
      "required" : true
    },
    "related" : {
      "type" : "array",
      "required" : true,
      "items" : {"$ref": "$#"}
    }
  }
}

我可以在没有引用的情况下继续准备我的架构,但很高兴知道这是否可能。

【问题讨论】:

    标签: json commonjs jsonschema


    【解决方案1】:

    我不知道,但是您的 ref 似乎是错误的,请删除“$”符号,即:

    "items" : {"$ref": "#"}
    

    这是我在 json 论坛上发布的关于参考的复制粘贴帖子:

    根据草案 03,这里是我对指定 id 并从任何地方引用它的理解。如果有问题,请发表评论。 案例写在下面: 一种。我从中检索架构的 uri 湾。模式得到了 C。如何引用此架构

    1. A schema without any "id" property
    I fetch :   http://someSite.com/somePath
    I get : { }
    I can ref it :  {"$ref":"http://someSite.com/somePath#"}
    
    2. Same absolute id and uri path
    I fetch :   http://someSite.com/somePath
    I get : {"id":"http://someSite.com/somePath#"}
    I can ref it :  {"$ref":"http://someSite.com/somePath#"}
    
    3. Different absolute id and path
    I fetch :   http://someSite.com/somePath
    I get : {"id":"http://anotherSite.com/anotherPath#"}
    I can ref it :  {"$ref":"http://anotherSite.com/anotherPath#"}
    
    4. Relative - a fragment
    I fetch :   http://someSite.com/somePath
    I get : {"id":"#something"}
    I can ref it :  {"$ref":"http://someSite.com/somePath#something"}
    
    5. Relative path and fragment
    I fetch :   http://someSite.com/somePath
    I get : {"id":"/oneMore/path#something"}
    I can ref it :  {"$ref":"http://someSite.com/somePath/oneMore/path#something"}
    
    6. Relative path from file
    I fetch :   file:///someFolder/someFile
    I get : {"id":"/oneMore/path#something"}
    I can ref it :  {"$ref":"file:///someFolder/someFile/oneMore/path#something"}
    
    7. Inner schema (sub schema) with id "#subschema" (no id for the main schema)
    I fetch :   http://someSite.com/somePath
    I get : {"properties" : { "aKeyName" : { "id":"#subschema" }}}
    I can ref it :  {"$ref":"http://someSite.com/somePath#subschema"}
    
    8. Inner schema (sub schema) with id "#subschema" 
    I fetch :   http://someSite.com/somePath
    I get : { {"id":"#mainSchema"}, "properties" : { "aKeyName" : { "id":"#subschema" }} }
    I can ref it :  {"$ref":"http://someSite.com/somePath#subschema"}
    I can also do:   {"$ref":"http://someSite.com/somePath#mainSchema/properties/aKeyName"}
    
    9. Inner schema (within the property under the "aKeyName") but no id at all 
    I fetch :   http://someSite.com/somePath
    I get : { "properties" : { "aKeyName" : { }} }
    can ref it:  {"$ref":"http://someSite.com/somePath#/properties/aKeyName"}
    
    10. Inner schema (sub schema) with a main custom absolute uri" 
    I fetch :   http://someSite.com/somePath
    I get : { {"id":"scheme://something"}, "properties" : { "aKeyName" : { "id":"#subschema" }} }
    I can ref it :  {"$ref":"scheme://something#subschema"}
    I can also do:   {"$ref":"scheme://something#/properties/aKeyName"}
    
    11. Inner schema  with it's own absolute uri" 
    I fetch :   http://someSite.com/somePath
    I get : { {"id":"#main"}, "properties" : { "aKeyName" : { "id":"http://domain.com/subSchema" }} }
    I can ref it :  {"$ref":"http://domain.com/subSchema#"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多