【发布时间】:2016-10-03 15:18:42
【问题描述】:
我有这个 json 模式,它有一个包含多个对象的数组,每个对象根据某些模式与其他对象略有不同。
示例。
[
{
"ID": "pgID",
"Name": "John",
"Surname": "Doe",
"ProjectsInvolved": [
"My Project",
"My Project 2"
]
},
{
"ID": "jtID",
"Name": "John",
"Surname": "Doe",
"WorksOn": [
"Monday",
"Thursday"
]
}
]
相应的 json 架构是:
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "array",
"items": {
"oneOf": [
{
"type": "object",
"properties": {
"ID": {
"type": "string",
"pattern": "^(pg)\\w*$"
},
"Name": {
"type": "string"
},
"Surname": {
"type": "string"
},
"ProjectsInvolved": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
{
"type": "object",
"properties": {
"ID": {
"type": "string",
"pattern": "^(jt)\\w*$"
},
"Name": {
"type": "string"
},
"Surname": {
"type": "string"
},
"WorksOn": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
]
},
"additionalProperties": false
}
我的问题是,虽然真正的 json 是相似的,但它有更多的项目,并且随着时间的推移它会变得更大。因此我必须问,模式是否可以将相同的元素 Name 和 Surname 分组,并且只有 ID 和 oneOf 中的数组?
建议架构的示例:
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "array",
"items": {
"type": "object",
"properties": {
"Name": {
"type": "string"
},
"Surname": {
"type": "string"
},
"oneOf": [
{
"ID": {
"type": "string",
"pattern": "^(pg)\\w*$"
},
"ProjectsInvolved": {
"type": "array",
"items": {
"type": "string"
}
}
},
{
"ID": {
"type": "string",
"pattern": "^(jt)\\w*$"
},
"WorksOn": {
"type": "array",
"items": {
"type": "string"
}
}
}
]
}
},
"additionalProperties": false
}
【问题讨论】:
标签: json validation jsonschema