Crate json_schema_derive

Source
Expand description

Derive macro for generating JSON Schema from Rust types.

This crate provides a #[derive(JsonSchema)] macro that generates a JSON Schema for your types. It supports custom schema attributes via #[json_schema(...)] and optionally integrates with common serde attributes when the serde-compat feature is enabled.

§Example

use json_schema_derive::JsonSchema;

#[derive(JsonSchema)]
struct User {
    #[json_schema(comment = "User's name", minLength = 2)]
    name: String,
    /// User's age
    age: u32,
    tags: Vec<String>,
}

let schema = User::json_schema();

§Features

  • serde-compat: Enables compatibility with serde attributes for schema generation

§Serde Compatibility

When the serde-compat feature is enabled, the following serde attributes are supported:

  • #[serde(skip)] – Omits the field from the schema
  • #[serde(rename = "new_name")] – Renames the field in the schema
  • #[serde(flatten)] – Inlines nested struct fields
  • #[serde(tag = "...")] – Supports internally tagged enums
#[derive(JsonSchema)]
#[serde(tag = "type")]
enum Event {
    Login { user: String },
    Logout,
}

Traits§

JsonSchema
Trait for generating JSON Schema from a type.

Derive Macros§

JsonSchema