首页 > 解决方案 > AWS-API 网关——jsonschema 子对象应该在父对象存在时进行验证

问题描述

我需要为以下 JSON 输入创建 Jsonschema。仅当存在 Vehicle 对象时,才应需要 Vehicle 下的属性(例如制造商、模型等)。

 {
     "Manufacturer": "",
     "Characteristics": {
         "Starts": "new",
         "vehicle": {
             "Manufacturer": "hello",
             "Model": "hh",
             "Opening": "",
             "Quantity": "",
             "Principle": "",
             "Type": ""
         }
     }
 }

我尝试了以下 JsonSchema 但这在 Vehicle 对象不存在时有效但如果我们将 Vehicle 重命名为其他一些 ex: Vehicle1 它不会给出错误。请指导我如何解决这个问题。

 {
    "$schema": "http://json-schema.org/draft-07/schema",
    "type": "object",
    "properties": {
        "Manufacturer": {
            "type": [
                "string",
                "null"
            ]
        },
        "Characteristics": {
            "type": "object",
            "properties": {
                "Starts": {
                    "type": [
                        "string",
                        "null"
                    ]
                },
                "Vehicle": {
                    "$ref": "#/definitions/Vehicle"
                }
            },
            "required": [
                "Starts", "Vehcle"
            ]
        }
    },
    "required": [
        "Manufacturer"
    ],
    

    "definitions": {
        "Vehicle": {
            "type": "object",
            "properties": {
                "Manufacturer": {
                    "type": [
                        "string",
                        "null"
                    ]
                },
                "Model": {
                    "type": [
                        "string",
                        "null"
                    ]
                },

                "Opening": {
                    "type": [
                        "string",
                        "null"
                    ]
                },
                "PanelQuantity": {
                    "type": [
                        "string",
                        "null"
                    ]
                },

                "Principle": {
                    "type": [
                        "string",
                        "null"
                    ]
                },
                "Type": {
                    "type": [
                        "string",
                        "null"
                    ]
                }
            },
            "required": ["Manufacturer", "Model", "Opening", "Quantity", "Principle", "Type"]
    }
    }
}

谢谢,巴斯卡

标签: aws-api-gatewayjsonschemajson-schema-validator

解决方案


听起来您想添加"additionalProperties": false- 如果存在任何其他未在properties.


推荐阅读