JSON模式-需要所有属性 [英] JSON Schema - require all properties
问题描述
JSON模式具有properties
,required
和additionalProperties
字段.例如,
JSON Schema features the properties
, required
and additionalProperties
fields. For example,
{
"type": "object",
"properties": {
"elephant": {"type": "string"},
"giraffe": {"type": "string"},
"polarBear": {"type": "string"}
},
"required": [
"elephant",
"giraffe",
"polarBear"
],
"additionalProperties": false
}
将验证JSON对象,例如:
Will validate JSON objects like:
{
"elephant": "Johnny",
"giraffe": "Jimmy",
"polarBear": "George"
}
但是如果属性列表不是完全 elephant, giraffe, polarBear
,则会失败.
But will fail if the list of properties is not exactly elephant, giraffe, polarBear
.
我经常将properties
的列表复制粘贴到required
的列表中,当由于错别字和其他愚蠢的错误导致列表不匹配时,我会遇到烦人的错误.
I often copy-paste the list of properties
to the list of required
, and suffer from annoying bugs when the lists don't match due to typos and other silly errors.
是否有一种较短的方法来表示所有属性都是必需的,而无需显式命名它们?
推荐答案
您可以只使用"minProperties"属性,而不是显式命名所有字段.
You can just use the "minProperties" property instead of explicity naming all the fields.
{
"type": "object",
"properties": {
"elephant": {"type": "string"},
"giraffe": {"type": "string"},
"polarBear": {"type": "string"}
},
"additionalProperties": false,
"minProperties": 3
}
这篇关于JSON模式-需要所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!