如何在递归json模式中使用引用模式OR字符串



我想为以下递归json对象定义模式:

{
"options": [
{
"mode": "A",
"values": [
{
"mode": "B",
"values": ["hello?"]
},        
]
}
]
}

我使用以下模式,但是我不确定如何指定";类型";CCD_ 1阵列的"可以是CCD_ 2CCD_。

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"type": "object",
"properties": {
"options": {
"type": "array",
"items": {
"$ref": "#/definitions/option"
}
}
},
"required": [
"options"
],
"definitions": {
"option": {
"type": "object",
"properties": {
"mode": {
"type": "string"
},
"values": {
"type": [
"array"
],
"items": {
"$ref": "#/definitions/option"
}
}
},
"required": [
"mode",
"values"
]
}
}
}

实际上,我想做这样的事情:

"option": {
"type": "object",
"properties": {
"mode": {
"type": "string"
},
"values": {
"type": [
"array"
],
"items": {
"type": ["string", {"$ref": "#/definitions/option"}]
}
}
},
"required": [
"mode",
"values"
]
}

正如@jason desrosiers所指出的,我可以使用anyOf来实现这一点

...
{
"properties":
{
"mode":
{
"type": "string"
},
"values":
{
"type": ["array"],
"items":
{
"anyOf": [{"$ref": "#/definitions/option"},{"type": "string"}]
}
}
}
}
...

最新更新