microsoft / vscode

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

[json] support setting schema associations at runtime #96120

Open ayqy opened 4 years ago

ayqy commented 4 years ago

Related PR: https://github.com/microsoft/vscode/pull/73424 Related FR: https://github.com/microsoft/vscode/issues/69868

Feature Request: Configure jsonValidation contribution point dynamically. Use Case: Twin json files like myFile.json and myFile.js, which file name is the same as another file under the same directory, but file extension name is normal .json. So, glob matching or content detection does not work well, dynamic configuration is required.

aeschli commented 4 years ago

How would the dynamic configurator know what schemes applies to which file?

ayqy commented 4 years ago

In my case, there are some twin files like this:

.
├── project.json
└── src
    ├── foo.js
    ├── foo.json
    ├── bar.js
    ├── bar.json
    └── non-match.js

I wanna to configure a specified JSON Schema to validate foo.js and bar.js under my project (detected from project.json at project root folder).

As you see, static glob is not working. Any workarounds or a more graceful solution will be great as well.

aeschli commented 4 years ago

One possible solution is to specify the schema in the JSON file "$schema": "..."

ayqy commented 4 years ago

Not really bad, but it's a little bit dirty for someone who reads and parses JSON key-values from the original file. And all consumers (such as our source code analysis service, native application...) have to take care of $schema from now on.

But anyway I think it's accepted for this case, so you can close this or take it into consideration for other clean approaches in the future. Thanks a lot.

dsherret commented 3 years ago

I have a use case for this. In my scenario, the schema of the JSON file changes based on the plugins specified, for example:

{
  "incremental": true,
  "lineWidth": 160,
  "json": {
    "indentWidth": 2,
  },
  "typescript": {
    "useTabs": true
  },
  "includes": [
    "**/*.{ts,tsx,js,jsx,json}"
  ],
  "plugins": [
    "https://plugins.dprint.dev/typescript-0.48.0.wasm",
    "https://plugins.dprint.dev/json-0.12.1.wasm"
  ]
}

Given the plugins specified, the schema of the plugin specific properties change (ex. in this case the "typescript" and "json" properties are added to the schema). I previously thought #98443 would solve my use case, but it has the problem of the "plugins" needing to be kept in sync with the "$schema" and it's annoying for the user to have to specify this.

Instead, it would be neat if the dprint vscode extension could programatically generate a schema based on the plugins in the plugins array and use that to help set the schema of the file (so this would be ideally programatically specified rather than a uri, but as a workaround I could create a web server route with some query parameters to make this work).

aeschli commented 3 years ago

@dsherret What you want can be done with a programmatic schema. See https://github.com/microsoft/vscode/issues/127812

dsherret commented 3 years ago

@aeschli oh great! That looks like it will work. Thanks so much!