验证具有不同属性的json对象



我有一个json对象,它的属性值是唯一的,可以是任何值;

{
"cat1": {
"name": "kitty",
"type": "animal",
"color": "ginger"
},
"dog2": {
"name": "ripple",
"type": "animal",
"color": "black"
},
"book10": {
"name": "myBook",
"type": "book",
"color": "NA"
},
"orange6": {
"name": "NA",
"type": "fruit",
"color": "orange"
},
"pig1":{
"name": "spring",
"type": "animal",
"color": "pink"
}
}

现在我很困惑如何编写它的验证模式。有人知道怎么做吗?

var mySchema = {
"type": "object",
"properties": {
// no idea how to check varying properties like cat1, dog2, etc. which might change next time
}
}

你可以试试这个

var mySchema = {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"name": { "type": "string"},
"type": { "type": "string"},
"color": { "type": "string"},
}
}
}

ref:JSONSchema如何定义动态对象的模式

相关内容

  • 没有找到相关文章

最新更新