Skip to main content

write_config_schema

Function write_config_schema 

Source
pub fn write_config_schema<S>(output_path: impl AsRef<Path>) -> ConfigResult<()>
where S: JsonSchema,
Expand description

Writes a Draft 7 JSON Schema for the root config type.

The same generated schema can be referenced from TOML, YAML, JSON, and JSON5 configuration files. TOML and YAML templates bind it with editor directives. JSON and JSON5 templates bind it with a top-level $schema property. Generated schemas omit JSON Schema required constraints so editors provide completion without requiring every config field to exist in each partial config file.

§Type Parameters

§Arguments

  • output_path: Destination path for the generated JSON Schema.

§Returns

Returns Ok(()) after the schema file has been written.

§Examples

use confique::Config;
use rust_config_tree::{ConfigSchema, write_config_schema};
use schemars::JsonSchema;

#[derive(Config, JsonSchema)]
struct AppConfig {
    #[config(default = [])]
    include: Vec<std::path::PathBuf>,
    #[config(default = "demo")]
    mode: String,
}

impl ConfigSchema for AppConfig {
    fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
        layer.include.clone().unwrap_or_default()
    }
}

let path = std::env::temp_dir().join("rust-config-tree-write-schema-doctest.json");
write_config_schema::<AppConfig>(&path)?;

assert!(path.exists());