[英]Combining properties from two JSON schema objects
I am writing some JSON schemas that have some objects with shared properties. 我正在写一些具有共享属性的对象的JSON模式。
in JSON schema 1: 在JSON模式1中:
...
"properties": {
"someProperty": {
"type": "object",
"required": [
"propertyA",
"propertyB",
"propertyC",
],
"additionalProperties": false,
"properties": {
"propertyA": {
"type": "integer"
},
"propertyB": {
"type": "integer"
},
"propertyC": {
"type": "integer"
}
}
}
}
...
in JSON schema 2: 在JSON模式2中:
...
"properties": {
"someProperty": {
"type": "object",
"required": [
"propertyD",
"propertyB",
"propertyC",
],
"additionalProperties": false,
"properties": {
"propertyD": {
"type": "integer"
},
"propertyB": {
"type": "integer"
},
"propertyC": {
"type": "integer"
}
}
}
}
...
So the only difference between those two objects ( someProperty
) is the propertyA
vs. propertyD
. 因此,这两个对象(
someProperty
)之间的唯一区别是propertyA
与propertyD
。
Therefore, I decided to make a common JSON schema that will contain a definition for those two shared properties, propertyB
and propertyC
. 因此,我决定制作一个通用的JSON模式,其中将包含这两个共享属性
propertyB
和propertyC
。
One solution was to have a definition of an object in the common schema that would contain those two properties, but I wanted my JSON to have those three properties at the same level (flat, unwrapped), and not like this: 一种解决方案是在通用模式中定义一个对象,该对象将包含这两个属性,但是我希望我的JSON具有这三个属性处于同一级别(平坦,未包装),而不是这样:
"someProperty": {
"propertyA": 2,
"sharedObject": {
"propertyB": 3,
"propertyC": 4
}
}
Is what I want possible? 我想要什么? I want to end up with something that would enable my JSON to be valid like this:
我想结束的事情将使我的JSON像这样有效:
"someProperty": {
"propertyA": 2,
"propertyB": 3,
"propertyC": 4
}
and: 和:
"someProperty": {
"propertyD": 2,
"propertyB": 3,
"propertyC": 4
}
With all of these properties as required
and no additionalProperties
allowed. 具有所有
required
的这些属性,并且不允许additionalProperties
属性。
You can achieve this with oneOf
. 您可以使用
oneOf
实现。 oneOf
is an exclusive OR statement. oneOf
是互斥的OR语句。 One or the other of "propertyA" or "propertyD" and not both can be present at the same time. “ propertyA”或“ propertyD”中的一个或另一个可以同时存在。
{
"type": "object",
"properties": {
"someProperty": {
"type": "object",
"properties": {
"propertyA": {
"type": "integer"
},
"propertyB": {
"type": "integer"
},
"propertyC": {
"type": "integer"
},
"propertyD": {
"type": "integer"
}
},
"required": ["propertyB", "propertyC"],
"oneOf": [
{ "required": ["propertyA"] },
{ "required": ["propertyD"] }
],
"additionalProperties": false
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.