A Rust procedural macro library that generates TypeScript type definitions and Zod v4 validation schemas from Rust structs and enums, and whole service definitions -- messages, clients and dispatchers, in both languages, and Model Context Protocol servers -- from Rust traits, ensuring type safety and consistency between Rust backends and TypeScript frontends.
Add the following to your Cargo.toml:
[dependencies]
tixschema = "0.6"
serde = { version = "1.0", features = ["derive"] }A crate also lists serde_json = "1.0" where the generated code names it:
- a type lists
enable = ["jsonschema"]; - a type lists
enable = ["decode_with"]; - the crate invokes a service's dispatcher, client or server macro;
- a type names a
preprocess; - a type or a member writes
#[serde(default)], in a build with any ofzod,dart,swiftandkotlinon; - a type's doc comment holds a
rust exampleblock, in a build withzodon; - a member is written as a bare
ValueorMap<K, V>.
A crate that invokes a service's dispatcher, client or server macro also lists serde_path_to_error = "0.1", which the generated readers use to name the path of a value serde refuses. A crate that invokes a dispatcher or server macro lists tracing = "0.1" beside it, and one that invokes the amqp_rpc server macro lists lapin, tokio and futures as well: each macro's doc names the crates its expansion calls.
A crate that declares any pattern also names regex, because the generated check calls it directly.
TypeScript 5.7 or later reads the generated types: a Vec<u8> is Uint8Array<ArrayBuffer>, which earlier releases do not parse.
Important: This crate requires Zod 4.3.2 or later. Zod v3 is not supported. An earlier Zod 4 release refuses every payload of a variant that holds a struct under #[serde(tag = "...")]: the schema intersects the tag's strict object with the struct's, and before 4.3.2 each side refuses the other's keys.
npm install zod@^4.3.2
# or
yarn add zod@^4.3.2Kotlin 2.0.20 or later compiles the generated types: the valid class of a model type is @ConsistentCopyVisibility, which makes its copy() as private as its constructor, and Kotlin declares it from 2.0.20.
use tixschema::model_schema;
use serde::{Deserialize, Serialize};
#[model_schema()]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
pub id: String,
pub name: String,
pub email: String,
pub age: u32,
pub is_active: bool,
}This generates:
User::ts_definition()-- Returns a TypeScript type definition and Zod schema as a stringUser::json_schema()-- Returns a JSON schema (requiresenable = ["jsonschema"])
Generated TypeScript:
export type UserDraft = {
id: string;
name: string;
email: string;
age: number;
is_active: boolean;
};
export type User = Readonly<UserDraft> & $brand<"User">;
const UserDraft$RawSchema = z.strictObject({
id: z.string(),
name: z.string(),
email: z.string(),
age: z.number().int().min(0).max(4294967295),
is_active: z.boolean(),
}).meta({ id: "UserDraft" });
export const UserDraft$Schema: ZodType<UserDraft> = UserDraft$RawSchema;
export const User$Schema: ZodType & { _zod: { output: User } } = UserDraft$Schema.brand<"User">().meta({
...(UserDraft$Schema.meta() ?? {}),
id: "User",
examples: []
});The guide is one file per topic, under docs/guide/, in the order a crate meets them:
- Models: Structs, published names, unknown keys, optional fields, defaults, collections and maps, numbers, bytes, ranges, results and the unit type.
- Enums: Every tagging serde writes and how each surface describes it.
- Flattened members:
#[serde(flatten)]: the shapes that read back, and the build errors that name the fix for the others. - Untagged enums:
#[serde(untagged)]: the variant order serde reads in, and what it refuses. - Nested, recursive and aliased types: Types that hold each other, types that hold themselves, and type aliases.
- Type parameters and brands: Generic types,
default_types, and branded newtypes. - Services:
#[service_schema]: messages, errors, faults, validation, what a service generates, and theamqp_rpcwalkthrough. - The
http_resttransport: Routes, bodies, headers, streams, multipart and the OpenAPI document. - The
mcptransport: Serving a service to an MCP host and calling one. - The
ws_rpctransport: A service over a WebSocket. - The form each client sends: What each client writes on the wire for each type.
- Field validation:
#[model_schema_prop(...)]bounds,validate(), and where a constraint is checked. - Preprocess and check: Correcting a value before it is read, and judging it after.
- Upgrading: What a crate that upgrades meets, each rule with what a build or a read answers.
- Checks:
check: a rule written once in Rust and run on every surface. validate()and the read in Dart: What the Dart read refuses, andpreprocessandcheckin Dart.validate()and the read in Kotlin: What the Kotlin read refuses, andpreprocessandcheckin Kotlin.validate()and the read in Swift: What the Swift read refuses, andpreprocessandcheckin Swift.- Literal values and type names:
literalandas. - Preprocessing:
preprocess: the function a read calls first, on every surface. - Optional and nullable keys:
ts_optionalandnullable. - Compiler-validated examples: Examples the compiler checks against the type they describe.
- MongoDB
ObjectId:ObjectIdon every surface. - Chrono dates and times: Dates and times, and
as_number. - Recovering decode:
decode_with: reading stored or received values that may not match their model. - arky frontmatter: Converting to and from arky's frontmatter
Value. - MongoDB operations and typed filters: Reading and writing a model's own rows through typed filters, updates and operations.
- Feature flags and generation: Every feature, generating the TypeScript files, and wiring the frontend.
- Serde attributes: Which serde attributes each surface reads.
- Troubleshooting: Notes, the errors the macros answer and what each asks, and Zod v4.