Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yaml 如何强制数组中的对象键名_Yaml_Jsonschema - Fatal编程技术网

Yaml 如何强制数组中的对象键名

Yaml 如何强制数组中的对象键名,yaml,jsonschema,Yaml,Jsonschema,我正在使用YAML标记一些公式,并使用JSON模式提供参考模式。 YAML的一个例子可能是: formula: # equates to '5 + (3 - 2)' add: - 5 - subtract: [3, 2] 虽然我已经了解了如何使公式的直接子对象(“add”,在本例中)具有正确的键名和类型(使用“oneOf”数组的“required”)。我不知道如何确保数组的对象(“subtract”)同样使用特定的键名 到目前为止,我可以使用以下方法确保类型。但使用此方法,

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

formula: # equates to '5 + (3 - 2)'
  add:
    - 5
    - subtract: [3, 2]
虽然我已经了解了如何使公式的直接子对象(
“add”
,在本例中)具有正确的键名和类型(使用
“oneOf”
数组的
“required”
)。我不知道如何确保数组的对象(
“subtract”
)同样使用特定的键名

到目前为止,我可以使用以下方法确保类型。但使用此方法,只要使用的对象与subtract类型匹配,就允许使用任何键名,而不限于
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
}

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

听起来您想要的是递归引用

"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]" },
      ]
    }
}
通过创建一个新的定义,该定义是操作和值中的一个,然后允许引用新定义的项,您就有了递归引用

"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]" },
      ]
    }
}

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

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

"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]" },
      ]
    }
}
通过创建一个新的定义,该定义是操作和值中的一个,然后允许引用新定义的项,您就有了递归引用

"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": "#" }
    }
  }
}

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

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

因此,我最终做的是扩展我已经使用的“
”oneOf“
”数组的
“required”
,添加了一个
“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

因此,我最终做的是扩展我已经使用的“
”oneOf“
”数组的
“required”
,添加了一个
“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