【发布时间】: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