Search code examples
jsonschemadefault-value

Json schema provides a default for an object but not for its properties


I have a property somewhere in my JSON schema that is of type "object" and has a default value - an object with its properties and their values. Inside the object schema I have the properties listed and their types specified - but no defaults for them. The question is - in what will this property result if only one of the values is provided but non of the rest? Will they default to what they would if noting was provided?

"autoStart": {
            "type": "object",
            "default":{
                "foo": true,
                "bar": 3
            },
            "properties":{
                "foo": {
                    "type": "boolean"
                },
                "bar": {
                    "type": "number"
                }
            }
        }

If the provided json is

"autoStart": {
  "foo": false
}

what will happen to the "bar"?


Solution

  • What does default do? Nothing according to the specification

    default is an annotation keyword. An annotation is to provide information to an implementation or application so they can define their own additional behaviour. This means any behaviour which does ANYTHING with default is implementation or application specific.

    Let's look at what the spec says...

    There are no restrictions placed on the value of this keyword. When multiple occurrences of this keyword are applicable to a single sub-instance, implementations SHOULD remove duplicates.

    This keyword can be used to supply a default JSON value associated with a particular schema. It is RECOMMENDED that a default value be
    valid against the associated schema.

    https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-10.2

    Besides annotation collection and return, there's nothing an implementation should be expected to DO with an annotation keyword, apart from that which it defines in its own documentation.