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