【发布时间】:2018-11-25 15:56:44
【问题描述】:
使用 Jsonschema 草案 6,我正在尝试创建符合以下内容的模式:
- 属性 A、B1、B2 和 B3 是数字或
null - 如果属性 A 存在且非 null,则属性 B、C 和 D 必须不存在或为 null
- 如果属性 B1、B2 和 B3 中的任何一个存在且非 null,则属性 A 必须为 null 或不存在。
- A、B1、B2 和 B3 可能都不存在
合格文件示例:
{}
{"A": 1}
{"A": 1, "B2": null}
{"B1": 1}
{"B1": 1, "B2": 1, "B3": 1}
{"A": null, "B1": 1, "B2": 1, "B3": 1}
不合格文件示例:
{"A": 1, "B1": 2}
{"A": 1, "B1": null, "B2": 1}
我看到了一些相关的问题,但没有完全回答问题:
- How to make anyOf a set of multually exclusive properties except one
- Use json-schema to require or disallow properties based on another property value?
- jsonSchema attribute conditionally required
- How to define choice element in json schema when elements are optional?
- How to define a JSON schema that requires at least one of many properties
这是我当前的架构,它只强制执行约束 #1 和 #4:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"A": {"oneOf": [{"type": "null"}, {"type": "number"}],
"B1": {"oneOf": [{"type": "null"}, {"type": "number"}],
"B2": {"oneOf": [{"type": "null"}, {"type": "number"}],
"B3": {"oneOf": [{"type": "null"}, {"type": "number"}]
}
}
这里的正确方法是什么?我的要求是不合理的吗?
【问题讨论】:
标签: jsonschema