我有一个模式来验证传入的JSON,
JSON模式
{
'title': 'storage schema',
'description': 'storage schema',
'type': 'object',
'properties':{
'title': {
'title':'storage Name',
'type': 'string',
'minLength': 1,
'maxLength': 255
},
'storageType': {
'title': 'storage Type',
'enum' : ['DVD', 'HDD', 'Network', 'Internet']
},
'minCapacity': {
'title': 'Minimum Storage Capacity',
'type': 'number'
},
'maxCapacity': {
'title': 'Maximum Storage Capacity',
'type': 'number'
}
},
'additionalProperties':false,
'required':['title', 'storageType']
}如果minCapacity或HDD是DVD或HDD,则希望在json中显示storageType和DVD属性,如果json是Network,则不显示在json中。
如果我将模式修改为以存储为对象,并将最小和最大容量作为其属性,则可以这样做,如下面的模式所示。
{
'title': 'storage schema',
'description': 'storage schema',
'type': 'object',
'properties':{
'title': {
'title':'storage Name',
'type': 'string',
'minLength': 1,
'maxLength': 255
},
'storage': {
'title': 'storage Details',
'type': 'object',
'oneOf' : [{'$ref': '#/storage/disk'},
{'$ref': '#/storage/network'}]
},
},
'additionalProperties':false,
'required':['title', 'storage'],
'storage':{
'disk':{
'properties':{
'type': {
'title': 'Storage Type',
'enum': ['HDD', 'DVD']
},
'minCapacity': {
'title': 'Minimum Storage Capacity',
'type': 'number'
},
'maxCapacity': {
'title': 'Maximum Storage Capacity',
'type': 'number'
}
},
'additionalProperties': false,
'required':['type', 'minCapacity', 'maxCapacity']
},
'network':{
'properties':{
'type': {
'title': 'Storage Type',
'enum': ['Network', 'Internet']
}
},
'additionalProperties': false,
'required':['type']
}
}
}但是,我希望在不改变架构结构的情况下实现这一点。
能办到吗?
有效Json 1
{
'title': 'additional mandatory properties',
'storageType': 'HDD',
'minCapacity': 0.1,
'maxCapacity': 1
}有效Json 2
{
'title': 'no additional mandatory properties',
'storageType': 'Network'
}无效的Json 1
{
'title': 'additional mandatory properties',
'storageType': 'Internet',
'minCapacity': 0.1,
'maxCapacity': 1
}无效Json 2
{
'title': 'no additional mandatory properties',
'storageType': 'HDD'
}更新
当从jason's应答尝试模式时,它并不是验证时,只有在json中存在一个非必需的参数时才进行验证。下面给出了无效的JSON。
无效的Json 3
{
'title': 'additional mandatory properties',
'storageType': 'Internet',
'minCapacity': 0.1
}无效的Json 4
{
'title': 'additional mandatory properties',
'storageType': 'Internet',
'maxCapacity': 1
}我在“不需要”部分中对模式进行了小修改,解决了这个问题,如下所示。
{
"title": "storage schema",
"description": "storage schema",
"type": "object",
"properties": {
"title": {
"title": "storage Name",
"type": "string",
"minLength": 1,
"maxLength": 255
},
"storageType": {
"title": "storage Type"
},
"minCapacity": {
"title": "Minimum Storage Capacity",
"type": "number"
},
"maxCapacity": {
"title": "Maximum Storage Capacity",
"type": "number"
}
},
"required": ["title", "storageType"],
"anyOf": [
{
"properties": {
"storageType": {
"enum": ["DVD", "HDD"]
}
},
"required": ["minCapacity", "maxCapacity"]
},
{
"properties": {
"storageType": {
"enum": ["Network", "Internet"]
}
},
"allOf":[
{"not": {"required": ["maxCapacity"]}},
{"not": {"required": ["minCapacity"]}}
]
}
]
}发布于 2015-07-29 17:42:05
这是解决办法。
{
"title": "storage schema",
"description": "storage schema",
"type": "object",
"properties": {
"title": {
"title": "storage Name",
"type": "string",
"minLength": 1,
"maxLength": 255
},
"storageType": {
"title": "storage Type"
},
"minCapacity": {
"title": "Minimum Storage Capacity",
"type": "number"
},
"maxCapacity": {
"title": "Maximum Storage Capacity",
"type": "number"
}
},
"additionalProperties": false,
"required": ["title", "storageType"],
"anyOf": [
{
"properties": {
"storageType": {
"enum": ["DVD", "HDD"]
}
},
"required": ["minCapacity", "maxCapacity"]
},
{
"properties": {
"storageType": {
"enum": ["Network", "Internet"]
}
},
"not": { "required": ["maxCapacity", "minCapacity"] }
}
]
}不鼓励使用"additionalProperties": false。像#/anyOf/1/not这样的东西就是一个例子,说明这个特性可能会带来比它更大的麻烦。最佳实践是简单地忽略不属于的属性。
https://stackoverflow.com/questions/31703185
复制相似问题