带有$ ref的JSONSchema验证失败(Draft v7)

问题描述 投票:0回答:1

我已按照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。如果我省略了定义部分,它将很好地工作

python jsonschema json-schema-validator python-jsonschema
1个回答
0
投票

解释为什么参考分辨率无法按您期望的那样工作的最简单方法是,从-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解析。

让我知道您是否有任何后续问题。

© www.soinside.com 2019 - 2024. All rights reserved.