JSON Format
zod-rs uses serde’s default externally-tagged format for enums. Here’s how each variant type maps to JSON.
Format table
Section titled “Format table”| Variant Type | Rust | JSON |
|---|---|---|
| Unit | Status::Active | {"Active": null} |
| Tuple (single) | Message::Text("hi") | {"Text": "hi"} |
| Tuple (multiple) | Message::Coords(1, 2) | {"Coords": [1, 2]} |
| Struct | Event::Click { x: 1, y: 2 } | {"Click": {"x": 1, "y": 2}} |
Unit variants
Section titled “Unit variants”Unit variants serialize as {"VariantName": null}:
#[derive(Serialize, Deserialize, ZodSchema)]enum Status { Active, Inactive }
// JSON: {"Active": null}Tuple variants (single)
Section titled “Tuple variants (single)”Single-value tuple variants unwrap the value:
#[derive(Serialize, Deserialize, ZodSchema)]enum Msg { Text(String) }
// JSON: {"Text": "hello"}Tuple variants (multiple)
Section titled “Tuple variants (multiple)”Multi-value tuple variants use an array:
#[derive(Serialize, Deserialize, ZodSchema)]enum Msg { Coords(i32, i32) }
// JSON: {"Coords": [10, 20]}Struct variants
Section titled “Struct variants”Struct variants use a nested object:
#[derive(Serialize, Deserialize, ZodSchema)]enum Event { Click { x: i32, y: i32 } }
// JSON: {"Click": {"x": 1, "y": 2}}Validation
Section titled “Validation”Each variant is validated as a union. The value must be a single-key object where the key matches a variant name, and the value matches that variant’s expected format.