| Crates.io | json-schema-derive |
| lib.rs | json-schema-derive |
| version | 0.0.2 |
| created_at | 2025-04-07 10:28:46.977381+00 |
| updated_at | 2025-04-07 10:36:14.684578+00 |
| description | JSON Schema derive macro with arbitrary fields support |
| homepage | |
| repository | https://github.com/Pingid/json-schema-derive |
| max_upload_size | |
| id | 1623929 |
| size | 56,949 |
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.
Add to your Cargo.toml:
[dependencies]
json-schema-derive = { version = "0.0.1", features = ["serde-compat"] }
use json_schema_derive::JsonSchema;
#[derive(JsonSchema)]
#[json_schema(description = "Application user")]
struct User {
#[json_schema(description = "User's name", minLength = 2)]
name: String,
age: u32,
tags: Vec<String>,
}
let schema = User::json_schema();
Use json_schema to add arbitrary JSON Schema metadata:
#[derive(JsonSchema)]
struct Product {
#[json_schema(description = "Product name", minLength = 3)]
name: String,
#[json_schema(minimum = 0, maximum = 1000)]
price: f64,
#[json_schema(values = ["new", "used", "refurbished"])]
condition: String,
}
When the serde-compat feature is enabled, the following serde attributes are supported for schema generation:
#[derive(JsonSchema, Serialize)]
struct Example {
#[serde(skip)] // Field is excluded from schema
internal: String,
#[serde(rename = "userName")] // Field is renamed in schema
name: String,
#[serde(flatten)] // Flattens nested struct into parent
nested: NestedStruct,
}
#[derive(JsonSchema, Serialize)]
#[serde(tag = "type")]
struct Animal {
Dog { name: String },
Cat { name: String },
}
serde-compat: Enables compatibility with serde attributes for schema generationMIT License