Make an optional option if another parameter is defined in JSON Schema

I would like to define the certificate and privateKey needed if the secure flag is set to true. Can this be achieved?

 { 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' } } 
+5
source share
3 answers

You can use the keyword 'dependencies'.

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

You can even specify the scheme with which data should correspond when protection is present:

 { dependencies: { secure: { properties: { certificate: { type: 'string' } privateKey: { type: 'string' } }, required: ['certificate', 'privateKey'] } } } 
+1
source

Are you asking for content to be schema dependent? I mean, "what the circuit allows depends on what is in the target (json) content"?

You cannot do this.

0
source

There is a way, but it is not very. You need to use the anyOf keyword to determine how you want to check it if secure is true and how you want it to check when secure is false .

 { "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] } } } ] } 
0
source

All Articles