JSON Schema v4 "required" in nested object

I tried searching, but I'm not really sure how to put it in words! The point of confusion is how "required" works in the JSON v4 schema when there are nested key values โ€‹โ€‹of the same name .

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 the required "id", "name" and "LevelOneRepeat". However, inside LevelOneRepeat, I also have the required "id" and "name".

Is it only required to validate the elements at the same level as well as all children? Do I need to include the required one inside the LevelOneRepeat if the required key values โ€‹โ€‹(same name) are already specified at the level above?

I tested my JSON with my schema, but I could mess up my code somewhere as it doesn't need to work anymore.

+3


source to share


2 answers


You have a couple of problems with your circuit, which has probably led to your confusion as to how it works required

.

Here is the adjusted diagram.

{
    "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 how you define nested objects. The value of each property must be a schema. See how I changed the definition LevelOne

to see how to correctly define the nested object.

The second problem is that the keyword required

is in the wrong place. It must be at the same level as the keyword properties

, but not nested within an object properties

. This is why your constraints required

didn't work. See how I changed the definitions LevelOne

and LevelOneRepeat

.

Once you fix these issues, hopefully it should be clearer that the keyword required

only applies to the schema in which it is defined. It does not apply to any parent or child schema.

+7


source


id

in LevelOne

and LevelOneRepeat

are related only by coincidence. They must be independently specified as required

.



0


source







All Articles