lis*_*szt 3 validation jsonschema switch-statement python-jsonschema
我正在使用 JSON 架构jsonschema来验证 JSON 记录。这是一个示例架构。这里只有两个案例,但想象一下类似的场景,其中有一百个像这样布置的案例。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"oneOf": [
{
"type": "object",
"required": ["a", "b", "c"],
"properties": {
"a": {"type": "integer", "enum": [0]},
"b": {"type": "integer", "enum": [0, 2, 4, 6, 8]},
"c": {"type": "string", "enum": ["always the same"]}
}
},
{
"type": "object",
"required": ["a", "b", "c"],
"properties": {
"a": {"type": "integer", "enum": [1]},
"b": {"type": "integer", "enum": [1, 3, 5, 7, 9]},
"c": {"type": "string", "enum": ["always the same"]}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
关键问题是字段的重复"c"。我希望能够切换大小写 on "a",验证相应的"b",但"c"始终保持不变。我不想拼写"c"一百次不同的内容。这可以吗?
谢谢!
是的,这是可以做到的。事实上,只放入发生变化的部分是一个很好的anyOf做法oneOf。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"c": { "const": "always the same" }
},
"required": ["a", "b", "c"],
"anyOf": [
{
"properties": {
"a": { "const": 0 },
"b": { "enum": [0, 2, 4, 6, 8] }
}
},
{
"properties": {
"a": { "const": 1 },
"b": { "enum": [1, 3, 5, 7, 9] }
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2856 次 |
| 最近记录: |