2012-01-27 98 views
30

想知道模式草案03是否可以实现这一点。我已经获得了其他地方的依赖关系,我想可能只是为了使用它们而需要创造性地使用它们他们用于指定某个字段的required属性。JSON模式 - 根据另一个字段的值,指定字段是必需的

我目前的最佳尝试(不起作用)应该会给你一些关于我之后的想法。我想要一个默认值,当另一个字段有特定值时是可选的。

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

回答

25

这对草案版本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"即可。

+5

值得注意的是,在版本4中,将模式放在'type'关键字中将被禁止。你可能会想要使用'allOf'或'oneOf'。 – cloudfeet 2012-10-18 15:38:01

+0

在规范的第4版中呢? – Relequestual 2014-10-06 12:57:11

+1

我会添加一些答案,以澄清v4替代品 – cloudfeet 2014-10-06 13:30:40

2

我只是看了03版本的规范,我不认为你所描述的是可能的。这绝对不是“简单依赖”,而“Schema Dependency”的描述没有提及任何方式来考虑属性的

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

还有什么是可能用简单的一些讨论和架构的依赖关系在这里: http://groups.google.com/group/json-schema/msg/8145690ebb93963b

你可能会问该组是否有计划支持有条件的依赖关系。

+0

辉煌,感谢您的链接。看起来JSON Schema目前还处于初级阶段,发现任何类型的文档都很难! ><我认为真正需要的是依赖性来操纵它们在其中定义的模式的原始属性的方式 - 这将允许通过依赖性以及许多其他可能的技巧来覆盖默认值和需求。 – pospi 2012-02-03 00:41:08

+0

答案中提供的链接包含一个非常有价值的实践例子!对于规范的每个部分都没有更好的文档/更实用的示例,这是一种耻辱。 – Relequestual 2014-10-07 08:51:13

10

如果有人正在为草案4的解决方案,你可以用enum关键字一起使用dependencies关键字:

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

这样play-options总是需要play值为true

+0

是的,它的工作原理。谢谢。如果你告诉方法使''''''''''''''''''''''“''''''也就是说,如果条件满足,则需要玩选项。 – Nilesh 2017-01-25 05:27:26

+0

@Nilesh对不起,你想实现什么? – ppalacios 2017-01-26 14:01:11

+3

我不认为这回答了这个问题。答案中的依存意味着存在“play-options”时,“play”必须为“true”。但是当“play”是“true”时,“play-options”不必存在。 – Sisyphus 2017-10-21 13:35:44

0

尝试使用依赖与 'ID':

{ 
    "title": "Test dependncies", 
    "readOnly": false, 
    "$schema": "http://json-schema.org/draft-04/hyper-schema", 
    "description": "This a test to show how we can use dependencies in json schema", 
    "properties":{ 
     "graduate": { 
      "title":"Graduate?", 
      "type":"string", 
      "enum":["Yes","No"], 
      "options": { 
      "dependencies":[ 
       {"id":"minimumEligibilityAge","value":"Yes"}, 
       {"id":"courseName","value":"No"} 
       ] 
      } 
     }, 
     "minimumEligibilityAge":{ 
      "id":"minimumEligibilityAge", 
      "title":"Enter Age", 
      "type":"number", 
      "options":{"hide_display":true} 
      }, 
     "courseName":{ 
      "id":"courseName", 
      "title":"Enter Graduation Course Name", 
      "type":"string" 
     } 
    }, 
    "type": "object" 
} 

如果研究生的价值是肯定的,它需要minimumEligibilityAge。 如果毕业生的价值是否,它需要courseName

相关问题