2017-02-16 7 views
0

Ich muss ein JSON-Schema erstellen, um jede Nachricht zu formatieren, die eine Anwendung an eine andere senden muss.Wert abhängig von einem Eigenschaftswert erforderlich

ich dies schon bauen:

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

Vorerst will ich auf dem Sollwert einige Eigenschaften erfordern abhängig, wie:

  • , wenn der Befehl "Lesen" ist setted auf, i möchten Sie den Pfad erfordern, wird
  • , wenn der Befehl zum „schreiben“ setted, i Pfad, den Wert und Priorität
erforderlich sein soll 210

etc ...

Ich sah einige Themen dazu, wie JSON Schema - specify field is required based on value of another field, aber ich war nicht in der Lage, an meinem Fall anzupassen, durch usinf den Entwurf v4.

Irgendwelche Vorschläge?

Antwort

0

einen Ausweg gefunden:

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