JSON模式 - 根据另一个字段的值指定字段

时间:2012-01-27 05:59:01

标签: json dependencies schema jsonschema

想知道这是否可以使用模式草案03.我已经在其他地方使用了依赖项,我认为可能只需要一些创造性的使用它们来使用它们来指定某些字段的required属性

我目前的最佳尝试(不起作用)应该让你知道我在追求什么。我想要一个默认值所需的值,并且当另一个字段具有特定值时可选。

{
    "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"
        }
    }
}

4 个答案:

答案 0 :(得分:27)

草案版本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"
        }
    }
}

因此,您实际上为您的架构定义了两个子类型,一个针对需要邮政编码的国家/地区,另一个针对不需要邮政编码的国家/地区。

编辑 - v4等效版非常相似。只需将顶级"type"数组重命名为"oneOf"

答案 1 :(得分:12)

如果有人正在为草案4寻找解决方案,您可以将dependencies关键字与enum关键字一起使用:

{
    "type": "object",
    "properties": {
        "play": {
            "type": "boolean"
        },
        "play-options": {
            "type": "string"
        }
    },
    "dependencies": {
        "play-options": {
            "properties": {
                "play": {
                     "enum": [true]
                }
            }
        }
    }
}

这样play-options始终需要play值为true

答案 2 :(得分:2)

我只是查看了规范的03版本,我不认为你所描述的是可能的。它绝对不是“简单依赖”,并且“模式依赖”的描述没有提及任何考虑属性的的方法。

听起来你需要的是“条件架构依赖”。

这里讨论了Simple和Schema依赖关系的可能性: http://groups.google.com/group/json-schema/msg/8145690ebb93963b

如果有计划支持条件依赖,您可以询问该组。

答案 3 :(得分:1)

在最新模式中,您可以使用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"] } 
      ]
}

oneOf条件要求数组中的条件之一为true。

相关问题