【问题标题】:JSONSchema validation failure with $ref (Draft v3)JSONSchema 验证失败,$ref (Draft v3)
【发布时间】:2017-11-10 09:05:30
【问题描述】:

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

{
"$schema": "http://json-schema.org/draft-03/schema#",
"additionalProperties": false,
"type": "object",
"properties": {
    "ExecutionPlanList": {
        "type": "array",
        "items": [{
            "type": "object",
            "properties": {
                "model": {
                    "required": true,
                    "properties": {
                        "featureList": {
                            "required": true,
                            "items": {
                                "properties": {
                                    "featureName": {
                                        "type": ["string", "null"]
                                    },
                                    "featureType": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "type": "array"
                        },
                        "modelId": {
                            "required": true,
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "cascadeSteps": {
                    "required": false,
                    "items": {
                        "properties": {
                            "binaryModel": {
                                "$ref": "#/properties/ExecutionPlanList/items/properties/model",
                                "required": true
                            },
                            "threshold": {
                                "required": true,
                                "default": "0.0",
                                "maximum": 100.0,
                                "type": "number"
                            },
                            "backupModel": {
                                "$ref": "#/properties/ExecutionPlanList/items/properties/model",
                                "required": true
                            }
                        }
                    },
                    "type": "array"
                },
                "marketplaceId": {
                    "required": true,
                    "type": "integer"
                }
            }
        }]
    }
},
"required": true
}

本质上,executionPlanList 包含模型列表和 cascadeStep,每个 cascadeStep 包含两个带有编号的模型。因此,我尝试在 cascadeStep 中重新使用模型的架构,但验证 (https://www.jsonschemavalidator.net/) 因Could not resolve schema reference '#/properties/ExecutionPlanList/items/properties/model' 而失败。

希望能提供有关此架构有什么问题的任何指示。

【问题讨论】:

    标签: jsonschema json-schema-validator


    【解决方案1】:

    如何添加“定义”并像这样引用:

    {
    "$schema": "http://json-schema.org/draft-03/schema#",
    "additionalProperties": false,
    "type": "object",
    "definitions": {
          "model": {
              "required": true,
              "properties": {
                  "featureList": {
                      "required": true,
                      "items": {
                          "properties": {
                              "featureName": {
                                  "type": ["string", "null"]
                              },
                              "featureType": {
                                  "type": "string"
                              }
                          },
                          "type": "object"
                      },
                      "type": "array"
                  },
                  "modelId": {
                      "required": true,
                      "type": "string"
                  }
              },
              "type": "object"
          }
    },
    "properties": {
        "ExecutionPlanList": {
            "type": "array",
            "items": [{
                "type": "object",
                "properties": {
                    "model": {
                      "$ref" : "#/definitions/model"
                    },
                    "cascadeSteps": {
                        "required": false,
                        "items": {
                            "properties": {
                                "binaryModel": {
                                    "$ref" : "#/definitions/model",
                                    "required": true
                                },
                                "threshold": {
                                    "required": true,
                                    "default": "0.0",
                                    "maximum": 100.0,
                                    "type": "number"
                                },
                                "backupModel": {
                                    "$ref" : "#/definitions/model",
                                    "required": true
                                }
                            }
                        },
                        "type": "array"
                    },
                    "marketplaceId": {
                        "required": true,
                        "type": "integer"
                    }
                }
            }]
        }
    },
    "required": true
    }
    

    【讨论】:

    • 这很有帮助,谢谢,花了很长时间解决类似的问题,这就是解决方案。 “定义”不是特殊或保留字,但由于某种原因,jsonschema 不喜欢同一部分中的“$ref”,不确定确切的规则,但如果您指向的“$ref”似乎更喜欢它位于结构中的另一个对象中,具有不同的顶级键。
    【解决方案2】:

    应该是'#/properties/ExecutionPlanList/items/0/properties/model'

    顺便说一下,架构仅验证第一项。

    【讨论】:

    • 谢谢。您能否详细说明第二部分“仅验证第一项”
    • 由于“items”值是一个数组,因此那里的模式仅适用于数组中的第一项。如果这是有意的,那没关系,但这也是一个常见的错误......
    • 哦……那绝对不是我的本意。由于 v3 不支持字典,这里推荐的重用模式的方法是什么?
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2014-02-10
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多