Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I describe mutually exclusive values of properties?

I have two properties (propOne and propTwo).

If one of the properties values is true the other must be set to false.

How do I describe this in jsonschema?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can say if propOne is true require propTwo, but this isn’t enough:

"anyOf": [
    {
        "not": {
            "properties": {
                "propOne": {
                    "const": true
                }
            },
            "required": [
                "propOne"
            ]
        }
    },
    {
        "required": [
            "propTwo"
        ]
    }
],

What is the most concise way to describe my requirements?

Edit:
Per the answer this does what I want. I know there is a handful of ways to describe this. It seems like oneOf is the most concise? I think if conditions would require more lines?

"oneOf": [
    {
        "type": "object",
        "properties": {
            "propOne": {
                "const": true
            },
            "propTwo": {
                "const": false
            }
        }
    },
    {
        "type": "object",
        "properties": {
            "propOne": {
                "const": true
            },
            "propTwo": {
                "const": false
            }
        }
    }
],

>Solution :

There are the if/then/else keywords, and also oneOf (see https://json-schema.org/understanding-json-schema/reference/conditionals.html)

So, in pseudocode:

  • must be an object.
  • require propOne and propTwo.
  • propOne is a boolean.
  • propTwo is a boolean.

and either of these are equivalent (when combined with the above restrictions):

  • if propOne is true, then propTwo is false, else propTrue is true.
  • oneOf:
    • propOne is true and propTwo is false.
    • propOne is false and propTwo is true.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading