5

When editing settings JSON file, Visual Studio Code suggests property names. I am assuming it's using a JSON Schema internally.

It's a very nice feature and can be useful for editing other JSON files that have a JSON Schema. I can't find an option for adding schema to a JSON file. If it's an internal feature, where it is located in the source code and can we build an extension that surfaces the feature so we can provide JSON Schema for editing a JSON? Using $schema property doesn't seem to be working.

enter image description here

4 Answers 4

3

I found out it's possible to provide those JSON Schemas. In settings.json there are a couple of examples for Bower and package.json

They are under "json.schemas".

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

Comments

3

If you're trying to set the schema for a JSON file that should be using VSCode's settings schema, you can find your editor's current schema at vscode://schemas/settings/user.

To set that as the active schema for a JSON file, you could add this to the file:

"$schema": "vscode://schemas/settings/user",

Comments

1

We can also do it this way:

In your .json file, just after the first opening curly bracket, add $schema property that is the path to the schema file:

{
    "$schema": "../node_modules/sp-build-tasks/schema/v1/sppp.json",
     startTypinghere...
}

More info

Comments

0

There are three ways to enable IntelliSense (and effectively, proper validation) for your JSON files:

  1. $schema property in the file itself.

    Using $schema property in your JSON does work in recent VS Code, however the culprit here is that you'll need to have $schema as an allowed property in the very same schema you're referencing, otherwise it won't be valid.

    Although this might feel like a little self-recursive bloat, it has the advantage that any users of that JSON will get a reference to the schema no matter what editor they are using.

    Reference: https://code.visualstudio.com/Docs/languages/json#_mapping-in-the-json

  2. Referencing schema in settings.json:

    "json.schemas": [
      {
        "fileMatch": [
          "/*.foo.json"
        ],
        "url": "./myschema.json"
      }
    ]
    

    Reference: https://code.visualstudio.com/Docs/languages/json#_mapping-to-a-schema-in-the-workspace

  3. Putting the schema directly into settings.json:

    "json.schemas": [
      {
        "fileMatch": [
          "/.myconfig"
        ],
        "schema": {
          "type": "object",
          "properties": {
            "name" : {
              "type": "string",
              "description": "The name of the entry"
            }
          }
        }
      }
    ]   
    

    https://code.visualstudio.com/Docs/languages/json#_mapping-to-a-schema-in-the-workspace

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.