如何在数组中强制对象键名

How to force object key name in array

我正在使用 YAML 来标记一些公式,并使用 JSON 模式来提供参考模式。 YAML 的示例可能是:

formula: # equates to '5 + (3 - 2)'
  add:
    - 5
    - subtract: [3, 2]

虽然我已经弄清楚如何使公式的直接子对象(在此示例中为 "add")具有正确的键名称和类型(使用 "oneOf" 数组 "required"s)。我不确定如何确保数组对象 ("subtract") 同样使用特定的键名。

到目前为止,我可以使用以下方法确保类型。但是用这个方法,只要使用的对象匹配subtract type,就可以任意键名,不限于subtract:

"definitions: {
    "add": {
        "type": "array",
        "minItems": 2,
        "items": {
            "anyOf": [
                { "$ref": "#/definitions/value"}, # value type is an integer which allows for the shown scalar array elements
                { "$ref": "#/definitions/subtract" }
                // other operation types
            ]
        }
    },
    "subtract": {
        "type": "array",
        "minItems": 2,
        "maxItems": 2,
        "items": {
            "anyOf": [
                { "$ref": "#/definitions/value"},
                { "$ref": "#/definitions/add" }
                // other operation types
            ]
        }
    }
    // other operation types
}

如何引入限制,使数组中对象的键匹配特定名称,同时还允许标量元素?

听起来你想要的是递归引用。

通过创建一个新定义,即 oneOf 操作和 value,然后允许项目引用回新定义,您就拥有了递归引用。

"definitions: {
    "add": {
        "type": "array",
        "minItems": 2,
        "items": { "$ref": "#/definitions/operations_or_values"},
    },
    "subtract": {
        "type": "array",
        "minItems": 2,
        "maxItems": 2,
        "items": { "$ref": "#/definitions/operations_or_values"},
    }
    // other operation types
    "operations_or_values": {
      "anyOf": [
        { "$ref": "#definitions/add" },
        { "$ref": "#definitions/subtract" },
        { "$ref": "#definitions/value" },                 # value type is an integer which allows for the shown scalar array elements
        { "$ref": "#definitions/[OTHERS]" },
      ]
    }
}

我还没有时间对此进行测试,但我相信它将是或接近您所需要的。如果它不起作用,请告诉我。我可能没有完全理解这个问题。

多么有趣的问题!这个非常简洁的模式可以表达任何表达式。

{
  "type": ["object", "number"],
  "propertyNames": { "enum": ["add", "subtract", "multiply", "divide"] },
  "patternProperties": {
    ".*": {
      "type": "array",
      "minItems": 2,
      "items": { "$ref": "#" }
    }
  }
}

所以我最终做的是扩展我已经使用 "required" 的 '"oneOf" 数组的想法,添加一个 "anyOf".

因此,运算符模式现在是:

"definitions": {
    "add": {
        "type": "array",
        "minItems": 2,
        "items": {
            "anyOf": [
                { "$ref": "#/definitions/literal" }, // equates to a simple YAML scalar
                { "$ref": "#/definitions/constant" },
                { "$ref": "#/definitions/variable" },
                {
                    "type": "object",
                    "oneOf": [
                        { "required": ["add"] },
                        { "required": ["subtract"] }
                        // more operator names
                    ],
                    "properties": {
                        "add": { "$ref": "#/definitions/add" },
                        "subtract": { "$ref": "#/definitions/subtract" }
                        // more operator type references
                    }
                }
            ]
        }
    },
    // more definitions
}

这可以重构为更容易跨不同运算符应用的东西,如下所示:

"definitions": {
    "operands": {
        "literal": { "type": "number" }, // equates to a simple YAML scalar
        "constant": {
            "type": "object",
            "properties": {
                "value": { "type": "number" }
            },
            "required": [ "value" ]
        },
        "variable": {
            "type": "object",
            "properties": {
                "name": { type": "string" },
                "type": { "type": "string" }
            },
            "required": [ "name", "type" ]
        }
    }
    "operators": {
        "add": {
            "type": "array",
            "minItems": 2,
            "items": { "$ref": "#/definitions/anyOperandsOrOperators" }
        },
        "subtract": {
            "type": "array",
            "minItems": 2,
            "maxItems": 2,
            "items": { "$ref": "#/definitions/anyOperandsOrOperators" }
        }
        // more operator types
    },
    "anyOperator": {
        "type": "object",
        "oneOf": [
            { "required": ["add"] },
            { "required": ["subtract"] }
            // more operator names
        ],
        "properties": {
            "add": { "$ref": "#/definitions/operators/add" },
            "subtract": { "$ref": "#/definitions/operators/subtract" }
            // more operator type references
        }
    },
    "anyOperandsOrOperators":
    {
        "anyOf": [
            { "$ref": "#/definitions/operands/literal" },
            { "$ref": "#/definitions/operands/constant" },
            { "$ref": "#/definitions/operands/variable" },
            { "$ref": "#/definitions/anyOperator"}
        ]
    }
}

这意味着操作员的 YAML 可以如下所示

                    \/mapping   \/mapping
add:[ 5, subtract:[ *constantA, *variableB ] ]
scalar^  ^mapping with specific name