创建 JSON 架构值依赖项
Create JSON Schema value dependency
我有一个 JSON 模式,我想通过 JSON 结构的 值 之一的依赖项来更改。例如,如果 {"foo":1}
还在架构中包含 {"fooBar":"number"}
,则结果为 {"foo":"number", "fooBar":"number"}
,但如果 {"foo":2}
改为包含 {"fooBar2":"bool", "fooBar3":"string"}
,则结果为 {"foo":1, "fooBar2":"bool", "fooBar3":"string"}
。这可能吗
我知道如何使包含的键更改模式(here 中的代码示例),但我找不到任何示例说明如何使用值来完成此操作。如果这甚至可能的话。
{
"type": "object",
"properties": {
"name": { "type": "string" },
"credit_card": { "type": "number" },
"billing_address": { "type": "string" }
},
"required": ["name"],
"dependencies": {
"credit_card": ["billing_address"]
}
}
可以做,就是有点复杂。以下是一般模式。
{
"type": "object",
"anyOf": [
{
"properties": {
"foo": { "enum": [1] },
"fooBar": { "type": "number" }
}
},
{
"properties": {
"foo": { "enum": [2] },
"fooBar2": { "type": "boolean" },
"fooBar3": { "type": "string" }
}
}
]
}
我有一个 JSON 模式,我想通过 JSON 结构的 值 之一的依赖项来更改。例如,如果 {"foo":1}
还在架构中包含 {"fooBar":"number"}
,则结果为 {"foo":"number", "fooBar":"number"}
,但如果 {"foo":2}
改为包含 {"fooBar2":"bool", "fooBar3":"string"}
,则结果为 {"foo":1, "fooBar2":"bool", "fooBar3":"string"}
。这可能吗
我知道如何使包含的键更改模式(here 中的代码示例),但我找不到任何示例说明如何使用值来完成此操作。如果这甚至可能的话。
{
"type": "object",
"properties": {
"name": { "type": "string" },
"credit_card": { "type": "number" },
"billing_address": { "type": "string" }
},
"required": ["name"],
"dependencies": {
"credit_card": ["billing_address"]
}
}
可以做,就是有点复杂。以下是一般模式。
{
"type": "object",
"anyOf": [
{
"properties": {
"foo": { "enum": [1] },
"fooBar": { "type": "number" }
}
},
{
"properties": {
"foo": { "enum": [2] },
"fooBar2": { "type": "boolean" },
"fooBar3": { "type": "string" }
}
}
]
}