问题描述
我正在使用 tv4.js 来针对模式(已嵌套oneOf属性)验证一些json ),但当我使用有效数据时会返回错误.这是我从tv4.js validateMultiple方法获得的结果对象:
I'm using tv4.js to validate some json against a schema (which has nested oneOf properties) but it returns errors when I am using valid data. Here is the result object I get back from the tv4.js validateMultiple method:
{"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"
}
}
}
}
}
这是我希望验证的数据:
And here is the data I would expect to validate:
{
"shape": {
"thetype": "circle",
"shade":"red"
}
}
我似乎仅在使用嵌套的"oneOf"时遇到此问题.这是我的架构问题吗?还是tv4.js的错误?是否有其他替代验证器将在Web浏览器中执行此验证?
I seem to only encounter this issue when using nested "oneOf".Is this an issue with my schema? Or a bug with tv4.js?Are there any alternative validators which will do this validation within a web browser?
任何帮助将不胜感激.
推荐答案
为我工作(使用试用tv4演示).
It works for me (using the "Try out tv4" demo).
通常,如果您认为自己已经提出建议,通常建议您在 GitHub存储库上提交问题发现错误.但是,错误输出包含schemaKey
的事实使我认为您使用的是相当旧的版本.
Normally, I'd suggest you file a issue on the GitHub repo if you think you've found an error. However, the fact that the error output includes schemaKey
makes me think you're using a fairly old version.
您是否正在使用tv4的最新版本?
Are you using an up-to-date version of tv4?
这篇关于嵌套的"oneOf"使用tv4.js的JSON模式草案4中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!