Crate omni_schema

Crate omni_schema 

Source
Expand description

§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.

§Quick Start

Add omni-schema to your Cargo.toml:

[dependencies]
omni-schema = { version = "0.1", features = ["all-formats"] }

Then use the Schema derive macro:

use omni_schema::Schema;
use std::collections::HashMap;

#[derive(Schema)]
#[schema(description = "A user in the system")]
pub struct User {
    #[schema(description = "Unique identifier")]
    pub id: u64,

    #[schema(min_length = 1, max_length = 100)]
    pub name: String,

    #[schema(format = "email")]
    pub email: String,

    pub tags: Vec<String>,
}

#[derive(Schema)]
pub enum Status {
    Active,
    Inactive,
    Suspended { reason: String },
}

Generate schemas:

use omni_schema::prelude::*;

// Generate JSON Schema
let json_schema = User::json_schema();

// Generate TypeScript types
let typescript = User::typescript_type();

// Generate GraphQL SDL
let graphql = User::graphql_sdl();

// Generate Protocol Buffers
let proto = User::proto_definition();

§Batch Export with Registry

For exporting multiple types together:

use omni_schema::{Schema, SchemaRegistry};

let registry = SchemaRegistry::new()
    .register::<User>()
    .register::<Status>()
    .with_title("My API")
    .with_version("1.0.0");

// Export to files
registry.export_json_schema("./schemas/json/").unwrap();
registry.export_typescript("./schemas/types.ts").unwrap();
registry.export_graphql("./schemas/schema.graphql").unwrap();
registry.export_proto("./schemas/models.proto").unwrap();

§Feature Flags

  • 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
  • uuid-support - UUID type support
  • chrono-support - DateTime type support
  • url-support - URL type support
  • serde-compat - Serde attribute compatibility
  • full - Everything enabled

Re-exports§

pub use serde_json;

Modules§

formats
Format-specific generators for advanced usage.
prelude
Prelude module for convenient imports.

Structs§

EnumAttribute
EnumDefinition
EnumVariant
FieldAttribute
FieldDefinition
NewtypeDefinition
RegistryConfig
SchemaAttributes
SchemaDefinition
SchemaRegistry
StructDefinition
StructField
TypeAttribute
VariantAttribute

Enums§

EnumRepresentation
PrimitiveType
RenameRule
SchemaError
SchemaType
TypeReference
VariantData

Traits§

ExternalSchema
Trait for providing custom schema implementations for external types.
FlattenableSchema
Trait for types that can be flattened into their parent struct.
SchemaImpl
The main trait for types that can generate schema definitions.

Type Aliases§

SchemaResult

Derive Macros§

Schema