是否可以验证下面的json数据,以便" info"只有在" name"是" a"否则它必须为空或为空?
[
{
"name": "a",
"info": "this is mandatory"
},
{
"name": "b",
"info": "validation must fail"
}
]
JSONSchema
{
"title": "Array of things",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": [
"a",
"b"
]
},
"info": {
"type": "string"
}
}
}
}
中的json
答案 0 :(得分:2)
试试这个:
{
"title": "Array of things",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": ["a", "b"]
},
"info" : {
"type": ["string", "null"]
}
},
"required": ["name"],
"oneOf": [
{
"properties": {
"name": {"enum": ["a"] }
},
"required": ["info"]
},
{
"properties": {
"name": {"enum": ["b"] },
"info": {"enum": [null]}
}
}
]
}
}