@@ -14,7 +14,9 @@ It handles:
1414- loading a ` confique ` schema into a directly usable config object through
1515 Figment runtime providers
1616- ` config-template ` , ` completions ` , and ` install-completions ` command handlers
17+ - Draft 7 JSON Schema generation for editor completion and validation
1718- config template generation for YAML, TOML, JSON, and JSON5
19+ - schema directives for TOML and YAML templates without adding runtime fields
1820- recursive include traversal
1921- ` .env ` loading before environment values are merged
2022- source tracking through Figment metadata
@@ -36,6 +38,7 @@ implementing `ConfigSchema` to expose the schema's include field.
3638rust-config-tree = " 0.1"
3739confique = { version = " 0.4" , features = [" yaml" , " toml" , " json5" ] }
3840figment = { version = " 0.10" , features = [" yaml" , " env" ] }
41+ schemars = { version = " 1" , features = [" derive" ] }
3942serde = { version = " 1" , features = [" derive" ] }
4043clap = { version = " 4" , features = [" derive" ] }
4144```
@@ -198,6 +201,19 @@ output format is inferred from the output path:
198201- ` .json` and `.json5` generate JSON5-compatible templates
199202- unknown or missing extensions generate YAML
200203
204+ Use `write_config_schema` to create one Draft 7 JSON Schema that can be shared
205+ by TOML, YAML, and JSON configuration files :
206+
207+ ` ` ` rust
208+ use rust_config_tree::write_config_schema;
209+
210+ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
211+ write_config_schema::<AppConfig>("schemas/myapp.schema.json")?;
212+
213+ Ok(())
214+ }
215+ ` ` `
216+
201217Use `write_config_templates` to create a root template and every template file
202218reachable from its include tree :
203219
@@ -211,6 +227,28 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
211227}
212228` ` `
213229
230+ Use `write_config_templates_with_schema` when generated TOML and YAML templates
231+ should bind that schema for IDE completion and validation :
232+
233+ ` ` ` rust
234+ use rust_config_tree::write_config_templates_with_schema;
235+
236+ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
237+ write_config_templates_with_schema::<AppConfig>(
238+ "config.toml",
239+ "config.example.toml",
240+ "schemas/myapp.schema.json",
241+ )?;
242+
243+ Ok(())
244+ }
245+ ` ` `
246+
247+ TOML targets receive `#:schema ./schemas/myapp.schema.json`. YAML targets
248+ receive `# yaml-language-server: $schema=./schemas/myapp.schema.json`. JSON and
249+ JSON5 targets intentionally do not receive a `$schema` field; bind them with
250+ editor settings such as VS Code `json.schemas`.
251+
214252Template generation chooses its source tree in this order :
215253
216254- an existing config path
@@ -262,6 +300,7 @@ sections. Nested children are placed under their parent file stem, for example
262300Flatten `ConfigCommand` into your existing clap command enum to add :
263301
264302- ` config-template`
303+ - ` config-schema`
265304- ` completions`
266305- ` install-completions`
267306
@@ -285,9 +324,10 @@ use std::path::PathBuf;
285324
286325use clap::{Parser, Subcommand};
287326use confique::Config;
327+ use schemars::JsonSchema;
288328use rust_config_tree::{ConfigCommand, ConfigSchema, handle_config_command, load_config};
289329
290- #[derive(Debug, Config)]
330+ #[derive(Debug, Config, JsonSchema )]
291331struct AppConfig {
292332 #[config(default = [])]
293333 include: Vec<PathBuf>,
@@ -337,7 +377,11 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
337377
338378` config-template --output <path>` writes templates to the selected path. If no
339379output path is provided, it writes `config.example.yaml` in the current
340- directory.
380+ directory. Add `--schema <path>` to bind TOML and YAML templates to a generated
381+ JSON Schema without adding a runtime `$schema` field.
382+
383+ ` config-schema --output <path>` writes a Draft 7 JSON Schema. If no output path
384+ is provided, it writes `schemas/config.schema.json`.
341385
342386` completions <shell>` prints completions to stdout.
343387
0 commit comments