danwoods
danwoods

Reputation: 4907

In JSONSchema, how do I validate that an object has only one of two properties, or neither property?

I've found several questions similar to my situation, but none I thought were the same. I have an object that can have properties "address" and "geoPoint". It can have either or neither of these properties, just not both. How could this be done with JSONSchema?

Thanks in advance, Dan

Upvotes: 2

Views: 70

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24409

The best way to do it is with dependencies.

{
  "type": "object",
  "dependencies": {
    "address": { "not": { "required": ["geoPoint"] } },
    "geoPoint": { "not": { "required": ["address"] } }
  }
}

Upvotes: 3

Related Questions