JSON 架构必填字段取决于其他字段值

JSON Schema required field depending on other field value

是否可以根据 JSON 架构中的其他字段值创建一个字段 required/non-required?

JSON 架构包含 mode 字段。如果它等于'release'或'debug',则file_path不是必需的。如果它等于'custom',则它是必需的。

"mode": {
    "enum": [
        "debug",
        "release",
        "custom"
    ],
    "id": "mode",
    "required": true,
    "type": "string"
},
"file_path": {
    "id": "file_path",
    "required": false,
    "type": "string"
}

JSON 架构草案 7 有一个解决方案:

{
    "build": {
        "type": "object",
        "id": "build",

        "oneOf": [
            {
                "$ref": "#/definitions/ReleaseDebug"
            },
            {
                "$ref": "#/definitions/Custom"
            }
        ]
    }
},
"definitions": {
    "ReleaseDebug": {
        "required": ["mode"],
        "properties": {
            "mode": {
                "type": "string",
                "enum": [
                    "debug",
                    "release"
                ],
                "id": "mode"
            }
        }
    },
    "Custom": {
        "required": ["mode", "file_path"],
        "properties": {
            "mode": {
                "type": "string",
                "enum": [
                    "custom"
                ],
                "id": "mode"
            },
            "file_path": {
                "type": "string",
                "id": "file_path"
            },
        }
    }
}