Skip to content

Error Handling ​

A schema violation is an issue, not a Zig error. parse returns a result that holds every issue found, so a client can report all bad fields at once.

zig
pub const Issue = struct {
    code: z.Code,
    path: []const u8,
    message: []const u8,
    expected: ?[]const u8,
    received: ?[]const u8,
};

code is stable and machine-readable, path locates the value in the input, message is human-readable, and expected/received add context when it is useful.

Codes ​

CodeNameCategory
E001type_mismatchtype_mismatch
E002too_smalllength
E003too_largelength
E004length_mismatchlength
E005invalid_valuestructure
E006invalid_formatformat
E007missing_fieldstructure
E008unknown_fieldstructure
E009duplicate_fieldstructure
E010not_in_enumstructure
E011invalid_literalstructure
E012refinement_failedrefinement
E013transform_failedcoercion
E014coercion_failedcoercion
E015union_mismatchstructure
E016out_of_rangelength
E017not_finiterange
E018not_integertype_mismatch
E019missing_discriminatorstructure
E020invalid_discriminatorstructure
E021unsupportedunsupported

z.Code exposes label(), category() and message(). Default messages and the full table are also in the API reference.

Allocation-free first errors ​

z.checkFirst(S, value, buffer, .{}) checks an already-typed value and returns ?Issue using caller-provided scratch memory. It allocates nothing from the heap when the buffer is large enough; check machinery (paths, quoted values) also writes into the buffer, so even a valid value needs a realistically sized buffer — a too-small buffer returns error.OutOfMemory. The returned issue points into buffer, so it remains valid only while that buffer lives. Use z.validate when you need owning, multiple or rendered issues.

zig
var scratch: [4096]u8 = undefined;
if (try z.checkFirst(UserSchema, user, &scratch, .{})) |issue| {
    std.debug.print("{s} at {s}\n", .{ issue.code.label(), issue.path });
}

Rendering ​

zig
const text = try result.errors().render(allocator, .{ .capability = .ansi16 });
defer allocator.free(text);
text
error[E002]: value is too small
  $.age
    expected: 18..120
    received: 12

RenderOptions:

FieldDefaultMeaning
capability.none.none, .ansi16, .ansi256, .trueColor
show_expectedtrueprint the expected line
show_receivedtrueprint the received line
indent2spaces before details

Colours come from tint.zig and are selected per call. .none writes no escape sequences, which keeps logs and redirected output clean. The library never detects a terminal or reads the environment.

toJson returns a compact array with the same fields:

json
[{"code":"E002","path":"$.age","message":"value is too small","expected":"18..120","received":"12"}]

Use write/writeJson to stream into an existing writer without an intermediate allocation.

Message overrides ​

Override text per call; nothing is global:

zig
var result = try z.parse(Age, allocator, "12", .{
    .messages = .{ .too_small = "must be old enough" },
});

z.Messages has one optional field per code, named after the code. A null field keeps the default message. The code and path are unchanged, so clients can translate the category while the server customises wording.

Paths ​

  • $ is the document root.
  • .field selects an object member.
  • [0] selects an array element.
  • Paths use the output key, so a naming policy is applied.
  • Duplicate-key failures point at the repeated key, for example $.address.city.

Lifecycle hooks ​

zig
var result = try z.parse(UserSchema, allocator, input, .{
    .hooks = .{
        .on_start = logStart,
        .on_field = logField,
        .on_issue = logIssue,
        .on_finish = logFinish,
    },
});
HookCalled
on_start(schema_name)before decoding starts
on_field(path, accepted)for each object field
on_issue(issue)whenever an issue is recorded
on_finish(ok, total_issues)after decoding, including failures

Hooks are plain function pointers supplied per call, so there is no global logger and no hidden state. They run on the calling thread.

Error sets ​

decode returns error.Reported after recording an issue, or error.OutOfMemory. parse converts JSON syntax failures and duplicate keys under .reject into a result; malformed JSON surfaces as error.InvalidJson. z.validate returns the issue list instead of an error, so a typed check has no error branch beyond allocation failure.

stringify can return error.Unsupported when a schema has no encoder, such as a transform without .encode or a custom value. A transform whose .encode callback fails returns error.TransformFailed. float returns error.Unsupported for non-finite numbers.

Released under the MIT License.