rydantic 0.7.0-beta.2

Type-driven validation, JSON Schema, Cap'n Proto, Axum and SQLx models for Rust
Documentation

Rydantic

Validated Rust models, from JSON to HTTP, SQL, and Cap'n Proto.

CI crates.io License: MIT

Documentation · Quick start · Stable API · Homepage

Define a model once. Keep parsing, normalization, validation, and serialization rules next to its fields. Add the integrations your application needs without maintaining a separate validation layer for every boundary.

#[derive(rydantic::Model)]
struct User {
    #[model(trim, min_length = 3)]
    name: String,
    #[model(coerce, ge = 18, le = 120)]
    age: u8,
}

What you get

Capability What it does
Validated JSON Explicit coercion, string normalization, defaults, nested models, and structured errors.
Serde + JSON Schema Generated serialization/deserialization and input/output schemas from the same model.
HTTP with Axum Validated request extractors, responses, and structured rejections.
SQL with SQLx Model metadata, validated CRUD, queries, and migration planning for SQLite, PostgreSQL, and MySQL.
Native Cap'n Proto Generate schemas and official Rust bindings, or connect an existing protocol.
More model shapes Borrowed strings, enums, generics, recursive models, async validators, and streaming JSON.

Integrations are opt-in Cargo features. The default derive feature covers the core model API.

Start with a validated model

For the current stable 0.6.0 release:

[dependencies]
rydantic = "0.6.0"
use rydantic::{Model, SchemaMode};

#[derive(Debug, Model)]
struct User {
    #[model(trim, min_length = 3)]
    name: String,
    #[model(coerce, ge = 18, le = 120)]
    age: u8,
}

fn main() -> Result<(), rydantic::ValidationErrors> {
    let user = User::parse_json(r#"{"name":" Alice ","age":"25"}"#)?;

    assert_eq!(user.name, "Alice");
    assert_eq!(user.age, 25);
    println!("{}", user.dump_json()?);
    // {"age":25,"name":"Alice"}

    let schema = User::json_schema(SchemaMode::Output);
    assert!(schema.is_object());
    Ok(())
}

coerce makes conversion explicit. Invalid fields produce structured errors with locations and codes. Model also generates Serde implementations: deserialization validates input, and serialization revalidates the current object.

Continue with validation rules, model shapes, or JSON Schema.

Choose your integrations

Enable features on the version you selected:

# Stable HTTP + PostgreSQL example
rydantic = { version = "0.6.0", features = ["axum", "postgres"] }
Build with Features Guide
HTTP APIs axum Axum
Relational data sqlite, postgres, or mysql SQL models · Migrations
Binary protocols capnp Generated schemas · Existing schemas
Dates and exact decimals chrono, decimal Dates and Decimal
Runtime JSON Schema dynamic JSON Schema

See the complete feature reference. Automatic Cap'n Proto binding generation also requires the official capnp compiler on PATH.

Stable and beta

Channel Version Use it for
Stable 0.6.0 Published API, with versioned source documentation and Rust API reference.
Source candidate 0.7.0-beta.2 Testing opt-in layered settings and exact zero-exponent normalization, with the beta.1 derive contract and migration protections.

A version in this branch is not a publication announcement. Check published versions before installing a candidate. Once published, opt in exactly with rydantic = "=0.7.0-beta.2". The guides follow the working source; use the versioned links above for stable behavior.

Breaking upgrade: 0.7.0-beta.1 removes automatic Model derive insertion inside #[rydantic::capnp_models]. Add an explicit #[derive(rydantic::Model)] to every model struct and enum, including nested models. The beta also rejects unsupported custom SQLite index layouts before a table rebuild. Read the changelog and migration guidance before upgrading.

Breaking changes are allowed in this early-stage library. Application owners are responsible for backups, migration review, and deployment validation. Unsupported SQLite layouts need a reviewed migration, not removal of constraints to bypass the guard. We do not describe destructive changes as lossless.

Know the boundaries

  • JSON Schema cannot express every normalization, coercion, or custom-validation rule. Schema validation and model validation are not interchangeable.
  • Reusing models reduces repeated rules; it does not replace separate public, internal, and storage models when their contracts differ.
  • Native Cap'n Proto bindings use concrete, owned models. Keep protocol IDs and ordinals stable when wire compatibility matters.
  • Conditional definitions inside capnp_models have a known limitation; this beta does not claim general conditional schema generation.

Explore and contribute

The workspace contains the rydantic runtime, rydantic-derive macros, and executable integration fixtures. Browse examples for tool arguments, HTTP, databases, and binary protocols.

cargo run -p rydantic --example user
cargo run -p rydantic --example tool_call -- --schema
cargo test --workspace --all-features --locked
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
cargo fmt --all -- --check

Use Rust 1.98.1 to match the repository's tested toolchain, and install the official Cap'n Proto compiler for all-feature checks. Database integration tests require their configured services; see CI for the full matrix. To work on the site, see documentation development.

Found a bug? Open an issue with a minimal model, input, expected behavior, and crate version. Small fixes with regression tests are welcome.


MIT license