80 releases (14 breaking)

new 0.15.1 Apr 19, 2024
0.14.5 Mar 14, 2024
0.12.12 Dec 10, 2023
0.12.9 Nov 30, 2023
0.11.0 Jul 3, 2023

#195 in Encoding

Download history 573/week @ 2023-12-23 659/week @ 2023-12-30 892/week @ 2024-01-06 601/week @ 2024-01-13 781/week @ 2024-01-20 599/week @ 2024-01-27 1137/week @ 2024-02-03 745/week @ 2024-02-10 557/week @ 2024-02-17 1019/week @ 2024-02-24 932/week @ 2024-03-02 1602/week @ 2024-03-09 1359/week @ 2024-03-16 503/week @ 2024-03-23 714/week @ 2024-03-30 865/week @ 2024-04-06

3,703 downloads per month
Used in 15 crates (10 directly)

MIT license

180KB
4.5K SLoC

Schematic

Schematic is a library that provides:

  • A layered serde-driven configuration system with support for merge strategies, validation rules, environment variables, and more!
  • A schema modeling system that can be used to generate TypeScript types, JSON schemas, and more!

Both of these features can be used independently or together.

cargo add schematic

Get started: https://moonrepo.github.io/schematic

Configuration

  • Supports JSON, TOML, and YAML based configs via serde.
  • Load sources from the file system or secure URLs.
  • Source layering that merge into a final configuration.
  • Extend additional files through an annotated setting.
  • Field-level merge strategies with built-in merge functions.
  • Aggregated validation with built-in validate functions (provided by garde).
  • Environment variable parsing and overrides.
  • Beautiful parsing and validation errors (powered by miette).
  • Generates schemas that can be rendered to TypeScript types, JSON schemas, and more!

Define a struct or enum and derive the Config trait.

use schematic::Config;

#[derive(Config)]
struct AppConfig {
	#[setting(default = 3000, env = "PORT")]
	port: usize,

	#[setting(default = true)]
	secure: bool,

	#[setting(default = vec!["localhost".into()])]
	allowed_hosts: Vec<String>,
}

Then load, parse, merge, and validate the configuration from one or many sources. A source is either a file path, secure URL, or code block.

use schematic::{ConfigLoader, Format};

let result = ConfigLoader::<AppConfig>::new()
	.code("secure: false", Format::Yaml)?
	.file("path/to/config.yml")?
	.url("https://ordomain.com/to/config.yaml")?
	.load()?;

result.config;
result.layers;

Schemas

Define a struct or enum and derive or implement the Schematic trait.

use schematic::Schematic;

#[derive(Schematic)]
struct Task {
	command: String,
	args: Vec<String>,
	env: HashMap<String, String>,
}

Then generate output in multiple formats, like JSON schemas or TypeScript types, using the schema type information.

use schematic::schema::{SchemaGenerator, TypeScriptRenderer};

let mut generator = SchemaGenerator::default();
generator.add::<Task>();
generator.generate(output_dir.join("types.ts"), TypeScriptRenderer::default())?;

Dependencies

~2–16MB
~194K SLoC