你如何在JSON schema中引用其他属性值?

9
我有一个JSON架构,其中有两个属性:minimumTolerance和maximumTolerance。我需要确保maximumTolerance的值不小于minimumTolerance,并且反之亦然。 JSON模式是否支持此操作?
以下是我希望能够执行的示例:
{
  "$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"
      }
    }
}
2个回答

10

截至规范的第七版,使用JSON Schema无法实现此功能。


1
你知道这个会被包含在未来的版本中吗? - Måns Dahlström
不太可能。这被归类为“业务逻辑”。 - Relequestual

3
如果您使用AJV,可以使用$data和相对JSON指针来完成此操作。例如:
  low: {
    type: 'integer',
    maximum: {
      $data: '1/high',
    },
    exclusiveMaximum: true,
  },

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接