jsonschema - 如何使用 JSON Schema 实现条件嵌套属性
问题描述
我有基本的 json 架构base.schema.json
{
"$id": "base.schema.json",
"type": "object",
"properties": {
"remote_os": {
"default": "Windows",
"enum": [
"Linux",
"Windows" ]
}
},
"required": ["remote_os"]
}
现在在另一个 json 中引用了模式定义
{
"$id": "update.schema.json",
"properties": {
"common_data": {
"$ref": "base.schema.json"
}
},
"allOf": [
{
"if": {
"properties": {
"common_data": {
"remote_os": {
"const": "Windows"
}
}
}
},
"then": {
"properties": {
"file": {
"pattern": "^(.*.)(exe)$"
}
}
}
},
{
"if": {
"properties": {
"common_data": {
"remote_os": {
"const": "Linux",
"required": [
"remote_os"
]
}
}
}
},
"then": {
"properties": {
"file": {
"pattern": "^(.*.)(bin)$"
}
}
}
}
]
}
基本上添加if-else
逻辑以确保remote_os=Linux
file
应该结束.bin
并且remote_os=Windows
file
应该结束.exe
现在我正在尝试验证以下数据
{
"common_data": {
"remote_os": "Linux"
},
"file": "abc.bin"
}
[<ValidationError: "'abc.bin' does not match '^(.*.)(exe)$'">]
. 不知道这里有什么问题
解决方案
我认为你把它复杂化了——如果/那么/其他?
{
"if": {
"properties": { "common_data": "properties": { "remote_os": { "const": "Windows" } } }
},
"then": {
"properties": { "file": { "pattern": "^(.*.)(exe)$" } }
},
"else": {
"properties": { "file": { "pattern": "^(.*.)(bin)$" } }
}
}
推荐阅读
- asp.net-mvc - 无法访问关闭的流:groupdocs
- flutter - 颤振:ListTile 宽度
- sql - 使用 WHILE LOOP 根据唯一行更新 SQL Server
- python - PySpark,多次调用dataframe withColomn方法后避免StackOverflowException
- python - 如何在 PyQt5 中制作按钮的图标以根据按钮是否被按下来改变它的颜色
- javascript - 如何将侦听器添加到表列
- javascript - 当电子邮件列表在数组中时,MailApp.sendEmail 接收错误
- css - ::after textarea中的伪元素不起作用
- python - 通过python烧瓶将变量发送到javascript并使用谷歌图表显示条形图
- scheme - 当将符号定义为其他东西时,方案中发生了什么?