microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.99k stars 29.19k forks source link

[json] Local JSON schemas should find each other based on their id fields #139752

Open mitar opened 2 years ago

mitar commented 2 years ago

I have a JSON schema which looks like:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$id": "https://example.com/item.json",
  "allOf": [{
    "$ref": "https://example.com/definitions.json#/$defs/object"
  }]
}
{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$id": "https://example.com/definitions.json",
  "$defs": {
    "object": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        }
      }
    }
  }
}

I would like that I would be able to edit them in VSCode with all $refs resolving to their local counterparts. This can be done by VSCode parsing $id field and determining the mapping from the remote URL to local file based on $id.

JacobRuby commented 1 year ago

Looking for this as well. I have a repository of schemas and documents using those schemas, except they're organized into file structures. This means whenever I go another folder deeper into my documents, I have to add a ../ to the $schema and $refs in my document to reference the same local file. It would be nice to have all schemas in a workspace loaded to a list by their $ids that you can auto-complete when you start typing in your domain name in a $ref or $schema.

njakob commented 1 year ago

I'm also looking for a simple solution for my JSON schemas to be resolved locally and I wanted to collect my findings.

It was suggested in a related issue (https://github.com/microsoft/vscode/issues/174672) to use the json.schemas setting with the file:// protocol. However, I didn't manage to have VSCode load the correct schema when trying to reference a relative file since:

  1. Relative paths are not possible with a file:// protocol
  2. Using ${workspaceFolder} is not possible within schema references (https://github.com/microsoft/vscode/issues/166438)
  3. Using relative paths directly will simply be appended to the HTTPS URI (I'm not sure if this is an expected behavior)

As a note, it is possible to use fileMatch field but requires a specific path naming convention which is not always desired.

Resolving all schema by IDs as suggested by this issue would be the simplest! An alternative could be to go in the direction suggested by https://github.com/microsoft/vscode/issues/166438 and leverage the $ref field within the json.schemas configuration field.

{
    "json.schemas": [
        {
            "url": "https://example.com/item.json",
            "schema": {
                "$ref": "file://${workspaceFolder}/schemas/items.json"
            }
        }
    ]
}
tthiery commented 2 months ago

@aeschli I am hitting the same roadblock as the other commenters. Failed also with everything @njakob did.

Can we kindly ask to prioritize this work.