【发布时间】:2021-07-29 12:12:28
【问题描述】:
重新发布问题How to implement conditional nested properties with JSON Schema(标记为重复,尽管它是一个完全不同的问题) 我的 JSON 模式在下面尝试基于:JSON Schema if/then require nested object
{
"$id": "myschema.json",
"if":{
"type":"object",
"properties": {
"common_data": {
"type":"object",
"properties":{
"remote_os": {
"type":"object",
"required":["common_data"],
"const": "Linux"
}
},
"required":["remote_os"]
}
}
},
"then": {
"type":"object",
"properties": {
"file": {
"type": "string",
"pattern": "^(.*.)(bin)$"
}
}
},
"else": {
"type":"object",
"properties": {
"file": {
"type": "string",
"pattern": "^(.*.)(exe)$"
}
}
}
}
基本上添加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)$'">]
当尝试调试 python jsonschema 正在尝试在此架构之上构建哪些属性以验证我的数据时。明白了
properties {'common_data': {'type': 'object', 'properties': {'remote_os': {'type': 'object', 'required': ['common_data'], 'const': 'Linux'}}, 'required': ['remote_os']}}
properties {'remote_os': {'type': 'object', 'required': ['common_data'], 'const': 'Linux'}}
properties {'file': {'type': 'string', 'pattern': '^(.*.)(exe)$'}}
所以它总是与'pattern': '^(.*.)(exe)$' 匹配,而与remote_os 无关。寻找一些指导,如何解决这个问题。
【问题讨论】:
-
我将其标记为重复,因为这方面是最明显的问题,但是现在您已经纠正了这个问题,我可以看到其他一些问题,我很高兴为您提供解决方案!跨度>
-
@Relequestual 非常感谢。是的,我花了一些时间并试图通过几篇文章和类似的问题来纠正这个问题。但仍然无法使其工作。非常感谢您的帮助。
标签: jsonschema json-schema-validator python-jsonschema