JSON 模式 - 根据另一个字段的值指定字段是必需的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9029524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON Schema - specify field is required based on value of another field
提问by pospi
Wondering if this is possible with schema draft 03. I've gotten dependencies working elsewhere, I think there is possibly just some creative use of them required in order to use them for specifying the requiredproperty of some field.
想知道模式草案 03 是否可以实现。我已经在其他地方使用了依赖项,我认为可能只是需要对它们进行一些创造性的使用,以便使用它们来指定required某个字段的属性。
My current best attempt (which doesn't work) should give you some idea of what I'm after. I want a value required by default, and optional when another field has a particular value.
我目前的最佳尝试(不起作用)应该让您对我所追求的有所了解。我想要一个默认需要的值,当另一个字段具有特定值时可选。
{
"description" : "An address...",
"type" : "object",
"properties" : {
"postcode": {
"type" : "string",
// postcode should be required by default
"required" : true,
// postcode shouldn't be required if the country is new zealand
"dependencies" : {
"country" : {
"enum" : ["NZ", "NZL", "NEW ZEALAND"]
},
"postcode" : {
"required" : false
}
}
},
"country": {
"type" : "string",
"enum" : [
// various country codes and names...
],
"default" : "AUS"
}
}
}
采纳答案by cloudfeet
This is definitely possible with version 3 of the draft. Since you have a complete list of allowed countries, then you could do something like this:
这对于草案的第 3 版绝对是可能的。由于您拥有完整的允许国家/地区列表,因此您可以执行以下操作:
{
"type": [
{
"title": "New Zealand (no postcode)",
"type": "object",
"properties": {
"country": {"enum": ["NZ", "NZL", "NEW ZEALAND"]}
}
},
{
"title": "Other countries (require postcode)",
"type": "object",
"properties": {
"country": {"enum": [<all the other countries>]},
"postcode": {"required": true}
}
}
],
"properties": {
"country": {
"type" : "string",
"default" : "AUS"
},
"postcode": {
"type" : "string"
}
}
}
So you actually define two sub-types for your schema, one for countries that require a postcode, and one for countries that do not.
因此,您实际上为架构定义了两种子类型,一种用于需要邮政编码的国家/地区,另一种用于不需要邮政编码的国家/地区。
EDIT- the v4 equivalent is extremely similar. Simply rename the top-level "type"array to "oneOf".
编辑- v4 等效项非常相似。只需将顶级"type"数组重命名为"oneOf".
回答by ppalacios
If anybody is looking for a solution for draft 4 you can use dependencieskeyword together with a enumkeyword:
如果有人正在寻找草案 4 的解决方案,您可以将dependencies关键字与关键字一起使用enum:
{
"type": "object",
"properties": {
"play": {
"type": "boolean"
},
"play-options": {
"type": "string"
}
},
"dependencies": {
"play-options": {
"properties": {
"play": {
"enum": [true]
}
}
}
}
}
In this wayplay-optionswill always require playvalue to be true.
以这种方式play-options将始终需要play值true。
回答by Chris Sears
I just looked over the 03 version of the spec and I don't think what you are describing is possible. It's definitely not "Simple Dependency" and the description of "Schema Dependency" doesn't mention any way to consider the valueof the property.
我刚刚查看了规范的 03 版本,我认为您所描述的内容是不可能的。它绝对不是“简单依赖”,“模式依赖”的描述没有提到任何考虑属性值的方法。
It sounds like what you need is "Conditional Schema Dependency".
听起来你需要的是“条件模式依赖”。
There's some discussion of what's possible with Simple and Schema dependencies here: http://groups.google.com/group/json-schema/msg/8145690ebb93963b
这里有一些关于 Simple 和 Schema 依赖项的可能性的讨论:http: //groups.google.com/group/json-schema/msg/8145690ebb93963b
You might ask that group if there are plans to support conditional dependencies.
您可能会询问该小组是否有计划支持条件依赖。
回答by Mike
In the latest schema you can use the oneOfconditional to do this.
在最新的模式中,您可以使用oneOf条件来执行此操作。
{
"description" : "An address...",
"type" : "object",
"properties" : {
"postcode": {
"type" : "string"
},
"country": {
"type" : "string",
"enum" : [
// various country codes and names...
],
"default" : "AUS"
}
},
"oneOf": [
{
"properties": {
"country": { "enum" : ["NZ", "NZL", "NEW ZEALAND"] }
}
},
{ "required": ["postcode"] }
]
}
The oneOfcondition requires that one of the conditions in the array is true.
的oneOf条件要求的阵列中的条件之一为真。

