使用tv4.js在json架构草案4中嵌套“oneOf”问题

时间:2014-01-30 11:54:23

标签: jsonschema

我正在使用tv4.js来针对模式验证某些json(它具有嵌套的oneOf属性)但是当我使用有效数据时它会返回错误。这是我从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的错误? 是否有其他验证器可以在Web浏览器中进行验证?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:4)

works for me(使用"试用tv4"演示)。

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

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