1

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.

2
  • Is it applied / would you expect it to be applied on empty and/or absent value ? Would you expect [minItems] / [required] to influence behavior ? Thanks for any answer Commented Aug 26, 2024 at 11:44
  • 1
    @TestoTestini The example does validate as valid JSON schema but the VS Code json schema engine doesn't actually do anything with it. However just because the schema engine in VS Code ignores it doesn't mean a different platform would Commented Aug 28, 2024 at 0:10

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.