2017-02-16 4 views
0

Мне нужно построить схему 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, но я не смог адаптироваться в своем случае, используя usinf draft v4.

Любые предложения?

ответ

0

Нашли выход:

{ 
    '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' 
} 
Смежные вопросы