Search code examples
visual-studio-codevscode-extensionsjsonschema

How do I assign a JSON Schema to a YAML file in my VS Code extension?


I am writing a CLI tool that will have a fairly complex YAML configuration, so I wanted to create a VS Code extension to support that with completion provided by the JSON Schema and some snippets, but it seems not to work. I thought it would be useful to define a custom extension ".lake" for my config file.

This is my package.json:

{
  "name": "lake-config",
  "displayName": "Lake Configuration Helper",
  "publisher": "essay97",
  "description": "Provides schema validation and snippets to configure Lake",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.80.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [],
  "main": "./extension.js",
  "contributes": {
    "languages": [
      {
        "id": "yaml",
        "extensions": [
          ".lake"
        ],
        "aliases": [
          "Lake Config"
        ]
      }
    ],
    "jsonValidation": [
      {
        "fileMatch": "**/*.lake",
        "url": "./lake-schema.json"
      }
    ]
  },
  "scripts": {
    "lint": "eslint .",
    "pretest": "npm run lint",
    "test": "node ./test/runTest.js"
  },
  "devDependencies": {
    "@types/vscode": "^1.84.0",
    "@types/mocha": "^10.0.3",
    "@types/node": "18.x",
    "eslint": "^8.52.0",
    "glob": "^10.3.10",
    "mocha": "^10.2.0",
    "typescript": "^5.2.2",
    "@vscode/test-electron": "^2.3.6"
  }
}

When I try to open an extension development host with F5 and open a .lake file, I get the YAML syntax highlighting, but not the JSON Schema, the bottom bar says "no json schema".

What am I missing? I even tried to set the jsonValidation.url to a random JSON schema from the internet to make sure it was not a problem in my schema but it's still not working.

Also: I'm not sure if I should depend on redhat.vscode-yaml. extension and what extensionDependencies does exactly.


Solution

  • I ended up finding out that since the file I am trying to validate is YAML, it is handled by the YAML extension. In order to make it work, I added to the package.json the yamlValidation block inside the contributes. The syntax is the exact same of jsonValidation.