vld-ts 0.1.0

Generate TypeScript Zod schemas from vld JSON Schemas
Documentation
[![Crates.io](https://img.shields.io/crates/v/vld-ts?style=for-the-badge)](https://crates.io/crates/vld-ts)
[![docs.rs](https://img.shields.io/docsrs/vld-ts?style=for-the-badge)](https://docs.rs/vld-ts)
[![License](https://img.shields.io/badge/license-MIT-green?style=for-the-badge)](https://github.com/s00d/vld/blob/main/LICENSE)
[![Platform](https://img.shields.io/badge/platform-macOS%20%7C%20Windows%20%7C%20Linux-blue?style=for-the-badge)](https://github.com/s00d/vld)
[![GitHub issues](https://img.shields.io/badge/github-issues-orange?style=for-the-badge)](https://github.com/s00d/vld/issues)
[![GitHub stars](https://img.shields.io/badge/github-stars-yellow?style=for-the-badge)](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

| 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

```bash
cargo run -p vld-ts --example generate_zod
```

## License

MIT