JSONSchema验证失败,带有$ref(草案v7)

2024-05-29 05:58:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我根据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"
                                        }                    
                                    }
                                }                  
                            }              
                        }         
                    }

                }

            }
        }
    }
}

但Get error无法解析架构引用“#/definitions/categoryList”。路径“properties.songs.items.properties.composition.properties.publishing.items.properties.territory”,第40行,位置24。如果我省略了定义部分,它就完美地工作了


Tags: stringobjecttitle架构typeitemspropertiesarray
1条回答
网友
1楼 · 发布于 2024-05-29 05:58:45

解释为什么参考解析不能像您预期的那样工作的最简单方法是讨论draft-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解析,“想象”一个域部分就在它的位置上

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

相关问题 更多 >

    热门问题