Schemars

Generate JSON Schema documents directly from Rust types

Library
Cargo
v1.2.3
1,422 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
75 /100 Good
Development Activity 64
Maintenance 68
Community 68
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
81 /100 Excellent
Architecture 82
Code Quality 85
Innovation 75
Learning Curve 80

Schemars derives JSON Schema documents from Rust structs and enums with a single #[derive(JsonSchema)] macro, reusing the same field attributes (rename, skip, default) that serde already understands so your schema and your serialization stay in sync automatically. It ships JsonSchema implementations for most of the standard library plus popular crates (chrono, uuid, bytes, indexmap, rust_decimal) behind feature flags.

Because the generated schema is built from actual Rust type definitions rather than hand-written separately, Schemars is commonly used to produce OpenAPI-compatible schemas, validate config files, or describe tool/function-call interfaces for LLM integrations without maintaining a second schema definition by hand.

What You Get

  • A #[derive(JsonSchema)] macro plus schema_for!/schema_for_value! macros for one-line schema generation
  • Automatic reuse of serde attributes (rename, skip_serializing_if, default) so schema and serialization never drift
  • Built-in JsonSchema implementations for most std types and, via feature flags, chrono, uuid, bytes, indexmap, rust_decimal, and more
  • A Generator/SchemaSettings API for customizing schema draft version, $ref strategy, and inline-vs-referenced definitions
  • no_std-compatible core so it can run in embedded or constrained environments

Common Use Cases

  • Generating OpenAPI-compatible request/response schemas for a Rust web API (Axum, Actix, Rocket) alongside existing serde models
  • Producing JSON Schema tool/function definitions for LLM function-calling from native Rust structs
  • Validating configuration files against a schema derived straight from the config struct definition
  • Publishing a public JSON Schema for a data format defined by a Rust type, without maintaining it by hand

Under The Hood

Architecture: schemars/src/lib.rs defines the JsonSchema trait and re-exports the derive macro from the sibling schemars_derive proc-macro crate; generate.rs and schema.rs implement the actual schema-building logic and settings (draft version, $ref inlining), while json_schema_impls/ provides JsonSchema implementations for every supported type, gated behind per-crate feature flags (chrono04, uuid1, indexmap2, etc.) so consumers only compile in what they use.

Tech Stack: Pure Rust, no_std-compatible core (categories = ["encoding", "no-std"] in Cargo.toml) built on serde/serde_json plus small utility crates (dyn-clone, ref-cast); optional integrations use versioned feature-flag naming (e.g. chrono04, bigdecimal04) to support multiple major versions of dependent crates simultaneously.

Code Quality: The schemars crate ships 61+ integration/unit test files covering type-by-type schema output (including snapshot-style expected-JSON comparisons and an update-tests.sh regeneration script), reflecting the project’s need for byte-stable schema output across releases; clippy.toml enforces lint discipline project-wide.

API Design: The primary entry point is a single derive macro plus one macro call (schema_for!(MyType)), so most users never touch the Generator/SchemaSettings customization API at all — advanced schema-shape control (draft version, $ref policy) is opt-in rather than required, keeping the common case a two-line change to an existing serde-derived struct.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers