$ ref(Draft v7)导致JSONSchema验证失败

时间:2020-03-20 15:40:45

标签: python jsonschema json-schema-validator python-jsonschema

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

1 个答案:

答案 0 :(得分: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解析。

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