【问题标题】:nested "oneOf" issue in json schema draft 4 using tv4.js使用 tv4.js 在 json 模式草案 4 中嵌套“oneOf”问题
【发布时间】:2014-01-30 11:54:23
【问题描述】:

我正在使用tv4.js 来针对模式(其中嵌套了 oneOf 属性)验证一些 json,但是当我使用有效数据时它会返回错误。这是我从 tv4.js validateMultiple 方法返回的结果对象:

{"valid":false,"errors":[{"code":11,"message":"Data does not match any schemas from \"oneOf\"","schemaKey":null,"dataPath":"/shape","subErrors":[{"code":302,"message":"Missing required property: boxname","schemaKey":null,"dataPath":"/shape","subErrors":null},{"code":1,"message":"No enum match for: \"circle\"","schemaKey":null,"dataPath":"/shape/thetype","subErrors":null},{"code":12,"message":"Data is valid against more than one schema from \"oneOf\": indices 0 and 1","schemaKey":null,"dataPath":"/shape","subErrors":null}]}],"missing":[]}

这是我的测试架构:

{
    "type": "object",
    "properties": {
        "shape": {
            "oneOf": [
                { "$ref":"#/definitions/squareSchema" },
                { "$ref":"#/definitions/circleSchema" }
            ]
        }
    },
    "definitions": {
        "squareSchema": {
            "type": "object",
            "properties": {
                "thetype": {
                    "type": "string",
                    "enum": ["square"]
                },
                "colour":{},
                "shade":{},
                "boxname": {
                    "type":"string"
                }
            },
            "oneOf":[
                { "$ref":"#/definitions/colourSchema" },
                { "$ref":"#/definitions/shadeSchema" }
            ],
            "required": ["thetype", "boxname"],
            "additionalProperties":false
        },
        "circleSchema": {
            "type": "object",
            "properties": {
                "thetype": {
                    "type": "string",
                    "enum":["circle"]
                },
                "colour":{},
                "shade":{}
            },
            "oneOf":[
                { "$ref":"#/definitions/colourSchema" },
                { "$ref":"#/definitions/shadeSchema" }
            ],
            "additionalProperties":false
        },
        "colourSchema":{
            "type":"object",
            "properties":{
                "colour":{
                    "type":"string"
                },
                "shade":{
                    "type":"null"
                }
            }
        },
        "shadeSchema":{
            "type":"object",
            "properties":{
                "shade":{
                    "type":"string"
                },
                "colour":{
                    "type":"null"
                }
            }
        }
    }
}

这是我希望验证的数据:

{
    "shape": {
        "thetype": "circle",
        "shade":"red"
    }
}

我似乎只在使用嵌套的“oneOf”时遇到这个问题。 这是我的架构的问题吗?还是 tv4.js 的错误? 是否有任何替代验证器可以在网络浏览器中进行此验证?

任何帮助将不胜感激。

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    works for me(使用“Try out tv4”演示)。

    通常,如果您认为自己发现了错误,我建议您在 GitHub repo 上提出问题。但是,错误输出包含 schemaKey 的事实让我认为您使用的是相当旧的版本。

    您使用的是最新版本的 tv4 吗?

    【讨论】:

    • 我使用的是来自 github 的最新版本。但你帮我弄清楚发生了什么。它在您使用“validate”或“validateResult”方法时有效,但在使用“validateMultiple”方法时无效。谢谢您的帮助!!!我会尽快将其标记为正确:)
    • 奇怪,因为“试用 tv4”演示使用validateMultiple()。如果 validateMultiple() 失败,但 validate() 通过,这是一个 严重 错误,您应该在 GitHub 存储库中提出。 (作为 tv4 的作者,我将不胜感激:p)
    • 好的就行。除了这个堆栈问题中的问题外,tv4 一直运行良好。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 2020-01-30
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多