在子条件下验证对象 属性
Validate object property on sub condition
我编写了以下 JSON 模式验证多个设备组件的目标图像版本,包含多个组件的活动版本和非活动版本,即 foo
、bar
。
我如何防止例如如果组件名称是 component2
,它只包含 active
属性 而不需要 inactive
属性?
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "config.json",
"type": "object",
"title": "Config parameters",
"required": [
"Versions"
],
"properties": {
"Versions": {
"$id": "#/properties/Versions",
"type": "array",
"minItems": 1,
"additionalItems": false,
"items": {
"$id": "#/properties/Versions/items",
"title": "Version Info",
"description": "Version information",
"type": "object",
"properties": {
"foo": {
"$ref": "#/properties/Versions/items/definitions/image-info"
},
"bar": {
"$ref": "#/properties/Versions/items/definitions/image-info"
}
},
"definitions": {
"image-info": {
"type": "object",
"properties": {
"component1": {
"$ref": "#/properties/Versions/items/definitions/image-settings"
},
"component2": {
"$ref": "#/properties/Versions/items/definitions/image-settings"
}
},
"additionalProperties": false
},
"image-settings": {
"type": "object",
"properties": {
"active": {
"$ref": "#/properties/Versions/items/definitions/image-version"
},
"inactive": {
"$ref": "#/properties/Versions/items/definitions/image-version"
}
},
"additionalProperties": false
},
"image-version": {
"type": "string",
"pattern": "^20[0-9]{2}-[01][0-9]-[0-3][0-9]-[0-9]{2}$"
}
}
}
}
},
"additionalProperties": false
}
我希望以下 JSON 被视为无效,因为 component2
包含 inactive
属性
{
"Versions": [
{
"foo": {
"component1": {
"active": "2021-11-15-01",
"inactive": "2021-11-15-00"
}
}
},
{
"bar": {
"component2": {
"active": "2021-11-15-03",
"inactive": "2021-11-15-04"
}
}
}
]
}
但我希望以下内容被视为有效 JSON
{
"Versions": [
{
"foo": {
"component1": {
"active": "2021-11-15-01",
"inactive": "2021-11-15-00"
}
}
},
{
"bar": {
"component2": {
"active": "2021-11-15-03"
}
}
}
]
}
假设您不想修改 image-setting
的定义,并希望尽可能避免重复,您可以修改 component2
的定义以禁止 inactive
使用 not
关键字。
当您使用 JSON 架构的参考和 draft-07 时,您需要将该参考包含在 allOf
中。 (如果您使用 2019-09
或更高版本,则不会,因为 $ref
现在可以与其他关键字一起使用。)
{
"allOf": [
{
"$ref": "#/properties/Versions/items/definitions/image-settings"
}, {
"not": {
"required": ["inactive"]
}
}
]
}
你可以看到它在这里工作:https://jsonschema.dev/s/EIGp1
我编写了以下 JSON 模式验证多个设备组件的目标图像版本,包含多个组件的活动版本和非活动版本,即 foo
、bar
。
我如何防止例如如果组件名称是 component2
,它只包含 active
属性 而不需要 inactive
属性?
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "config.json",
"type": "object",
"title": "Config parameters",
"required": [
"Versions"
],
"properties": {
"Versions": {
"$id": "#/properties/Versions",
"type": "array",
"minItems": 1,
"additionalItems": false,
"items": {
"$id": "#/properties/Versions/items",
"title": "Version Info",
"description": "Version information",
"type": "object",
"properties": {
"foo": {
"$ref": "#/properties/Versions/items/definitions/image-info"
},
"bar": {
"$ref": "#/properties/Versions/items/definitions/image-info"
}
},
"definitions": {
"image-info": {
"type": "object",
"properties": {
"component1": {
"$ref": "#/properties/Versions/items/definitions/image-settings"
},
"component2": {
"$ref": "#/properties/Versions/items/definitions/image-settings"
}
},
"additionalProperties": false
},
"image-settings": {
"type": "object",
"properties": {
"active": {
"$ref": "#/properties/Versions/items/definitions/image-version"
},
"inactive": {
"$ref": "#/properties/Versions/items/definitions/image-version"
}
},
"additionalProperties": false
},
"image-version": {
"type": "string",
"pattern": "^20[0-9]{2}-[01][0-9]-[0-3][0-9]-[0-9]{2}$"
}
}
}
}
},
"additionalProperties": false
}
我希望以下 JSON 被视为无效,因为 component2
包含 inactive
属性
{
"Versions": [
{
"foo": {
"component1": {
"active": "2021-11-15-01",
"inactive": "2021-11-15-00"
}
}
},
{
"bar": {
"component2": {
"active": "2021-11-15-03",
"inactive": "2021-11-15-04"
}
}
}
]
}
但我希望以下内容被视为有效 JSON
{
"Versions": [
{
"foo": {
"component1": {
"active": "2021-11-15-01",
"inactive": "2021-11-15-00"
}
}
},
{
"bar": {
"component2": {
"active": "2021-11-15-03"
}
}
}
]
}
假设您不想修改 image-setting
的定义,并希望尽可能避免重复,您可以修改 component2
的定义以禁止 inactive
使用 not
关键字。
当您使用 JSON 架构的参考和 draft-07 时,您需要将该参考包含在 allOf
中。 (如果您使用 2019-09
或更高版本,则不会,因为 $ref
现在可以与其他关键字一起使用。)
{
"allOf": [
{
"$ref": "#/properties/Versions/items/definitions/image-settings"
}, {
"not": {
"required": ["inactive"]
}
}
]
}
你可以看到它在这里工作:https://jsonschema.dev/s/EIGp1