Justin Ohms
Justin Ohms

Reputation: 3543

Is there a way to add a default to a json schema array

I just want to understand if there is a way to add a default set of values to an array. (I don't think there is.)

So ideally I would like something like how you might imagine the following working. i.e. the fileTypes element defaults to an array of ["jpg", "png"]

            "fileTypes": {
                "description": "The accepted file types.",
                "type": "array",
                "minItems": 1,
                "items": {
                    "type": "string",
                    "enum": ["jpg", "png", "pdf"]
                },
                "default": ["jpg", "png"]
            },

Of course, all that being said... the above actually does seem to be validate as json schema however for example in VS code this default value does not populate like other defaults (like for strings) populate when creating documents.

Upvotes: 1

Views: 4064

Answers (1)

Joey V.
Joey V.

Reputation: 1926

It appears to be valid based on the spec.

9.2. "default" 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.

See https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.2

It's up to the tooling to take advantage of that keyword in the JSON Schema and sounds like VS code is not.

Upvotes: 3

Related Questions