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

Is it possible to refer to a collection of definitions in JSON Schema?

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

这些操作可以采用变量形式的操作数,但它们也可以采用自身的实例。即"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 '/'
        ]
    }
}

您已经在使用 definitionsdefinitions 关键字中的键值必须是 JSON Schema。因此,您可以组合 anyOf$ref,类似于您已经完成的方式。

The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. The keyword does not directly affect the validation result.

This keyword's value MUST be an object. Each member value of this object MUST be a valid JSON Schema.

https://datatracker.ietf.org/doc/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”的名称更改为您想要的名称即可。我假设你的其他定义已经存在,但你只是没有将它们包含在你的问题中(这对这个目的来说很好)。