JSON 架构 |只允许一个对象将 boolean 属性 设置为 true
JSON Schema | Only allow one object to have boolean property set to true
我有 JSON 如下所示的数据:
{
"Plugins": {
"TestPlugin": {
"enabled": true,
"auth": {
"basic": {
"username": "MyUsername",
"password": "MyPassword",
"enabled": true
},
"oauth": {
"token": "MyToken",
"enabled": false
}
}
},
"AnotherTestPlugin": {
"enabled": true,
"auth": {
"basic": {
"username": "MySecondUsername",
"password": "MySecondPassword",
"enabled": true
},
"oauth": {
"token": "MySecondToken",
"enabled": true
}
}
}
}
}
当前应该用于验证的架构(2020-12 版)如下所示:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"Plugins": {
"type": "object",
"properties": {
"TestPlugin": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"auth": {
"type": "object",
"properties": {
"basic": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"username",
"password",
"enabled"
]
},
"oauth": {
"type": "object",
"properties": {
"token": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"token",
"enabled"
]
}
},
"required": [
"basic",
"oauth"
]
}
},
"required": [
"enabled",
"auth"
]
},
"AnotherTestPlugin": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"auth": {
"type": "object",
"properties": {
"basic": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"username",
"password",
"enabled"
]
},
"oauth": {
"type": "object",
"properties": {
"token": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"token",
"enabled"
]
}
},
"required": [
"basic",
"oauth"
]
}
},
"required": [
"enabled",
"auth"
]
}
}
}
},
"required": [
"Plugins"
]
}
如您所见,每个插件都包含一个 auth
对象。 auth
对象包含相应插件支持的可用身份验证方式。每种身份验证方法都可以是 enabled/disabled 以及必须存在且包含布尔值的相应 enabled
属性。
重要的是,对于每个插件,只启用一种可用的身份验证方法(布尔值:true),必须通过将 enabled
属性 设置为 false
.
我查看了 JSON 架构规范,但无法真正弄清楚如何实现这一点。
提前致谢!
It is important that for each plugin, only one of the available authentication methods is enabled (boolean: true), the other ones must be disabled by having the enabled property set to false.
您可以在 draft2020-12 中使用 oneOf 属性 执行此操作:在伪代码中,它将是“其中之一:-基本身份验证已启用=true,其他身份验证已启用=false;-誓言auth 已启用=true;...”即:
"oneOf": [
{
"properties": {
"basic": {
"properties": {
"enabled": { "const": true }
}
}
},
"additionalProperties": {
"properties": {
"enabled": { "const": false }
}
}
},
... same, but for the other auth types
]
您也可以使用 if/then/else 子句来做同样的事情,如果您发现它更具可读性:在伪代码中,那将是“allOf: [if basic has enabled=true, then oauth has enabled= false 并且启用=false;如果oauth 启用=true 那么...]"
如果身份验证类型可以是任何类型,那么它就比较棘手,因为您不能在 oneOf 子句中逐条列出名称,但在下一个草案中可能会出现,其中“包含”关键字被扩展为还包括对象。那时,你可以说“auth 包含一个 enabled=true 的对象,并且最多可以有一个”(即 maxContains=1)。
我有 JSON 如下所示的数据:
{
"Plugins": {
"TestPlugin": {
"enabled": true,
"auth": {
"basic": {
"username": "MyUsername",
"password": "MyPassword",
"enabled": true
},
"oauth": {
"token": "MyToken",
"enabled": false
}
}
},
"AnotherTestPlugin": {
"enabled": true,
"auth": {
"basic": {
"username": "MySecondUsername",
"password": "MySecondPassword",
"enabled": true
},
"oauth": {
"token": "MySecondToken",
"enabled": true
}
}
}
}
}
当前应该用于验证的架构(2020-12 版)如下所示:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"Plugins": {
"type": "object",
"properties": {
"TestPlugin": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"auth": {
"type": "object",
"properties": {
"basic": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"username",
"password",
"enabled"
]
},
"oauth": {
"type": "object",
"properties": {
"token": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"token",
"enabled"
]
}
},
"required": [
"basic",
"oauth"
]
}
},
"required": [
"enabled",
"auth"
]
},
"AnotherTestPlugin": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"auth": {
"type": "object",
"properties": {
"basic": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"username",
"password",
"enabled"
]
},
"oauth": {
"type": "object",
"properties": {
"token": {
"type": "string"
},
"enabled": {
"type": "boolean"
}
},
"required": [
"token",
"enabled"
]
}
},
"required": [
"basic",
"oauth"
]
}
},
"required": [
"enabled",
"auth"
]
}
}
}
},
"required": [
"Plugins"
]
}
如您所见,每个插件都包含一个 auth
对象。 auth
对象包含相应插件支持的可用身份验证方式。每种身份验证方法都可以是 enabled/disabled 以及必须存在且包含布尔值的相应 enabled
属性。
重要的是,对于每个插件,只启用一种可用的身份验证方法(布尔值:true),必须通过将 enabled
属性 设置为 false
.
我查看了 JSON 架构规范,但无法真正弄清楚如何实现这一点。
提前致谢!
It is important that for each plugin, only one of the available authentication methods is enabled (boolean: true), the other ones must be disabled by having the enabled property set to false.
您可以在 draft2020-12 中使用 oneOf 属性 执行此操作:在伪代码中,它将是“其中之一:-基本身份验证已启用=true,其他身份验证已启用=false;-誓言auth 已启用=true;...”即:
"oneOf": [
{
"properties": {
"basic": {
"properties": {
"enabled": { "const": true }
}
}
},
"additionalProperties": {
"properties": {
"enabled": { "const": false }
}
}
},
... same, but for the other auth types
]
您也可以使用 if/then/else 子句来做同样的事情,如果您发现它更具可读性:在伪代码中,那将是“allOf: [if basic has enabled=true, then oauth has enabled= false 并且启用=false;如果oauth 启用=true 那么...]"
如果身份验证类型可以是任何类型,那么它就比较棘手,因为您不能在 oneOf 子句中逐条列出名称,但在下一个草案中可能会出现,其中“包含”关键字被扩展为还包括对象。那时,你可以说“auth 包含一个 enabled=true 的对象,并且最多可以有一个”(即 maxContains=1)。