Raj Shrikrishnan
Raj Shrikrishnan

Reputation: 49

Json Schema required validation

I have my json schema where all values are required. For example:

....
{
"properties" : {
   "minimumDelay" : {
            "type" : "number"
        },
   "length" : {
            "type" : "number"
        },
}, 
"required": {
   "minimumDelay",
   "length"
}

Here the json data will be valid if I enter both minimumDelay and length values.

But my requirement is json data must be valid when I enter either 1 of the values(like XOR case). How my schema must be modified to achieve the same?

Upvotes: 0

Views: 165

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24409

In JSON Schema, the XOR operator is oneOf.

{
  "properties" : {
    "minimumDelay" : {
      "type" : "number"
    },
    "length" : {
      "type" : "number"
    }
  },
  "oneOf": [
    { "required": ["minimumDelay"] },
    { "required": ["length"] }
  ]
}

Upvotes: 1

Related Questions