如何在 json 模式中指示动态添加支持?
How to indicate dynamic additions support in json schema?
刚开始使用 json架构。我想描述一个 属性 的对象集合,其中密钥可能无法先验地知道。
这是我的起点:
"tx_properties": {
"type": "object",
"anyOf": [
{
"required": [
"original_msg"
]
}
],
"properties": {
"original_msg": {
"type": "string"
}
}
}
}
我希望能够验证为 tx_properties
添加的更多属性,这些属性可能具有不同的类型,但在架构定义时未知。
例如我可能在 json:
"tx_properties": {
"original_msg": "foo",
"something_else": "bar",
"or_something_numeric": 172,
"or_even_deeper_things": {
"fungible": false,
}
}
作为一个 n00b,我对如何完成这个有点困惑。使用 anyOne
是我认为至少在最终解决方案中需要的。
正如@Evert 所说,"additionalProperties": false
可用于指示除 properties
关键字(和 patternProperties
)中列出的属性外,不允许使用其他属性。如果 additionalProperties
被省略,行为就好像模式说 "additionalProperties": true
(即允许附加属性)。
另请注意,additionalProperties
处的值不必是布尔值:它本身可以是子模式,以允许您根据其值有条件地允许其他属性。
参考:https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties
刚开始使用 json架构。我想描述一个 属性 的对象集合,其中密钥可能无法先验地知道。
这是我的起点:
"tx_properties": {
"type": "object",
"anyOf": [
{
"required": [
"original_msg"
]
}
],
"properties": {
"original_msg": {
"type": "string"
}
}
}
}
我希望能够验证为 tx_properties
添加的更多属性,这些属性可能具有不同的类型,但在架构定义时未知。
例如我可能在 json:
"tx_properties": {
"original_msg": "foo",
"something_else": "bar",
"or_something_numeric": 172,
"or_even_deeper_things": {
"fungible": false,
}
}
作为一个 n00b,我对如何完成这个有点困惑。使用 anyOne
是我认为至少在最终解决方案中需要的。
正如@Evert 所说,"additionalProperties": false
可用于指示除 properties
关键字(和 patternProperties
)中列出的属性外,不允许使用其他属性。如果 additionalProperties
被省略,行为就好像模式说 "additionalProperties": true
(即允许附加属性)。
另请注意,additionalProperties
处的值不必是布尔值:它本身可以是子模式,以允许您根据其值有条件地允许其他属性。
参考:https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties