如何将一个属性指定为null或引用? [英] How to specify a property as null or a reference?
本文介绍了如何将一个属性指定为null或引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个json文档,其中的一部分可以为null或子对象,如下所示:
I have a json document in which a part can be either null or a subobject, like this:
[{
"owner":null
},
{
"owner":{
"id":1
}
}]
问题是,是否有可能使用ref在json模式草案v4中对此建模?
The question is if its possible to model this in json schema draft v4 using ref?
我想要的是这样的
{
"type":"object",
"properties":{
"owner":{
"type":["null", "object"],
"$ref":"#/definitions/id"
}
},
"definitions":{
"id":{
"type":"object",
"properties":{
"id":{
"type":"number"
}
}
}
}
}
推荐答案
如果您从定义中删除了"type":"object"
,则发布的内容应该可以使用.
What you've posted should work, if you remove the "type":"object"
from the definition.
但是,一种更简洁,更明确的方法来指定替代方法是使用oneOf
.您可以保持"id"
定义不变,只需使用:
However, a neater, more explicit way to specify alternatives is to use oneOf
. You can keep your "id"
definition untouched, and just use:
"owner":{
"oneOf": [
{"type": "null"},
{"$ref":"#/definitions/id"}
]
}
这篇关于如何将一个属性指定为null或引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文