是否可以引用JSON Schema中的定义集合?

时间:2018-05-02 14:01:15

标签: json jsonschema

我正在使用JSON模式构建模式,其中一部分描述了数学运算(例如加,乘,减,除等)。

这些操作可以获取作为变量的操作数,但它们也可以获取自身的实例。这是"add""multiply"类型可以表示如下:

"add": {
    "description": "The mathematical operation of adding two or more operands together",
    "type": "array",
    "minItems": 2,
    "items": {
        "anyOf": [
            { "$ref": "#/definitions/variable" },
            { "$ref": "#/definitions/subformula" },
            { "$ref": "#/definitions/add" },
            { "$ref": "#/definitions/multiply" },
            { "$ref": "#/definitions/subtract" },
            { "$ref": "#/definitions/divide" },
            { "$ref": "#/definitions/sum" },
            { "$ref": "#/definitions/maximum" },
            { "$ref": "#/definitions/average" }
        ]
    }
},
"multiply": {
    "description": "The mathematical operation of multiplying two or more operands together",
    "type": "array",
    "minItems": 2,
    "items": {
        "anyOf": [
            { "$ref": "#/definitions/variable" },
            { "$ref": "#/definitions/subformula" },
            { "$ref": "#/definitions/add" },
            { "$ref": "#/definitions/multiply" },
            { "$ref": "#/definitions/subtract" },
            { "$ref": "#/definitions/divide" },
            { "$ref": "#/definitions/sum" },
            { "$ref": "#/definitions/maximum" },
            { "$ref": "#/definitions/average" }
        ]
    }
}

对于JSON,例如:

"add": [ 5, 6, "multiply": [ 2, 3 ] ]

此类型引用列表对于每个操作和子公式都是类似的。如果可能添加更多操作,则需要在列表发生的每个位置将其添加到此列表中,这相当麻烦......

是否有可能以某种方式标记操作,以便我可以将它们称为一个组,如下所示:

"add": {
    "description": "The mathematical operation of adding two or more operands together",
    "type": "array",
    "minItems": 2,
    "items": {
        "anyOf": [
            { "$ref": "#/definitions/variable" },
            { "$ref": "#/definitions/subformula" },
            { "$ref": "#/definitions/operations/" } //note the trailing '/'
        ]
    }
},
"multiply": {
    "description": "The mathematical operation of multiplying two or more operands together",
    "type": "array",
    "minItems": 2,
    "items": {
        "anyOf": [
            { "$ref": "#/definitions/variable" },
            { "$ref": "#/definitions/subformula" },
            { "$ref": "#/definitions/operations/" } //note the trailing '/'
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

您已使用definitionsdefinitions关键字中的键值必须是JSON模式。因此,您可以将anyOf$ref结合起来,类似于您已经完成的工作。

  

“definitions”关键字为架构提供标准化位置   作者将可重用的JSON模式内联到更通用的模式中。   该关键字不会直接影响验证结果。

     

此关键字的值必须是一个对象。每个成员的价值   对象必须是有效的JSON模式。

https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9

"items": {
  "$ref": "#/definitions/myOtherGroup",
}

...

"definitions": {
  "operations": {
    "anyOf": [
      { "$ref": "#/definitions/add" },
      { "$ref": "#/definitions/multiply" },
      { "$ref": "#/definitions/subtract" },
      { "$ref": "#/definitions/divide" },
      { "$ref": "#/definitions/sum" },
      { "$ref": "#/definitions/maximum" },
      { "$ref": "#/definitions/average" }
    ]
  }
  "myOtherGroup": {
    "anyOf": [
      { "$ref": "#/definitions/variable" },
      { "$ref": "#/definitions/subformula" },
      { "$ref": "#/definitions/operations" },
    ]
  }
}

您只需要将“myOtherGroup”的名称更改为您想要的名称。我假设你的其他定义已经存在,但你只是没有在你的问题中包含它们(这对于这个目的来说很好)。