JSON模式无法解析多个深度级别

时间:2018-11-04 21:56:34

标签: json schema

我正在尝试为表示数学表达式树的JSON创建架构。我陷入了一个问题,即我的架构除了外部JSON节点(“表达式”)之外不验证任何内容。但是,如果我在其他任何节点上出错,它将被忽略。这是架构

    {
    "definitions": {
        "operator": {
            "type": "string",
            "enum": [
                "+",
                "-",
                "/",
                "*",
                "^"
            ],
            "pattern": "^(.*)$"
        },
        "Identifier": {
            "type": "string",
            "examples": [
                "x",
                "y"
            ],
            "pattern": "^(.*)$"
        },
        "Binary": {
            "type": "object",
            "required": [
                "operator",
                "left",
                "right"
            ],
            "properties": {
                "operator": {
                    "$ref": "#/definitions/operator"
                },
                "left": {
                    "$ref": "#/definitions/left"
                },
                "right": {
                    "$ref": "#/definitions/right"
                }
            }
        },
        "left": {
            "type": "object",
            "oneOf": [
                {
                    "properties": {
                        "Binary": {
                            "$ref": "#/definitions/Binary"
                        }
                    }
                },
                {
                    "properties": {
                        "Unary": {
                            "$ref": "#/definitions/Unary"
                        }
                    }
                },
                {
                    "properties": {
                        "FunctionCall": {
                            "$ref": "#/definitions/FunctionCall"
                        }
                    }
                },
                {
                    "properties": {
                        "Identifier": {
                            "$ref": "#/definitions/Identifier"
                        }
                    }
                },
                {
                    "properties": {
                        "Number": {
                            "$ref": "#/definitions/Number"
                        }
                    }
                },
            ]
        },
        "right": {
            "type": "object",
            "oneOf": [
                {
                    "properties": {
                        "Binary": {
                            "$ref": "#/definitions/Binary"
                        }
                    }
                },
                {
                    "properties": {
                        "Unary": {
                            "$ref": "#/definitions/Unary"
                        }
                    }
                },
                {
                    "properties": {
                        "FunctionCall": {
                            "$ref": "#/definitions/FunctionCall"
                        }
                    }
                },
                {
                    "properties": {
                        "Identifier": {
                            "$ref": "#/definitions/Identifier"
                        }
                    }
                },
                {
                    "properties": {
                        "Number": {
                            "$ref": "#/definitions/Number"
                        }
                    }
                },
            ]
        },
        "Number": {
            "type": "string",
            "pattern": "^(.*)$"
        },
        "Unary": {
            "type": "object",
            "required": [
                "operator",
                "expression"
            ],
            "properties": {
                "operator": {
                    "$ref": "#/definitions/operator"
                },
                "expression": {
                    "oneOf": [
                        {
                            "$ref": "#/definitions/Binary"
                        },
                        {
                            "$ref": "#/definitions/Unary"
                        },
                        {
                            "$ref": "#/definitions/FunctionCall"
                        },
                        {
                            "$ref": "#/definitions/Identifier"
                        },
                        {
                            "$ref": "#/definitions/Number"
                        }
                    ]
                }
            }
        },
        "FunctionCall": {
            "type": "object",
            "required": [
                "name",
                "args"
            ],
            "properties": {
                "name": {
                    "type": "string",
                    "enum": [
                        "sin",
                        "cos",
                        "tan",
                        "atan",
                        "asin",
                        "acos",
                        "abs",
                        "log",
                        "exp",
                        "sqrt"
                    ]
                },
                "args": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "enum": [
                            "0"
                        ],
                        "required": [
                            "0"
                        ],
                        "properties": {
                            "0": {
                                "oneOf": [
                                    {
                                        "$ref": "#/definitions/Binary"
                                    },
                                    {
                                        "$ref": "#/definitions/Unary"
                                    },
                                    {
                                        "$ref": "#/definitions/FunctionCall"
                                    },
                                    {
                                        "$ref": "#/definitions/Identifier"
                                    },
                                    {
                                        "$ref": "#/definitions/Number"
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    },
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "required": [
        "Expression"
    ],
    "properties": {
        "Expression": {
            "type": "object",
            "oneOf": [
                {required: [
                        "Binary"
                    ]
                },
                {required: [
                        "Unary"
                    ]
                },
                {required: [
                        "FunctionCall"
                    ]
                },
                {required: [
                        "Idetifier"
                    ]
                },
                {required: [
                        "Number"
                    ]
                }
            ],
            "anyOf": [
                {
                    "properties": {
                        "Binary": {
                            "$ref": "#/definitions/Binary"
                        }
                    }
                },
                {
                    "properties": {
                        "Unary": {
                            "$ref": "#/definitions/Unary"
                        }
                    }
                },
                {
                    "properties": {
                        "FunctionCall": {
                            "$ref": "#/definitions/FunctionCall"
                        }
                    }
                },
                {
                    "properties": {
                        "Identifier": {
                            "$ref": "#/definitions/Identifier"
                        }
                    }
                },
                {
                    "properties": {
                        "Number": {
                            "$ref": "#/definitions/Number"
                        }
                    }
                },
            ]
        }
    }
}

这是我要验证的JSON示例

{
"Expression": {
    "Binary": {
        "operator": "+",
        "left": {
            "Binary": {
                "operator": "+",
                "left": {
                    "Binary": {
                        "operator": "*",
                        "left": {
                            "Number": "2"
                        },
                        "right": {
                            "Identifier": "x"
                        }
                    }
                },
                "right": {
                    "FunctionCall": {
                        "name": "sqrt",
                        "args": [
                            {
                                "0": {
                                    "Binary": {
                                        "operator": "*",
                                        "left": {
                                            "Number": "3"
                                        },
                                        "right": {
                                            "Identifier": "x"
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        },
        "right": {
            "Number": "6"
        }
    }
}

谁能说出什么问题?

0 个答案:

没有答案