Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
      }
    }
}
like image 649
Daniel Mason Avatar asked Apr 24 '18 08:04

Daniel Mason


2 Answers

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

like image 186
Relequestual Avatar answered Sep 21 '22 01:09

Relequestual


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,
  },
like image 21
fezfox Avatar answered Sep 20 '22 01:09

fezfox