【发布时间】:2021-07-28 12:04:20
【问题描述】:
我有基本的 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)$'">]。不知道这里出了什么问题
【问题讨论】:
-
用 "'abc.bin' 难以理解的地方与 '^(.*.)(exe)$'" 不匹配?
-
对于验证数据
remote_os=Linux和 JSON Schema 中的if-elseupdate.schema.jsonfile应该以bin扩展名结束。所以我的验证数据只是正确的,它不应该引发ValidationError -
啊,我明白了。我建议一个更好的问题标题:“How to check nested properties with JSON Schema if else” 而不是“not working”。
-
感谢反馈,我现在改标题了。
-
在示例中,参考架构定义是相同 JSON 的一部分,我希望它也适用于本地文件参考。仍然没有找到
if-else应该去的地方。一些例子真的很有帮助
标签: jsonschema json-schema-validator python-jsonschema