Json 架构 - oneOf 与通配符路径的引用?
Json Schema - oneOf with ref to wildcard path?
我想组织我的架构的定义部分以将许多类似的定义分组。在下面的示例中,我有一个 "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/*"
}
有什么办法吗?
没有。
引用使用 json-pointer.
此外,即使可以,也不能将$ref
用作oneOf
的子级,因为oneOf
的值必须是数组。
根据 JSON 架构规范,$ref
只能用于替代子架构,而不能只用于 JSON 架构中您喜欢的任何位置。
您可以创建一个新的定义,像这样...
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ipv4": {...},
"ipv6": {...}
"ipAddress": {
"oneOf": [{
"$ref": "#/definitions/ipv4"
},{
"$ref": "#/definitions/ipv6"
}]
}
}
"properties": {
"anIPAddress": {
"$ref": "#/definitions/ipAddress"
}
}
}
另请注意,您的定义不能有多个图层。如果想进一步分离定义,你可以在多个文件中这样做。
我想组织我的架构的定义部分以将许多类似的定义分组。在下面的示例中,我有一个 "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/*"
}
有什么办法吗?
没有。 引用使用 json-pointer.
此外,即使可以,也不能将$ref
用作oneOf
的子级,因为oneOf
的值必须是数组。
$ref
只能用于替代子架构,而不能只用于 JSON 架构中您喜欢的任何位置。
您可以创建一个新的定义,像这样...
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ipv4": {...},
"ipv6": {...}
"ipAddress": {
"oneOf": [{
"$ref": "#/definitions/ipv4"
},{
"$ref": "#/definitions/ipv6"
}]
}
}
"properties": {
"anIPAddress": {
"$ref": "#/definitions/ipAddress"
}
}
}
另请注意,您的定义不能有多个图层。如果想进一步分离定义,你可以在多个文件中这样做。