我想组织我的架构的定义部分,以对许多相似的定义进行分组。在下面的示例中,我有一个“ ipAddress”组,该组具有“ ipv4”和“ ipv6”子定义。 (您可以想象为标识特定区域的ip范围添加更多的子定义,等等。)
{
"definitions": {
"generalType": {
"ipAddress": {
"ipv4": {
"type": "object",
"properties": {
"address": {
"type":"string",
"pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
},
"generalType": {
"type": "string",
"default": "ipAddress"
},
"specificType": {
"type": "string",
"default": "ipv4"
}
}
},
"ipv6": {
"type": "object",
"properties": {
"address": {
"type":"string",
"pattern": "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
},
"generalType": {
"type": "string",
"default": "ipAddress"
},
"specificType": {
"type": "string",
"default": "ipv6"
}
}
}
}
}
},
"type": "object",
"additionalProperties": false,
"required": ["ipAddress"],
"properties": {
"ipAddress": {
"oneOf": [{
"$ref": "#/definitions/generalType/ipAddress/ipv4"
},{
"$ref": "#/definitions/generalType/ipAddress/ipv6"
}]
}
}
}
我要避免做的是枚举"oneOf"
属性中的每个子定义。
换句话说,代替
"oneOf": [
{
"$ref": "#/definitions/generalType/ipAddress/ipv4"
},{
"$ref": "#/definitions/generalType/ipAddress/ipv6"
}
]
我宁愿
"oneOf": {
"$ref": "#/definitions/logicalType/ipAddress/*"
}
有什么办法吗?
答案 0 :(得分:0)
不。 引用使用json-pointer。
此外,即使可以,也不能将$ref
用作oneOf
的子级,因为oneOf
的值必须是一个数组。
$ref
仅可用于代替子架构,而不仅仅是JSON Schema中您喜欢的任何地方。
您可以像这样创建新的定义...
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ipv4": {...},
"ipv6": {...}
"ipAddress": {
"oneOf": [{
"$ref": "#/definitions/ipv4"
},{
"$ref": "#/definitions/ipv6"
}]
}
}
"properties": {
"anIPAddress": {
"$ref": "#/definitions/ipAddress"
}
}
}
还要注意,您的定义不能有多个层次。如果要进一步分离定义,可以将其分成多个文件。