下面是我的JSON模式的摘录。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"images": {
"type": "array",
"items": { "$ref": "#/definitions/bits" },
}
},
"definitions": {
"identifier": {
"type": "string"
},
"bits": {
"type": "integer",
"enum": [
8,
16,
32
]
}
}
}
如前所述,我相信一个图像数组,其中每个元素由一个字符串标识符和一个值可以是 8、16 或 32 的整数组成,将被视为有效的 JSON 数据。
这对于我的一些 JSON 数据来说很好。
但是,如果我想进一步约束架构,使整数值只能为 32,该怎么办?在仍然允许某些 JSON 数据对原始架构有效的情况下,我将如何做到这一点?
例如,是否可以在一个对象中引用两个模式,例如:
items": { "$ref": "#/definitions/bits" AND "$ref": "#/definitions/otherSchema"}
您可以使用
allOf
针对多个架构进行验证。
{
"items": {
"allOf": [
{ "$ref": "#/definitions/bits" },
{ "$ref": "#/definitions/otherSchema" }
]
}
}