[](https://crates.io/crates/vld-ts)
[](https://docs.rs/vld-ts)
[](https://github.com/s00d/vld/blob/main/LICENSE)
[](https://github.com/s00d/vld)
[](https://github.com/s00d/vld/issues)
[](https://github.com/s00d/vld/stargazers)
# vld-ts
Generate TypeScript [Zod](https://zod.dev/) schemas from JSON Schema definitions produced by [vld](https://crates.io/crates/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
```toml
[dependencies]
vld-ts = "0.1"
serde_json = "1"
```
## Quick start
```rust
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
```typescript
// 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
| `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
```bash
cargo run -p vld-ts --example generate_zod
```
## License
MIT