IDE Completions
English | 中文 | 日本語 | 한국어 | Français | Deutsch | Español | Português | Svenska | Suomi | Nederlands
Generated JSON Schemas can be used by TOML, YAML, JSON, and JSON5 config files.
They are generated from the same Rust type used by confique:
#![allow(unused)]
fn main() {
use confique::Config;
use schemars::JsonSchema;
#[derive(Debug, Config, JsonSchema)]
struct AppConfig {
#[config(nested)]
#[schemars(extend("x-tree-split" = true))]
server: ServerConfig,
}
}
Generate them with:
#![allow(unused)]
fn main() {
use rust_config_tree::write_config_schemas;
write_config_schemas::<AppConfig>("schemas/myapp.schema.json")?;
Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
}
This writes the root schema and section schemas such as
schemas/server.schema.json. Generated schemas omit required constraints so
completion works for partial config files without missing-field diagnostics.
The root schema omits split nested section properties, so split child section
completion is available only in files that bind the matching section schema.
Unmarked nested sections remain in the root schema.
Fields marked with x-env-only are omitted from generated schemas, so IDEs do not suggest secrets or other values that must come only from environment variables.
IDE schemas are for completion and basic editor checks, such as type, enum, and
unknown property checks supported by the generated schema. They do not decide
whether a concrete field value is legal for the application. Implement field
value validation in code with #[config(validate = Self::validate)], then run
it through load_config or config-validate. Required fields and final merged
config validation also use those runtime paths.
TOML
TOML files should bind the schema with a top-of-file #:schema directive:
#:schema ./schemas/myapp.schema.json
[server]
bind = "0.0.0.0"
port = 3000
Do not use a root $schema = "..." field in TOML. It becomes real config data
and can affect runtime deserialization. write_config_templates_with_schema
adds the #:schema directive automatically for TOML templates.
YAML
YAML files should use the YAML Language Server modeline:
# yaml-language-server: $schema=./schemas/myapp.schema.json
server:
bind: 0.0.0.0
port: 3000
write_config_templates_with_schema adds this modeline automatically for YAML
templates. Split YAML templates bind their section schema, for example
log.yaml binds ./schemas/log.schema.json.
JSON
JSON and JSON5 files can bind a schema with a top-level $schema property.
write_config_templates_with_schema adds it automatically for generated JSON
and JSON5 templates:
{
"$schema": "./schemas/myapp.schema.json"
}
Editor settings are still useful when a project does not want an in-file binding:
{
"json.schemas": [
{
"fileMatch": [
"/config.json",
"/config.*.json",
"/deploy/*.json"
],
"url": "./schemas/myapp.schema.json"
}
]
}
YAML can also be bound through VS Code settings:
{
"yaml.schemas": {
"./schemas/myapp.schema.json": [
"config.yaml",
"config.*.yaml",
"deploy/*.yaml"
]
}
}
The final layout is:
schemas/myapp.schema.json:
Root file fields only
schemas/server.schema.json:
Server section schema
config.toml:
#:schema ./schemas/myapp.schema.json
config.yaml:
# yaml-language-server: $schema=./schemas/myapp.schema.json
server.yaml:
# yaml-language-server: $schema=./schemas/server.schema.json
config.json:
"$schema": "./schemas/myapp.schema.json"
References: