如何对 collection+json 对象进行 json 模式验证?
How to do json schema validation on collection+json objects?
我想验证 collection+json 具有在同一数组下具有不同格式的架构的对象。例如:
{
"href": "https://example.com/whatnot",
"data": [
{
"name": "foo",
"value": "xyz:123:456"
},
{
"name": "bar",
"value": "8K"
},
{
"name": "baz",
"value": false
}
]
}
这里,值是精确模式之一 (\w+:\d+:\d+),精确 ([\w\d]+) 之一,精确布尔值之一。没有其他变化。
json 架构中是否有任何方法可以根据这些要求检查此列表?
我睡了一夜,想出了如何制作 oneOf 模式。我尝试在"properties"里面使用,结果发现不行。对于完美的解决方案,我想我需要 "explicitOf" 种方法。但是,这已经足够了。
{
"type": "object",
"required": [
"name",
"value"
],
"oneOf": [
{
"properties":
{
"name":
{
"type": "string",
"pattern": "foo"
},
"value":
{
"type": "string",
"pattern": "^(\w+:\d+:\d+)$"
}
}
},
{
"properties":
{
"name":
{
"type": "string",
"pattern": "bar"
},
"value":
{
"type": "string",
"pattern": "^([\w\d]+)$"
}
}
},
{
"properties":
{
"name":
{
"type": "string",
"pattern": "baz"
},
"value":
{
"type": "boolean"
}
}
}
]
}
我想验证 collection+json 具有在同一数组下具有不同格式的架构的对象。例如:
{
"href": "https://example.com/whatnot",
"data": [
{
"name": "foo",
"value": "xyz:123:456"
},
{
"name": "bar",
"value": "8K"
},
{
"name": "baz",
"value": false
}
]
}
这里,值是精确模式之一 (\w+:\d+:\d+),精确 ([\w\d]+) 之一,精确布尔值之一。没有其他变化。
json 架构中是否有任何方法可以根据这些要求检查此列表?
我睡了一夜,想出了如何制作 oneOf 模式。我尝试在"properties"里面使用,结果发现不行。对于完美的解决方案,我想我需要 "explicitOf" 种方法。但是,这已经足够了。
{
"type": "object",
"required": [
"name",
"value"
],
"oneOf": [
{
"properties":
{
"name":
{
"type": "string",
"pattern": "foo"
},
"value":
{
"type": "string",
"pattern": "^(\w+:\d+:\d+)$"
}
}
},
{
"properties":
{
"name":
{
"type": "string",
"pattern": "bar"
},
"value":
{
"type": "string",
"pattern": "^([\w\d]+)$"
}
}
},
{
"properties":
{
"name":
{
"type": "string",
"pattern": "baz"
},
"value":
{
"type": "boolean"
}
}
}
]
}