Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an optional option required when another one is defined in JSON Schema

I would like to define certificate and privateKey required when secure flag is set to true. Is this possible to achieve?

{
    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'
        }
}
like image 641
adelura Avatar asked Nov 09 '22 14:11

adelura


1 Answers

You can use 'dependencies' keyword.

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

You can even specify the schema the data should match when secure is present:

{
  dependencies: {
    secure: {
      properties: {
        certificate: {
          type: 'string'
        }
        privateKey: {
          type: 'string'
        }
      },
      required: ['certificate', 'privateKey']
    }
  }
}
like image 123
esp Avatar answered Nov 15 '22 04:11

esp