Daniel Mason
Daniel Mason

Reputation: 280

How do you reference other property values in JSON schema?

I have a JSON schema with 2 properties, minimumTolerance and maximumTolerance. I need to make sure that the value of maximumTolerance is not smaller than minimumTolerance & vice versa. Is this possible in JSON schema?

Here is an example of what I'd like to be able to do:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "title": "MinMax",
  "description": "Minum & Maximum",
  "type": "object",
  "properties": {
    "minimumTolerance":{
      "type": "number"
      "maximum":{
        "$ref":"maximumTolerance"
      }
    }
    "maximumTolerance":{
      "type": "number"
      "minumum": {
        "$ref":"minimumTolerance"
      }
    }
}

Upvotes: 10

Views: 5258

Answers (2)

fezfox
fezfox

Reputation: 967

If you are using AJV You can do this using $data and relative JSON pointers. Example:

  low: {
    type: 'integer',
    maximum: {
      $data: '1/high',
    },
    exclusiveMaximum: true,
  },

  high: {
    type: 'integer',
    minimum: {
      $data: '1/low',
    },
    exclusiveMinimum: true,
  },

Upvotes: 3

Relequestual
Relequestual

Reputation: 12355

As of Draft-7 of the specification there is no way to do this with JSON Schema.

Upvotes: 11

Related Questions