Examples ​
Every example on this page is a real program in the repository under examples/. Each one compiles against the published package, runs without arguments and prints the output shown on its page.
Run a single example with:
bash
zig build run-basicOr run all of them:
bash
zig build run-all-examplesWhat each example covers ​
| Example | What it demonstrates |
|---|---|
| Basic Validation | A format check, an integer bound and a charset rule, plus plain and coloured error output. |
| Model Objects | z.object bound to a declared struct, defaults, enums, arrays, naming, aliases, query parsing and JSON Schema. |
| Nested Objects | Composing object schemas and reading nested error paths. |
| Collections | Arrays with uniqueness and records backed by std.array_hash_map.String. |
| Formats | The built-in string formats and the allocation-free z.validators predicates. |
| Unions | Untagged unions and discriminated unions with exhaustive switch. |
| Modifiers | Defaults, optional fields, nullable fields, refinements and typed validation. |
| Structured Errors | Plain rendering, JSON rendering and the z.validate issue list. |
| Query Strings & Forms | Query and form decoding with coercion, repeated keys and round-trip serialization. |
Conventions used by every example ​
- The allocator is always explicit, and is the second argument to a service (except
z.checkFirst, which takes the value second and a scratch buffer third). - Every service takes a trailing options value, even when it is
.{}. - A
ParseResultowns its decoded strings;defer result.deinit()releases them. - Schemas are values of type
type, so they can be declared as top-level constants and reused for JSON, query strings, validation and JSON Schema.
Related guides ​
- Inferred Objects, Tuples & Intersections covers
z.objectOf,z.tupleandz.intersection. - JSON, Query & Forms documents nested
a[b]query paths and duplicate-key handling. - Date & Time covers calendar arithmetic after string validation.
- JSON Schema explains
$defs,$refand what can be represented.
