json - JSONSchema - if property is true - another field is required
问题描述
I want to create a schema such that when it has the property has_reference
the field reference
exists (if the field doesn't exist, it is implicitly false). and vice versa. The schema has other fields a
and b
.
So this is a valid instance:
{
"a": 1, "b": 2
}
{
"a": 1, "b": 2
"has_refernce": true, "reference": "bla"
}
but this is not valid:
{
"a": 1, "b": 2
"has_refernce": true
}
{
"a": 1, "b": 2
"refernce": "bla"
}
Is this possible?
EDIT: I understand that correct design would be to remove "use_reference" and use null "reference" to indicate whether a reference exists, but I can change the design
解决方案
If you are using the latest version of schema then you can utilize the following hack to achieve this -
"dependencies": {
"has_refernce": [
"reference"
],
"reference": [
"has_refernce"
]
}
You can also make use of conditions in schema -
"allOf" : [
{
"if": {
"properties": {
"has_refernce": {
"const": true
}
}
},
"then": {
"required": [
"reference"
]
}
}
]
推荐阅读
- flutter - 当我在 Flutter 的 Hive Db 中添加元素时,如何修复错误路由长度被调用为 null?
- python - 无法在自定义编码器中将 dict 编码为 JSON
- java - Spring Data JPA @Modifying 注解使用@Transactional
- android - Android Room FTS4 如何删除自动创建的触发器
- python - 使用 print(path) 时无法打印图像名称
- github - github主页无法正确加载且不完整
- html - 替换datalist自带的默认箭头
- makefile - 迭代子制作的值
- c# - AIMLStandard 机器人值不能为空 path2 错误
- python - Pandas/SQL 仅返回 groupby 至少有 n 行的结果