vld-ts 0.1.0

Generate TypeScript Zod schemas from vld JSON Schemas
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented2 out of 3 items with examples
  • Size
  • Source code size: 19.02 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 21s Average build duration of successful builds.
  • all releases: 21s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • s00d/vld
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • s00d

Crates.io docs.rs License Platform GitHub issues GitHub stars

vld-ts

Generate TypeScript Zod schemas from JSON Schema definitions produced by vld.

Overview

vld-ts takes standard JSON Schema objects (as serde_json::Value) and converts them into TypeScript Zod schema code strings. This enables sharing a single validation contract between your Rust backend and TypeScript frontend.

Installation

[dependencies]
vld-ts = "0.1"
serde_json = "1"

Quick start

use vld_ts::{json_schema_to_zod, generate_zod_file};

// Single schema
let schema = serde_json::json!({
    "type": "object",
    "required": ["name", "email"],
    "properties": {
        "name": {"type": "string", "minLength": 2, "maxLength": 50},
        "email": {"type": "string", "format": "email"},
        "age": {"type": "integer", "minimum": 0}
    }
});

let zod = json_schema_to_zod(&schema);
// => z.object({ name: z.string().min(2).max(50), email: z.string().email(), ... })

// Generate a complete .ts file with multiple schemas
let schemas = vec![
    ("User", schema),
    ("Email", serde_json::json!({"type": "string", "format": "email"})),
];
let ts_file = generate_zod_file(&schemas);
println!("{}", ts_file);

Generated output

// Auto-generated by vld-ts — do not edit manually
import { z } from "zod";

export const UserSchema = z.object({
  name: z.string().min(2).max(50),
  email: z.string().email(),
  age: z.number().int().min(0).optional()
});
export type User = z.infer<typeof UserSchema>;

export const EmailSchema = z.string().email();
export type Email = z.infer<typeof EmailSchema>;

Supported JSON Schema features

JSON Schema Zod output
type: "string" z.string()
type: "number" z.number()
type: "integer" z.number().int()
type: "boolean" z.boolean()
type: "null" z.null()
type: "array" z.array(...)
type: "object" z.object({...})
oneOf z.union([...])
allOf z.intersection(...)
anyOf z.union([...])
enum z.literal(...) / z.union()
format: "email" .email()
format: "uuid" .uuid()
format: "date"/"date-time" .date() / .datetime()
minLength/maxLength .min()/.max()
minimum/maximum .min()/.max()
pattern .regex()
description .describe()
nullable (oneOf + null) .nullable()

Examples

cargo run -p vld-ts --example generate_zod

License

MIT