Skip to content

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-basic

Or run all of them:

bash
zig build run-all-examples

What each example covers ​

ExampleWhat it demonstrates
Basic ValidationA format check, an integer bound and a charset rule, plus plain and coloured error output.
Model Objectsz.object bound to a declared struct, defaults, enums, arrays, naming, aliases, query parsing and JSON Schema.
Nested ObjectsComposing object schemas and reading nested error paths.
CollectionsArrays with uniqueness and records backed by std.array_hash_map.String.
FormatsThe built-in string formats and the allocation-free z.validators predicates.
UnionsUntagged unions and discriminated unions with exhaustive switch.
ModifiersDefaults, optional fields, nullable fields, refinements and typed validation.
Structured ErrorsPlain rendering, JSON rendering and the z.validate issue list.
Query Strings & FormsQuery 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 ParseResult owns 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.

Released under the MIT License.