🦀 Omni-Schema
Universal Schema Generator for Rust - One source of truth, multiple outputs.
omni-schema generates multiple schema formats from a single Rust struct/enum definition using derive macros. Define your types once, export to JSON Schema, OpenAPI, GraphQL, Protocol Buffers, TypeScript, and Avro.
✨ Features
- One Source of Truth: Define your types once in Rust, generate schemas for any format
- Multiple Output Formats: JSON Schema, OpenAPI 3.1, GraphQL SDL, Protocol Buffers, TypeScript, Avro
- Serde Compatible: Attribute syntax mirrors serde for easy adoption
- Rich Validation: Support for constraints like min/max length, patterns, ranges
- Zero Runtime Overhead: Schema generation happens on-demand
- Incremental Adoption: Enable only the formats you need via feature flags
📦 Installation
Add omni-schema to your Cargo.toml:
[]
= "0.1"
Or with specific features:
[]
= { = "0.1", = ["all-formats"] }
🚀 Quick Start
use Schema;
use HashMap;
Generate Schemas
use *;
📦 Batch Export with Registry
For exporting multiple types together:
use ;
📋 Supported Output Formats
| Feature | Format | Description |
|---|---|---|
json-schema |
JSON Schema | Draft 2020-12 compliant |
openapi |
OpenAPI 3.1 | Component schemas for REST APIs |
graphql |
GraphQL SDL | Type definitions for GraphQL |
protobuf |
Protocol Buffers | Proto3 message definitions |
typescript |
TypeScript | Type definitions (.d.ts) |
avro |
Apache Avro | Schema for Avro serialization |
🏷️ Attributes
Type-level Attributes
Field-level Attributes
Enum Representation
Supports all serde enum representations:
// Externally tagged (default)
// Output: {"Variant": {"field": "value"}}
// Internally tagged
// Output: {"type": "Variant", "field": "value"}
// Adjacently tagged
// Output: {"type": "Variant", "data": {"field": "value"}}
// Untagged
// Output: {"field": "value"}
🔧 Feature Flags
[]
= { = "0.1", = ["all-formats"] }
Output Formats
| Feature | Description |
|---|---|
json-schema |
JSON Schema generation (default) |
openapi |
OpenAPI 3.1 generation |
graphql |
GraphQL SDL generation |
protobuf |
Protocol Buffers generation |
typescript |
TypeScript type generation |
avro |
Apache Avro schema generation |
all-formats |
All format generators |
Type Support
| Feature | Description |
|---|---|
uuid-support |
UUID type support |
chrono-support |
DateTime type support |
url-support |
URL type support |
all-types |
All type support features |
Integration
| Feature | Description |
|---|---|
serde-compat |
Serde attribute compatibility |
full |
Everything enabled |
📊 Type Mappings
| Rust Type | JSON Schema | TypeScript | GraphQL | Protobuf | Avro |
|---|---|---|---|---|---|
bool |
boolean | boolean | Boolean | bool | boolean |
i8/i16/i32 |
integer | number | Int | int32 | int |
i64 |
integer | number | BigInt | int64 | long |
u8/u16/u32 |
integer | number | Int | uint32 | int |
u64 |
integer | number | BigInt | uint64 | long |
f32/f64 |
number | number | Float | float/double | float/double |
String |
string | string | String | string | string |
Option<T> |
oneOf [T, null] | T | null | T (nullable) | optional T | union [null, T] |
Vec<T> |
array | T[] | [T!]! | repeated T | array |
HashSet<T> |
array (unique) | Set<T> | [T!]! | repeated T | array |
HashMap<K,V> |
object | Record<K,V> | JSON | map<K,V> | map |
uuid::Uuid |
string (uuid) | string | ID | string | string (uuid) |
chrono::DateTime |
string (date-time) | string | DateTime | string | string |
🤝 Serde Compatibility
omni-schema uses attribute syntax that mirrors serde:
use ;
use Schema;
📖 Examples
Run the included examples:
# Basic usage example
# Serde compatibility example
📁 Crate Structure
| Crate | Description |
|---|---|
omni-schema |
Main library (re-exports) |
omni-schema-core |
Core types and format generators |
omni-schema-derive |
Proc-macro implementation |
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.