繁体   English   中英

$ref 的 JSONSchema 验证失败(草案 v7)

[英]JSONSchema validation failure with $ref (Draft v7)

我已经按照 v7 规范草案创建了一个 JSON 模式。 架构如下所示:

{
  "type": "object",

  "properties": {

        "songs": {
            "type": "array",
            "items": {
                "type": "object",      
                "properties": {

                    "composition": {
                        "type": "object",

                        "properties": {
                            "title": {
                                "type": "string"
                            },
                            "publishing": {
                                "type": "array",

                                "items": {
                                    "type": "object",
                                    "required": ["publisherId","territory"],

                                    "definitions": {
                                        "categoryList": {
                                            "type": "object",
                                            "properties": {
                                                "BR": {
                                                "type": "number"
                                                }
                                            }
                                        }
                                    },
                                    "properties": {
                                        "publisherId": {
                                            "type": "integer"
                                        },
                                        "territory": {
                                            "$ref": "#/definitions/categoryList"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "recordings": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "songVersion": {
                                    "type": "string"
                                },
                                "album": {
                                    "type": "object",
                                    "properties": {
                                        "title": {
                                        "type": "string"
                                        }                    
                                    }
                                }                  
                            }              
                        }         
                    }

                }

            }
        }
    }
}

但出现错误无法解析架构引用“#/definitions/categoryList”。 路径 'properties.songs.items.properties.composition.properties.publishing.items.properties.territory',第 40 行,位置 24。如果我省略了定义部分,它可以完美运行

解释为什么参考解析没有按预期工作的最简单方法是讨论草案 07 核心规范本身的修改示例。

   {
       "definitions": {
           "A": { },
           "B": {
               "definitions": {
                   "X": { },
                   "Y": { }
               }
           }
       }
   }

文档根是一个具有definitions属性的对象。

要访问#/definitions/A ,您可以使用#/definitions/A的引用。

要访问#/definitions/B/definitions/X ,您可以使用#/definitions/B/definitions/X的引用。

架构中的引用需要知道从文档根目录到子架构的完整路径。

我猜你已经假设 URI 是相对于最近的子模式或它所使用的子模式的,但事实并非如此。

参考: https : //tools.ietf.org/html/draft-handrews-json-schema-01#section-8.2.4

该示例包括一组更详尽的示例。 将 URI 引用视为 HTML 中的锚标记。 当不包含 URI 的域部分时,会在其位置“想象”一个以进行 URI 解析。

如果您有任何后续问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM