【问题标题】:Require value depending on a property value需要取决于属性值的值
【发布时间】:2017-07-05 15:55:57
【问题描述】:

我必须构建一个 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'  
}

现在,我想根据命令值需要一些属性,例如:

  • 如果命令设置为“读取”,我想要求路径,
  • 如果命令设置为“写”,我想要求路径、值和优先级

等等……

我看到了一些关于这个的话题,比如JSON Schema - specify field is required based on value of another field,但我无法适应我的情况,通过使用草案 v4。

有什么建议吗?

【问题讨论】:

    标签: json schema jsonschema


    【解决方案1】:

    找到了出路:

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

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 2019-04-11
      • 2019-11-09
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多