JSON 用于验证数组内多个相似对象的架构
JSON Schema to validate multiple similar objects inside an array
如果我希望数组的元素(都是对象)都遵循相同的模式,我会在 JSON 模式中使用什么关键字?
示例:
"data":
[
{ //validated
"id": 1,
"name": "Bob",
"ready": "Not Ready"
},
{ //validated
"id": 2,
"name": "Steve",
"ready": "Ready"
},
{ //not validated, missing "ready"
"id": 3,
"name": "Ted"
}
]
将"data"对象指定为数组类型,并在每一项中指明所需的元素。
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"ready": {
"type": "string"
}
},
"required": [
"id",
"name",
"ready"
]
}
}
},
"required": [
"data"
]
}
如果我希望数组的元素(都是对象)都遵循相同的模式,我会在 JSON 模式中使用什么关键字?
示例:
"data":
[
{ //validated
"id": 1,
"name": "Bob",
"ready": "Not Ready"
},
{ //validated
"id": 2,
"name": "Steve",
"ready": "Ready"
},
{ //not validated, missing "ready"
"id": 3,
"name": "Ted"
}
]
将"data"对象指定为数组类型,并在每一项中指明所需的元素。
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"ready": {
"type": "string"
}
},
"required": [
"id",
"name",
"ready"
]
}
}
},
"required": [
"data"
]
}