yaml language server and nvim configuration

Viewed 525

I would like to use nvim lsp to validate an OpenAPI file.

Here are the steps I've been following:

  1. I installed yaml-language-server, and made sure it was available in the PATH
  2. I downloaded the OpenAPI schema from here, and stored it in my filesystem.
  3. I modified my existing init.vim to include the following:
lspconfig.yamlls.setup {
  on_attach = on_attach,
  flags = {
    debounce_text_changes = 150,
  },
  settings = {
    yaml = {
      schemas = {
        {
          fileMatch = { ".openapi.yaml" },
          url = "file:///[...]/openapi.schema.yaml"
        }
      },
      format = {
        enable = true,
        singleQuote = false,
        bracketSpacing = true
      },
      validate = true,
      completion = true
    }
  }
}
  1. I wrote a simple OpenAPI spec file, and opened it with nvim.

It seems that my nvim correctly hits the yaml-language-server to validate the yaml syntax, but it does not seem to validate against the schema.

One of the problem I have is that I don't have access to logs of nvim, or the yaml-language-server, to have some insight about what's going on.

1 Answers

it's been a while but I figured I'd give this a shot in case it helps anyone.

First, about your lack of access to the nvim log. You don't explain why that is but if your nvim is running, and it seems that it is, you should be able to get the log running this command:

:lua vim.cmd('e'..vim.lsp.get_log_path())

I think the way you have the settings.yaml.schemas node is not correct.

It might also be a problem that you are using a yaml format. I highly recommend you use json for open api schema validation. If you want to try this, just sub all the yaml for json (s/yaml/json), and replace your local yaml file with the json one.

See the example below if it works for you.

-- configure yamlls ls:
require('lspconfig')['yamlls'].setup {
  on_attach = on_attach,
  capabilities = capabilities,
  settings = {
    yaml = {
      schemas = {
        ["https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.yaml"] = "/*"
      }
    }
  }
}

If you really need to load a local file, either one of these formats should work:

["file:///home/$user/openapi.schema.yaml"] = "/*"

["../relative/path/openapi.schema.yaml"] = "/*"

Related