41

想知道模式草案 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

4 回答 4

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".

于 2012-07-25T14:50:35.017 回答
15

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

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

这样一来,play-options将始终要求playvalue 为true

于 2016-10-27T19:44:40.840 回答
5

在最新的模式中,您可以使用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条件之一为真。

于 2020-05-19T20:39:27.520 回答
2

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

听起来您需要的是“条件模式依赖”。

这里有一些关于简单和模式依赖项的可能的讨论: http ://groups.google.com/group/json-schema/msg/8145690ebb93963b

您可能会询问该小组是否有计划支持条件依赖。

于 2012-02-01T20:14:06.133 回答