8000 GitHub - ajpinedam/tixschema · GitHub
[go: up one dir, main page]

Skip to content
ajpinedamPublic
forked from tixena/tixschema

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

TixSchema

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.

Installation

Rust Dependencies

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 of zod, dart, swift and kotlin on;
  • a type's doc comment holds a rust example block, in a build with zod on;
  • a member is written as a bare Value or Map<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.

Frontend Dependencies (Zod v4)

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

Kotlin Dependencies

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

Quick Start

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 string
  • User::json_schema() -- Returns a JSON schema (requires enable = ["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: []
});

Documentation

The guide is one file per topic, under docs/guide/, in the order a crate meets them:

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0