Skip to content

API Reference ​

Every public name is available from the package root:

zig
const z = @import("zigantic");

All configuration is passed as a trailing .{} value. Constructors are compile-time functions returning schema types; services are runtime functions taking an allocator before the value and options last.

Services ​

FunctionReturnsNotes
z.parse(S, allocator, input, .{})ParseResult(S)JSON text to a validated value.
z.stringify(S, allocator, value, .{})owned []u8validated value to JSON text.
z.parseQuery(S, allocator, input, .{})ParseResult(S)query string for object schemas, including a[b] paths.
z.parseForm(S, allocator, input, .{})ParseResult(S)application/x-www-form-urlencoded body.
z.stringifyQuery(S, allocator, value, .{})owned []u8object schema to a nested query string.
z.jsonSchema(S, allocator, .{ .target = .draft_2020_12 })owned []u8JSON Schema document with a root $ref and definitions.
z.validate(S, allocator, value, .{})IssueListchecks an already-typed value.
z.checkFirst(S, value, buffer, .{})?Issuefirst-error validation using caller scratch memory.

Most services take an allocator; checkFirst is the exception and takes a caller-provided scratch buffer instead.

parse, parseQuery and parseForm can return error.InvalidJson (malformed JSON or percent-encoding) and error.OutOfMemory. Schema violations are issues inside the result, not errors. stringify can return error.Unsupported for schemas without an encoder, error.TransformFailed when a transform or codec callback fails, and allocator or writer errors. jsonSchema can return error.Unsupported for custom schemas, which have no external representation. checkFirst returns error.OutOfMemory when the scratch buffer is too small.

ParseResult(S) ​

zig
pub fn Result(comptime S: type) type {
    return struct {
        value: ?S.Value,
        issues: IssueList,

        pub fn deinit(self: *Self) void;
        pub fn ok(self: *const Self) bool;
        pub fn errors(self: *Self) *IssueList;
    };
}
  • value is null exactly when validation failed.
  • errors() returns a borrowed list that is valid until deinit.
  • deinit frees the value arena and the issue list; the result must not be used afterwards.

Schema protocol ​

A schema is a type with these declarations:

MemberTypeMeaning
Valuetypevalidated Zig type
kindz.Kindschema discriminant
schema_name[]const u8name used in missing-field expectations
is_optionalboolmay be absent from an object
has_defaultboolsupplies a default when absent
decode(state, node, path)DecodeError!ValueJSON to value
decodeString(state, raw, path)DecodeError!Valuetext to value; only schemas with a text form provide it. Query decoding itself builds a synthetic JSON value and calls decode.
check(state, value, path)DecodeError!voidvalidate a typed value
encode(state, value)EncodeError!voidvalue to JSON
writeJsonSchema(state)SchemaError!voiddescribe the schema

DecodeError is error{ Reported, OutOfMemory }; Reported means at least one issue was stored. EncodeError includes writer, Unsupported and TransformFailed. SchemaError includes allocator and writer errors plus Unsupported.

Constructors validate their inputs at compile time. Passing a non-integer to z.integer, a schema whose Value does not match a model field, or a variant map that omits a union field is a build error.

Constructors ​

Strings ​

zig
z.string(.{})
z.email(.{})
z.url(.{})
z.uri(.{})
z.httpsUrl(.{})
z.uuid(.{})
z.guid(.{})
z.ulid(.{})
z.slug(.{})
z.semver(.{})
z.phoneNumber(.{})
z.creditCard(.{})
z.jwt(.{})
z.base64(.{})
z.base64Url(.{})
z.hex(.{})
z.hexColor(.{})
z.macAddress(.{})
z.ipv4(.{})
z.ipv6(.{})
z.isoDate(.{})
z.isoTime(.{})
z.isoDateTime(.{})
z.isoDuration(.{})
z.date(.{})
z.time(.{})
z.dateTime(.{})

All of them accept z.StringConfig:

FieldTypeDefaultMeaning
minusize0minimum byte length
maxusizemaxInt(usize)maximum byte length
len?usizenullexact byte length; takes precedence in messages
prefix?[]const u8nullrequired prefix
suffix?[]const u8nullrequired suffix
contains?[]const u8nullrequired substring
trimboolfalsetrim ASCII whitespace before checks
casez.StringCase.any.any, .lower, .upper
charsetz.StringCharset.any.any, .ascii, .printable, .alphabetic, .alphanumeric, .numeric, .hex
formatz.StringFormat.noneset automatically by format constructors
description[]const u8""JSON Schema description

Length checks run on the trimmed text when .trim is set. .format is set by the format constructors and is not usually assigned directly.

Numbers ​

zig
z.integer(T, .{})
z.float(T, .{})
z.positiveInt(T, .{})
z.negativeInt(T, .{})
z.nonNegativeInt(T, .{})
z.nonPositiveInt(T, .{})
z.safeInt(T, .{})
z.unsignedInt(T, .{})
z.percentage(T, .{})
z.probability(T, .{})
z.bytes(.{})

z.IntConfig:

FieldTypeDefaultMeaning
min?comptime_intnullinclusive minimum
max?comptime_intnullinclusive maximum
exclusive_min?comptime_intnullexclusive minimum
exclusive_max?comptime_intnullexclusive maximum
evenboolfalserequire an even value
multiple_of?comptime_intnullrequire a multiple
description[]const u8""JSON Schema description

z.FloatConfig:

FieldTypeDefaultMeaning
min?comptime_floatnullinclusive minimum
max?comptime_floatnullinclusive maximum
exclusive_min?comptime_floatnullexclusive minimum
exclusive_max?comptime_floatnullexclusive maximum
finitebooltruereject NaN and infinities
description[]const u8""JSON Schema description

The helpers are constraint presets that still accept the same config:

  • positiveInt sets min to at least 1.
  • negativeInt sets max to at most -1.
  • nonNegativeInt sets min to at least 0.
  • nonPositiveInt sets max to at most 0.
  • safeInt bounds the value to the IEEE-754 safe integer range.
  • unsignedInt requires an unsigned type at compile time.
  • percentage bounds a float to 0..100.
  • probability bounds a float to 0..1.

z.bytes(.{}) returns []const u8 decoded from standard base64, unpadded base64url, hexadecimal text or a JSON byte array. Its config is z.BytesConfig { encoding: z.ByteEncoding, min, max, len, description }; lengths refer to decoded bytes. Decoded storage belongs to the result arena.

Primitives ​

zig
z.boolean(.{})       // z.BoolConfig   { description }
z.enumType(E, .{})   // z.EnumConfig   { description }
z.nullValue(.{})     // z.NullConfig   { description }
z.literal(value)     // string, integer or boolean literal

z.literal takes the literal itself as its only argument; there is no separate options value. Its Value is []const u8 for string literals, i64 for comptime integers and bool for booleans.

Objects ​

zig
z.object(T, .{
    .field = Schema,
}, .{})

z.objectOf(.{
    .field = Schema,
}, .{})

z.merge(FirstSchema, SecondSchema, .{})
z.extend(FirstSchema, SecondSchema, .{})
z.pick(ObjectSchema, &.{"field"}, .{})
z.omit(ObjectSchema, &.{"field"}, .{})

z.object binds to a declared Zig type. z.objectOf infers the struct with @Struct; its Value is the synthesized type. Both accept z.ObjectConfig. merge and extend combine the .fields of two inferred objects and reject duplicate field names at compile time. pick selects existing fields; omit removes named fields and ignores unknown omissions.

z.ObjectConfig:

FieldTypeDefaultMeaning
namingz.Naming.verbatimkey policy for input, output and schema
unknown_fieldsz.UnknownFields.reject.reject or .strip
aliases[]const z.Alias&.{}extra accepted input keys
partialboolfalseallow any field to be absent
description[]const u8""JSON Schema description

Rules:

  • Every schema field must exist on T.
  • Every field of T without a schema must have a default value, be optional, or be allowed by .partial.
  • A missing schema field is filled from, in order: z.optional (null), schema default, model default, .partial, otherwise E007.

Collections ​

zig
z.array(S, .{})
z.fixedArray(S, N, .{})
z.set(S, .{})
z.record(S, .{})

z.ArrayConfig:

FieldTypeDefault
minusize0
maxusizemaxInt(usize)
len?usizenull
uniqueboolfalse
description[]const u8""

z.RecordConfig:

FieldTypeDefault
minusize0
maxusizemaxInt(usize)
key_minusize1
key_maxusize256
description[]const u8""

z.array and z.set produce []const S.Value; z.fixedArray produces [N]S.Value; z.record produces std.array_hash_map.String(S.Value). z.set uses z.ArrayConfig and always sets unique = true. z.fixedArray uses z.FixedConfig, which contains only description; its length is the N parameter, not a config option.

Tuples and intersections ​

zig
z.tuple(.{
    .first = Schema,
    .second = OtherSchema,
}, .{})

z.intersection(.{
    .first = Schema,
    .second = OtherSchema,
}, .{})

z.tuple synthesizes @Tuple from the field schemas. z.intersection requires every member to produce the same Value; decoding uses the first member and checks the result against the others. Both configs contain only description.

Unions ​

zig
z.unionOf(U, .{ .variant = Schema }, .{})
z.discriminatedUnion(U, "type", .{ .variant = Schema }, .{})

z.UnionConfig and z.DiscriminatedConfig both contain only description. Variant names must match the union fields and payload types. Every field of the union must have a variant schema; a missing variant is a build error.

Modifiers ​

ConstructorValueNotes
z.optional(S)?S.Valueabsent key; rejects explicit null
z.nullable(S)?S.Valueaccepts explicit null
z.nullish(S)?S.Valueaccepts an absent key or explicit null
z.withDefault(S, value)S.Valuecompile-time default
z.withDefaultFactory(S, factory)S.Valuefn () S.Value
z.fallback(S, value)S.Valuevalue used when decoding fails
z.refine(S, .{ .check, .message })S.Valuefn (S.Value) bool
z.transform(S, .{ .decode, .encode, .check })decode payloadreversible typed conversion
z.codec(Input, Output, .{ .decode, .encode })Output.Valuevalidated input/output conversion
z.meta(S, .{...})S.ValueJSON Schema metadata only
z.lazy(get)*const Inner.Valuefn () type
z.custom(T, .{ .check, .validate, .message })Ttyped checks only; a failed check is E012

z.Metadata fields: id, title, description (?[]const u8), examples ([]const []const u8), deprecated, read_only, write_only (bool).

z.RefineConfig(T):

FieldTypeDefault
check*const fn (T) boolrequired
message[]const u8"value failed a refinement check"

z.CustomConfig(T):

FieldTypeDefault
check?*const fn (T) boolnull
validate?*const fn (*z.Context, T) z.DecodeError!voidnull
message[]const u8"value failed a custom check"

At least one of check or validate must be supplied. When both are set, validate runs and check is ignored. validate may record several issues through the context and may return error.OutOfMemory.

z.TransformConfig(In, Out):

FieldTypeDefault
decode*const fn (Allocator, In.Value) anyerror!Outrequired
encode?*const fn (Allocator, Out) anyerror!In.Valuenull
check?*const fn (Out) boolnull
message[]const u8"value failed a transformation check"

Supplying encode makes z.stringify possible. Without encode, encode returns error.Unsupported. Without either encode or check, check also returns error.Unsupported.

z.CodecConfig(In, Out) requires both directions:

FieldTypeDefault
decode*const fn (Allocator, In.Value) anyerror!Out.Valuerequired
encode*const fn (Allocator, Out.Value) anyerror!In.Valuerequired
message[]const u8"value could not be encoded or decoded"

A codec validates Input.Value before decoding, validates Output.Value after decoding, and serializes through the input schema on encode. The input schema also supplies the generated JSON Schema.

Date and time ​

z.Date, z.Time and z.DateTime provide allocation-free civil arithmetic:

TypeMembers
Dateinit, parse, format, daysSinceEpoch, fromDaysSinceEpoch, weekday, dayOfYear, addDays, addMonths, addYears, daysUntil, compare, eql
Timeinit, parse, format, seconds, addSeconds, compare
DateTimeinit, parse, format, timestamp, fromTimestamp, addSeconds, addDays, dateDifference, compare

DateTime keeps a fixed offset_minutes; arithmetic is exact for that offset. Time-zone database transitions are not modelled. The z.datetime module also exports isLeapYear and daysInMonth.

z.date(.{}), z.time(.{}) and z.dateTime(.{}) are string schemas that decode into z.Date, z.Time and z.DateTime. Their config is z.TemporalConfig { description }; they never allocate.

Options ​

z.Options is shared by parse, parseQuery, parseForm and validate:

FieldTypeDefaultMeaning
unknown_fields?z.UnknownFieldsnullper-call override of the object policy
coerceboolfalseallow scalars as text (and text as numbers or booleans) in JSON; always on for query transports
duplicatesz.Duplicates.reject.reject, .first or .last for repeated JSON keys
messagesz.Messagesall nullper-code message overrides
max_depthusize128nesting limit for objects, arrays and lazy nodes
hooksz.Hooksall nulllifecycle callbacks

z.JsonSchemaOptions (used by jsonSchema):

FieldTypeDefault
targetz.JsonSchemaTarget.draft_2020_12

z.JsonSchemaTarget has .draft_2020_12, .draft_2019_09, .draft_07, .draft_06 and .draft_05. The 2019-09 and 2020-12 targets use $defs and #/$defs/...; older drafts use definitions and #/definitions/.... Draft 2020-12 uses prefixItems; earlier drafts use the tuple form of items plus additionalItems: false. Draft 5 uses boolean exclusive bounds and a one-element enum instead of const.

z.EncodeOptions (used by stringify):

FieldTypeDefault
emit_null_fieldsboolfalse

z.QueryStringifyOptions (used by stringifyQuery):

FieldTypeDefault
emit_nullsboolfalse

z.RenderOptions (used by IssueList.render / write):

FieldTypeDefaultMeaning
capabilityz.Capability.nonetint colour capability
show_expectedbooltrueprint the expected line
show_receivedbooltrueprint the received line
indentu82spaces before details

z.IssueList.init takes its own Options with capacity (usize, default 8), the initial issue capacity.

Hooks ​

zig
pub const Hooks = struct {
    on_start: ?*const fn (schema_name: []const u8) void = null,
    on_field: ?*const fn (path: []const u8, accepted: bool) void = null,
    on_issue: ?*const fn (issue: Issue) void = null,
    on_finish: ?*const fn (ok: bool, total_issues: usize) void = null,
};

Hooks are plain function pointers supplied per call. They run on the calling thread and must not retain borrowed slices after returning.

Issues ​

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

z.IssueList methods:

MethodResult
init(allocator, .{})owning empty list
deinit()frees the list and its arena
clear()removes issues, keeps capacity
count() / isEmpty()size queries
at(index)Issue by index
first() / last()?Issue
mark() / rollback(point)transactional suffix removal
add(issue) / addCode(...) / addError(...)append
write(writer, .{}) / render(allocator, .{})annotated text
writeJson(writer) / toJson(allocator)JSON array

at, first and last return borrowed Issue values whose strings are owned by the list.

Error codes ​

CodeNameCategoryDefault message
E001type_mismatchtype_mismatchinvalid type
E002too_smalllengthvalue is too small
E003too_largelengthvalue is too large
E004length_mismatchlengthinvalid length
E005invalid_valuestructureinvalid value
E006invalid_formatformatinvalid format
E007missing_fieldstructurerequired field is missing
E008unknown_fieldstructureunknown field is not allowed
E009duplicate_fieldstructureduplicate field
E010not_in_enumstructurevalue is not an allowed enum member
E011invalid_literalstructurevalue does not match the required literal
E012refinement_failedrefinementvalue failed a refinement check
E013transform_failedcoercionvalue could not be transformed
E014coercion_failedcoercionvalue could not be coerced
E015union_mismatchstructurevalue does not match any union variant
E016out_of_rangelengthvalue is outside the allowed range
E017not_finiterangevalue must be finite
E018not_integertype_mismatchvalue must be an integer
E019missing_discriminatorstructurediscriminator field is missing
E020invalid_discriminatorstructurediscriminator selects an unknown variant
E021unsupportedunsupportedoperation is not supported by this schema

z.Code exposes label(), category() and message(). z.Messages has one optional field per code, named after the code, for per-call overrides.

Naming ​

z.Naming (alias of Policy) values: .verbatim, .camel_case, .snake_case, .kebab_case, .pascal_case, .lower_case, .upper_case.

z.Alias:

zig
pub const Alias = struct {
    field: []const u8,
    keys: []const []const u8,
};

Aliases add accepted input keys. Encoding always uses the canonical key.

Validators ​

z.validators exposes pure, allocation-free predicates taking []const u8:

zig
isEmail, isUrl, isUri, isHttpsUrl, isUuid, isUlid, isSlug, isSemver,
isPhoneNumber, luhn, isCreditCard, isBase64, isBase64Url, isHex,
isHexColor, isMacAddress, isJwt, isIpv4, isIpv6, isIsoDate,
isIsoTime, isIsoDateTime, isIsoDuration

luhn takes a string of digits and verifies the checksum; isCreditCard normalises separators and then calls it.

Other exports ​

ExportMeaning
z.versionpackage version string
z.ValidationErrorerror{...} enum of validation failures
z.Categoryissue category enum
z.Kindschema discriminant enum
z.Statedecode state passed to schemas
z.UnknownFields.reject / .strip
z.Duplicates.reject / .first / .last
z.ObjectConfig, z.ArrayConfig, z.RecordConfig, z.FixedConfigobject and collection configuration
z.TupleConfig, z.IntersectionConfigtuple and intersection configuration
z.UnionConfig, z.DiscriminatedConfigunion configuration
z.RefineConfig, z.TransformConfig, z.CodecConfig, z.CustomConfigmodifier configuration
z.Metadataz.meta metadata fields
z.Context, z.State, z.Hooks, z.Optionsvalidation plumbing
z.Capabilitytint colour capability
z.Date, z.Time, z.DateTime, z.Weekdaycivil date/time arithmetic
z.datetimeisLeapYear, daysInMonth and the date/time types

Native documentation ​

bash
zig build docs

Writes the Zig-generated reference for every public declaration to zig-out/docs/api.

Released under the MIT License.