需要值取决于 属性 值
Require value depending on a property value
我必须构建一个 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'
}
现在,我想根据命令值要求一些属性,例如:
- 如果命令设置为"read",我想要求路径,
- 如果命令设置为"write",我想要求路径、值和优先级
等...
我看到了一些关于此的主题,例如 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'
}
我必须构建一个 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'
}
现在,我想根据命令值要求一些属性,例如:
- 如果命令设置为"read",我想要求路径,
- 如果命令设置为"write",我想要求路径、值和优先级
等...
我看到了一些关于此的主题,例如 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'
}