作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过下面的示例问题会变得更加清晰:
data = {"type": "object", "properties": {"product": {"type": ["boolean","string"]}}}
它包括boolean
和string
类型。它有效,但我想将字符串部分限制为枚举
列表:
["off", "on", "semi-on"]
当type
不是list
时它可以工作,但是当我将其作为列表时,我无法为enum
提供字符串
类型。
下面没有 boolean
的示例也可以工作:
data = {"type": "object", "properties": {"product": {"type": "string", "enum": ["off", "on", "semi-on"]}}}
应该拆分架构,还是有其他方法?
<小时/>EDIT-1:真实架构如下:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"is_trusted": {"type": "boolean"},
"is_active": {"type": "boolean"},
"addresses": {"type": "array"},
"attachments": {
"type": "object",
"properties": {
"send_web_push": {"type": "boolean"},
"receive_web_push": {"type": "boolean"}
},
"required": ["send_web_push", "receive_web_push"],
"additionalProperties": false
}
},
"required": ["is_trusted", "is_active", "addresses"],
"additionalProperties": false
}
最佳答案
JSON 架构定义了验证关键字,并按其应用于实例的方式进行划分。
enum
位于 Validation Keywords for Any Instance Type 下部分。
这意味着它适用于任何实例类型,包括 bool 值。
因此,您必须在有效值枚举中包含 bool 值 true
和 false
。 (在此示例中,这确实使 type
变得多余)。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"product": {
"type": [
"boolean",
"string"
],
"enum": [
"off",
"on",
"semi-on",
true,
false
]
}
}
}
下面的实例现在对于上面的架构有效。在您的第一个架构中,值为 true
的 product
将失败 enum
验证,因为 true
未包含在枚举。
{
"product": true
}
关于python - 如何使用枚举包含 Jsonschema 多类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53041395/
我是一名优秀的程序员,十分优秀!