2015-05-08 1 views
5

Ich möchte certificate und privateKey definieren, wenn secure Flag auf True gesetzt ist. Ist das möglich?Eine optionale Option erforderlich machen, wenn eine andere im JSON-Schema definiert wird

{ 
    type: 'object', 
    properties: { 
     'secure': { 
      title: 'Serve files over HTTPS', 
      description: 'Flag telling whether to serve contents over HTTPS and WSS', 

      type: 'boolean', 
      default: false 
     }, 

     'certificate': { 
      title: 'Certificate file', 
      description: 'Location of the certificate file', 

      type: 'string' 
     }, 

     'privateKey': { 
      title: 'Private key file', 
      description: 'Location of the private key file', 

      type: 'string' 
     } 
} 

Antwort

0

Fragen Sie nach dem Schema Inhalt abhängig? Ich meine "was das Schema erlaubt hängt davon ab, was im Ziel (JSON) Inhalt ist"?

Sie können das nicht tun.

0

Es gibt einen Weg, aber es ist nicht schön. Sie müssen das Schlüsselwort anyOf verwenden, um festzulegen, wie es validiert werden soll, wenn securetrue ist und wie es validiert werden soll, wenn securefalse ist.

{ 
    "type": "object", 
    "properties": { 
    "secure": { 
     "title": "Serve files over HTTPS", 
     "description": "Flag telling whether to serve contents over HTTPS and WSS", 
     "type": "boolean" 
    } 
    }, 
    "anyOf": [ 
    { 
     "type": "object", 
     "properties": { 
     "secure": { 
      "enum": [true] 
     }, 
     "certificate": { 
      "title": "Certificate file", 
      "description": "Location of the certificate file", 
      "type": "string" 
     }, 
     "privateKey": { 
      "title": "Private key file", 
      "description": "Location of the private key file", 
      "type": "string" 
     } 
     }, 
     "required": ["certificate", "privateKey"] 
    }, 
    { 
     "type": "object", 
     "properties": { 
     "secure": { 
      "enum": [false] 
     } 
     } 
    } 
    ] 
} 
1

Sie können das Schlüsselwort 'Abhängigkeiten' verwenden.

{ 
    dependencies: { 
    secure: ['certificate', 'privateKey'] 
    } 
} 

Sie auch das Schema die Daten sollten übereinstimmen können festlegen, wann sichere vorhanden ist:

{ 
    dependencies: { 
    secure: { 
     properties: { 
     certificate: { 
      type: 'string' 
     } 
     privateKey: { 
      type: 'string' 
     } 
     }, 
     required: ['certificate', 'privateKey'] 
    } 
    } 
} 
Verwandte Themen