简体   繁体   English

嵌套对象中的JSON Schema v4“必需”

[英]JSON Schema v4 “required” in nested object

I tried searching, but I'm not quite sure how to put this in words! 我试着搜索,但我不太确定如何用文字说话! The point of confusion is how "required" works in JSON schema v4 when there are nested key values with the same name . 混淆点是当存在具有相同名称的嵌套键值时,“必需”如何在JSON模式v4中工作。

For example, this schema: 例如,这个架构:

{
    "Root": {
        "type": ["array", "null"],
        "items": {
            "type": "object",
            "properties": {
                "LevelOne": {
                    "required": ["id", "name", "LevelOneRepeat"],
                    "id": {
                        "type": "string"
                        },
                    "name": {
                        "type": "string"
                        },
                    "LevelOneRepeat": {
                        "type": ["array", "null"],
                        "items": {
                            "type": "object",
                            "properties": {
                                "required": ["id", "name"],
                                "id": {
                                    "type": "string"
                                    },
                                "name": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Inside LevelOne, I have a required for "id", "name", and "LevelOneRepeat". 在LevelOne中,我有“id”,“name”和“LevelOneRepeat”的必需项。 However, inside LevelOneRepeat, I also have a required for "id" and "name". 但是,在LevelOneRepeat中,我还需要“id”和“name”。

Does required only check for elements in the same level, or also all child elements? 是否只需检查同一级别的元素,还是所有子元素? Do I need to include the required inside LevelOneRepeat if the key values required (same name) are already listed in the level above? 如果所需的键值(同名)已在上面的级别列出,我是否需要在LevelOneRepeat中包含所需的内容?

I tested my JSON with my schema, but I might've messed up my code somewhere as no required are working anymore. 我用我的架构测试了我的JSON,但是我可能已经搞砸了我的代码,因为没有必要的工作了。

You have a couple of issues with your schema that is probably what has led to your confusion about how required works. 您的架构存在一些问题,这可能导致您对required工作的困惑。

Here is the corrected schema. 这是更正的架构。

{
    "type": ["array", "null"],
    "items": {
        "type": "object",
        "properties": {
            "LevelOne": {
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "name": { "type": "string" },
                    "LevelOneRepeat": {
                        "type": ["array", "null"],
                        "items": {
                            "type": "object",
                            "properties": {
                                "id": { "type": "string" },
                                "name": { "type": "string" }
                            },
                            "required": ["id", "name"]
                        }
                    }
                },
                "required": ["id", "name", "LevelOneRepeat"],
            }
        }
    }
}

The first problem is with the way you define nested objects. 第一个问题是您定义嵌套对象的方式。 The value of each property must be a schema. 每个属性的值必须是架构。 See how I changed the LevelOne definition to see how to correctly define a nested object. 了解我如何更改LevelOne定义以了解如何正确定义嵌套对象。

The second problem is that have the required keyword in the wrong place. 第二个问题是将required关键字放在错误的位置。 It should be on the same level as the properties keyword, not nested within the properties object. 它应该与properties关键字位于同一级别,而不是嵌套在properties对象中。 This is why your required constraints weren't working. 这就是您required约束不起作用的原因。 See how I changed the LevelOne and LevelOneRepeat definitions. 了解我如何更改LevelOneLevelOneRepeat定义。

Once you fix these problems, hopefully it should be more clear that the required keyword only applies to the schema it is defined in. It does not apply to any parent or child schema. 一旦解决了这些问题,希望更清楚的是, required关键字仅适用于它所定义的模式。它不适用于任何父模式或子模式。

The id in LevelOne and LevelOneRepeat are only coincidentally related. LevelOneLevelOneRepeat中的id只是巧合相关。 They have to be independently specified as required . 他们必须为独立地指定required

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM