3

I've got a project with some relatively complicated .json file structure, and it's kind of pain to recall this structure, when I always need to reference another files to do it right. So I try to research this problem and find out that there are some solutions.

As I'm using coc.nvim for autocomplete, there is coc-json extension that should do what I want. At least documentation says it should. But after several hours of attempts I've got no result. Documentation is very poor, and absolutely no examples to reference to.

Some general words about structure I need to validate. All desired .json files contained in certain directory, they should be validated, but no other json outside of this directory. Filenames don't have any distinct patterns or suffixes, except for directory. Files themselves are arrays of objects.

From documentation FAQ:

How to add custom schema definitions/properties?
You have two choices:

use $schema in your json.
create json schema file and then configure json.schemes in your coc-settings.json, check out https://github.com/neoclide/coc-json/blob/master/package.json#L55

One of the described options is to use "$schema" field in JSON root, and this works in general. But as our file root is array, there is no way to specify $schema in it. To the next option.

Create json schema - easy, done. "configure json.schemes in your coc-settings.json" - how? What should be in it? package.json line 55 is referenced, and this line unrelated to schema as far as I can tell. There is 'json.schemas' field a little further with some JSON schema in it. What should I do with it? Ok, may be this schema describes structure of json.schemas in coc-settings.json. Several attempts - nothing. Let's try to copy this package.json part into project - no result after next hour of attempts.

There is one more mention of this problem in github issues section, but as far as I can tell, the answer references the approach above.

And even if above approach would succeed, it's a global setting. And I don't really need this schema anywhere beyond one project and one it's directory.

Also I've downloaded Vision plugin, but without settings it does nothing and really slows down vim.

Is there some examples of working setup? Or some more explanations on how to do it?

1 Answer 1

1

The json.schemas key in the CoC settings file is the way to go.

You're correct that the line reference to it is broken (since that URL doesn't reference a specific commit and the file has been updated, probably since that URL reference was posted), you can find an updated (and stable) link to it here.

While the json.schemas setting is global, it actually allows you to specify different schemas depending on specific file name patterns. That setting takes a JSON array of objects and each objects takes at least two keys, url being the URL to the schema (could be a https:// URL, or it could be a file:/// URL for a local file) and a fileMatch key, including a filename pattern that could match a file by extension or even by which directory the file lives in.

You can find examples of usage of the specific JSON schema (it's the schema to define which JSON schemas to use for which files) in src/catalog.json from the coc-json plug-in.

You can match by file extension, or by directory. If you match by directory, you could use a full absolute path, or you can use ** to include one or more directory components, which you should probably include at the beginning of the match when using the directory name where the files are directly stored only.

Below is an example (mostly inspired/extracted from the src/catalog.json file), showing a match by location (*.yml or *.yaml files inside a tasks directory) and a match by extension (files matching *.backportrc.json):

  "json.schemas": [
    {
      "name": "Ansible Role",
      "description": "Ansible role task files",
      "url": "https://json.schemastore.org/ansible-role-2.9",
      "fileMatch": ["**/tasks/*.yml", "**/tasks/*.yaml"],
    },
    {
      "name": ".backportrc.json",
      "description": "Backport configuration file",
      "fileMatch": [".backportrc.json"],
      "url": "https://json.schemastore.org/backportrc"
    },
  ],
2
  • 1
    Ok, so the root of my problem was in not setting url correctly (without file:///), then I just failed to properly combine url and fileMatch. Now, following your example and explanation, I finally managed to setup working solution, first with absolute path and then with relative to project root. Thank you!
    – MadBrozzeR
    Dec 17, 2021 at 10:41
  • @MadBrozzeR Awesome! If you think my example at the end can be expanded to be more illustrative, please edit my answer to add a few more cases... For example, adding an entry with a file:/// URL, or a fileMatch that was tricky to get right. Thanks!
    – filbranden
    Dec 17, 2021 at 15:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.