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

pos*_*spi 36 schema dependencies json 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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

clo*_*eet 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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 值得注意的是,在版本4中,将禁止在`type`关键字中放置模式.你可能会想要使用`allOf`或`oneOf`. (6认同)
  • 我的意思是,有没有更好/不同的方式来实现上述v4?到目前为止,我有一个100行json模式...我不想仅仅为一个条件要求加倍 (2认同)

ppa*_*ios 13

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

{
    "type": "object",
    "properties": {
        "play": {
            "type": "boolean"
        },
        "play-options": {
            "type": "string"
        }
    },
    "dependencies": {
        "play-options": {
            "properties": {
                "play": {
                     "enum": [true]
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方式play-options总是需要play价值true.

  • 我不认为这回答了这个问题.答案中的依赖性意味着当"play-options"存在时,"play"必须为"true".但是当'play`是'true`时,'play-options`不一定存在. (4认同)

Mik*_*ike 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"] } 
      ]
}
Run Code Online (Sandbox Code Playgroud)

oneOf条件要求的阵列中的条件之一为真。