0

我按照 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 位。如果我省略定义部分,它会完美运行

4

1 回答 1

0

解释为什么参考解决方案不能按您期望的那样工作的最简单方法是讨论草案 07 核心规范本身的修改示例。

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

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

要访问#/definitions/A,您可以使用#/definitions/A.

要访问#/definitions/B/definitions/X,您可以使用#/definitions/B/definitions/X.

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

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

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

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

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

于 2020-03-23T10:06:30.523 回答