首页 > 解决方案 > 数组中严格对象的 JSON Schema

问题描述

我创建了一个 JSON 模式来验证一个简单的 JSON 文件。好消息是它以我想要的方式进行验证,因为任何数量的预订元素可以以任何顺序出现,并且在每种类型的预订元素中都不允许有额外的属性。

理想情况下,我想删除 JSON 模式中的 bookingElement 对象(id、type、depair、destair、city)中可能属性的完整列表,只留下 oneOf 列表,这些列表清楚地显示了每种不同类型中允许哪些字段的元素。

任何人都可以提供没有仍然适用严格规则的完整列表的架构版本吗?

这是 JSON:

{
    "bookingElements": [
        {
            "id" : "00003",
            "type" : "flight",
            "depair" : "LHR",
            "destair" : "CDG"
        },
        {
            "id" : "00008",
            "type" : "hotel",
            "city" : "Paris"
        }
    ]
}

架构是:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "bookingElements": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "type": {
                        "type": "string"
                    },
                    "depair": {
                        "type": "string"
                    },
                    "destair": {
                        "type": "string"
                    },
                    "city": {
                        "type": "string"
                    }
                },
                "oneOf": [
                    {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "type": {
                                "type": "string"
                            },
                            "depair": {
                                "type": "string"
                            },
                            "destair": {
                                "type": "string"
                            }
                        }
                    },
                    {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "type": {
                                "type": "string"
                            },
                            "city": {
                                "type": "string"
                            }
                        }
                    }
                ]
            }
        }
    },
    "required": [
        "bookingElements"
    ]
}

理想情况下,JSON 模式看起来更接近于以下内容:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "bookingElements": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": false,
                "oneOf": [
                    {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "type": {
                                "type": "string"
                            },
                            "depair": {
                                "type": "string"
                            },
                            "destair": {
                                "type": "string"
                            }
                        }
                    },
                    {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "type": {
                                "type": "string"
                            },
                            "city": {
                                "type": "string"
                            }
                        }
                    }
                ]
            }
        }
    },
    "required": [
        "bookingElements"
    ]
}

标签: jsonschema

解决方案


推荐阅读