需要取决于属性值的值
问题描述:
我必须构建一个Json模式来格式化应用程序需要发送给另一个应用程序的每个消息。需要取决于属性值的值
我已经建立这个:
{
'description': 'BLABLA',
'definitions': {
'M2M_message_input' : {
'type' : 'object',
'properties' : {
'command' : {
'type': 'string',
'enum' : ['read', 'write', 'list', 'reset priority']
},
'path' : {
'type' : 'string',
'pattern' : '^\/'
},
'value' : {'type' : 'string'},
'priority' : {
'type' : 'integer',
'maximum' : 255,
'exclusiveMaximum' : false,
'minimum' : 0,
'exclusiveMinimum' : false
}
},
'required': ['command'],
'dependencies' : {
'value' : ['priority']
},
"additionalProperties" : false
}
},
'type': 'object',
'$ref' : '#/definitions/M2M_message_input'
}
现在,我想需要根据命令值,像一些属性:
- 如果命令是设置好的“读”,我想要求的路径,
- 如果命令是设置好的“写”,我想需要路径,价值和优先
etc ...
我看到一些关于此的话题,比如JSON Schema - specify field is required based on value of another field,但是我无法适应我的情况,通过使用草稿版本v4。
有什么建议吗?
答
找到了出路:
{
'description': '[...]',
'definitions': {
'readParameter' : {
'type' : 'object',
'required' : ['command','path'],
'properties' : {
'command' : {
'type' : 'string',
'enum' : ['read']
},
'path' : {
'type' : "string"
}
},
"additionalProperties" : false
},
'writeParameter' : {
'type' : 'object',
'required' : ['command','path', 'value', 'priority'],
'properties' : {
'command' : {
'type' : 'string',
'enum' : ['write']
},
'path' : {
'type' : "string"
},
'value' : {
'type' : "string"
},
'priority' : {
'type' : 'integer',
'maximum' : 255,
'exclusiveMaximum' : false,
'minimum' : 0,
'exclusiveMinimum' : false
}
},
"additionalProperties" : false
},
'listParameter' : {
'type' : 'object',
'required' : ['command'],
'properties' : {
'command' : {
'type' : 'string',
'enum' : ['list']
}
},
"additionalProperties" : false
},
'M2M_message_input' : {
'type' : 'object',
'oneOf': [
{ "$ref": "#/definitions/readParameter" },
{ "$ref": "#/definitions/writeParameter" },
{ "$ref": "#/definitions/listParameter" }
],
}
},
'type': 'object',
'$ref' : '#/definitions/M2M_message_input'
}