Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

I tested my JSON with my schema, but I might've messed up my code somewhere as no required are working anymore.

like image 701
Kevin Avatar asked Jul 27 '15 22:07

Kevin


People also ask

What is required in JSON Schema?

In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”. In Python, "objects" are analogous to the dict type. An important difference, however, is that while Python dictionaries may use anything hashable as a key, in JSON all the keys must be strings.

Do we need schema for JSON?

JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data.

What is JSON Schema additionalProperties?

The value of "additionalProperties" MUST be a boolean or an object. If it is an object, it MUST also be a valid JSON Schema. The value of "properties" MUST be an object. Each value of this object MUST be an object, and each object MUST be a valid JSON Schema. The value of "patternProperties" MUST be an object.

Which is the required object to run the validator on the schema?

An object representing the validator's meta schema (the schema that describes valid schemas in the given version). A jsonschema. TypeChecker that will be used when validating type keywords in JSON schemas.


1 Answers

You have a couple of issues with your schema that is probably what has led to your confusion about how required works.

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.

The second problem is that have the required keyword in the wrong place. It should be on the same level as the properties keyword, not nested within the properties object. This is why your required constraints weren't working. See how I changed the LevelOne and LevelOneRepeat definitions.

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.

like image 100
Jason Desrosiers Avatar answered Oct 03 '22 06:10

Jason Desrosiers