ToSchema - JSON Schema Derive Macro
A procedural macro that automatically generates JSON Schema from Rust structs. Nested structs are fully inlined, producing a complete self-contained schema.
Usage
Add to your Cargo.toml:
[]
= { = "../toschema" } # or appropriate path
Then derive ToSchema on your structs:
use ToSchema;
Output Example (NodeSpec with nested types)
For a complex struct with nested types, the schema is fully inlined:
Supported Types
| Rust Type | JSON Schema Type |
|---|---|
String, &str |
"string" |
bool |
"boolean" |
i8, i16, i32, i64, i128, isize |
"integer" |
u8, u16, u32, u64, u128, usize |
"integer" |
f32, f64 |
"number" |
Vec<T> |
"array" with items |
Option<T> |
Type with ["type", "null"] |
HashMap<K, V>, BTreeMap<K, V> |
"object" with additionalProperties |
| Custom structs | Fully inlined object schema |
| Enums (unit variants) | "enum": ["Variant1", "Variant2", ...] |
| Enums (with data) | "oneOf": [...] with variant schemas |
Enum Support
Enums are fully supported with proper JSON Schema generation:
Simple Enums (Unit Variants)
// Generates:
// {
// "$schema": "http://json-schema.org/draft-07/schema#",
// "title": "Color",
// "enum": ["Red", "Green", "Blue"]
// }
Complex Enums (With Data)
Enums with tuple or struct variants use oneOf:
// Generates:
// {
// "$schema": "http://json-schema.org/draft-07/schema#",
// "title": "Message",
// "oneOf": [
// {"const": "Quit"},
// {"type": "object", "properties": {"Move": {"type": "object", "properties": {"x": {"type": "integer"}, "y": {"type": "integer"}}, "required": ["x", "y"]}}, "required": ["Move"]},
// {"type": "object", "properties": {"Write": {"type": "string"}}, "required": ["Write"]},
// {"type": "object", "properties": {"ChangeColor": {"type": "array", "items": [...], "minItems": 3, "maxItems": 3}}, "required": ["ChangeColor"]}
// ]
// }
Enums in Structs
Enums can be used as field types in structs:
Methods Added
The derive macro adds these methods to your struct:
fn json_schema() -> String- Returns compact JSON Schema with$schemaheaderfn json_schema_pretty() -> String- Returns formatted JSON Schema with indentationfn schema_object() -> String- Returns just the object schema (for embedding)
AI Agent Instructions
When using this crate to generate schemas for AI agents:
-
Add the derive macro to any struct that needs schema generation:
use ToSchema; -
Call
json_schema_pretty()to get a human-readable schema:let schema = json_schema_pretty; -
For nested structs, each struct needs its own
#[derive(ToSchema)]: -
Optional fields are automatically detected and excluded from
required: -
The schema is self-contained - all nested types are fully inlined, no
$refpointers. This makes the schema directly usable by AI tools without needing to resolve references. -
The schema follows JSON Schema draft-07 and can be used with any JSON Schema validator or AI tool that accepts JSON Schema definitions.
Limitations
- Tuple structs are treated as arrays
- Unit structs are treated as null type
- Unions are not supported
- No support for custom schema attributes (yet)
- Recursive types will cause infinite loops (not currently detected)