"$schema" を設定ファイルのフィールドとして持ち、JSON Schema を効かせるパターン
整理
VS Code では JSON Schema を利用したエディタ支援に対応している
設定方法はいくつかあるが、そのうちの一つが設定ファイル自身に "$schema"というフィールドを持つパターン
値は string で、 JSON Schema ファイルの URI を表す(ローカルパスだったり https: 始まりだったりする)
Editing JSON with Visual Studio Code
これはJSON Schema の機能ではなく、VS Code の機能
Note that this syntax is VS Code-specific and not part of the JSON Schema specification. Adding the $schema key changes the JSON itself, which systems consuming the JSON might not expect, for example, schema validation might fail. If this is the case, you can use one of the other mapping methods.
Editing JSON with Visual Studio Code
ツール側は必ずしもこのフィールドを読まない
ツールはこれを許容するようスキーマを定義すればよい
あとは JSON Schema をホストしておけば、ユーザへ入力支援を勝手に与えられるという仕組み
"$schema" を利用できるツールをいくつか挙げてみる
TypeScript (tsconfig.json)
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#schema
Deno (deno.{json,jsonc})
https://docs.deno.com/runtime/fundamentals/configuration/#json-schema
https://github.com/denoland/deno/blob/main/cli/schemas/config-file.v1.json
Prettier (prettierrc.json)
https://prettier.io/docs/configuration.html#configuration-schema
Turborepo (turbo.json)
https://turborepo.com/docs/getting-started/editor-integration#json-schema-for-turbojson
どこの Schema を使うか
だいたい以下の3種類
1. 自分でホストしておくパターン
2. JSON Schema Store にホストするパターン
3. ライブラリに JSON Schema を含めておき、node_modules 内を参照するパターン