Skip to content

Validation Types ​

This page lists every built-in schema, the options it accepts and the failures it can produce.

Strings ​

z.string(.{}) is a []const u8. Every format constructor accepts the same z.StringConfig, so a format composes with length and character rules.

OptionTypeDefaultFailure
minusize0E002
maxusizemaxInt(usize)E003
len?usizenullE004
prefix?[]const u8nullE005
suffix?[]const u8nullE005
contains?[]const u8nullE005
trimboolfalsenone; normalises first
casez.StringCase.anyE005
charsetz.StringCharset.anyE005
description[]const u8""none; JSON Schema only

z.StringCase: .any, .lower, .upper.

z.StringCharset: .any, .ascii, .printable, .alphabetic, .alphanumeric, .numeric, .hex.

zig
const Handle = z.string(.{
    .min = 3,
    .max = 32,
    .prefix = "@",
    .charset = .alphanumeric,
    .trim = true,
});

trim removes ASCII whitespace before the length, prefix, suffix, contains, case, charset and format checks, and the returned slice excludes it.

Built-in formats ​

ConstructorAccepted formExpected text
emailRFC 5322 subset with dotted domain and alphabetic TLDemail address
urlabsolute URL with scheme and hostabsolute URL
uriabsolute URI with scheme; host optionalabsolute URI
httpsUrlabsolute URL with the https schemeHTTPS URL
uuid / guid8-4-4-4-12 hexadecimalUUID
ulid26 Crockford base32 characters, first 0-7ULID
sluglowercase words separated by single hyphenskebab-case slug
semverSemantic Version 2.0.0semantic version
phoneNumberoptional +, 7-15 digits, separators allowedphone number
creditCard12-19 digits with Luhn checksumpayment card number
jwtthree non-empty base64url segmentsJSON Web Token
base64 / base64Urlstrict alphabet and paddingbase64 text / base64url text
hexeven-length hexadecimaleven-length hexadecimal text
hexColor#RGB, #RRGGBB, #RRGGBBAAhex colour
macAddresssix octet pairs with a consistent separatorMAC address
ipv4 / ipv6RFC 4291 text form, including :: and IPv4 tailsIPv4 address / IPv6 address
isoDateYYYY-MM-DD with calendar checksISO 8601 date
isoTime`HH:MM[:SS[.fff]][Z±HH:MM]`
isoDateTimedate, T or space, timeISO 8601 date-time
isoDurationP... with date/time componentsISO 8601 duration

Format failures are E006. They are checked after length, prefix/suffix, contains, case and charset, so the most specific rule wins in order.

creditCard accepts spaces and hyphens as separators. ipv6 accepts IPv4-mapped and IPv4-compatible tails. isoDate rejects 2025-02-29 and accepts 2024-02-29.

Date and time values ​

The ISO string schemas validate wire text. When an application needs calendar arithmetic, parse the validated text into z.Date, z.Time or z.DateTime:

zig
const date = try z.Date.parse("2024-02-29");
const next = date.addYears(1);      // 2025-02-28
const weekday = date.weekday();     // .thursday

const value = try z.DateTime.parse("2024-02-29T23:30:00+02:00");
const later = value.addSeconds(3600);

These types are allocation-free, reject invalid calendar dates, clamp month arithmetic to the target month's last day, and preserve a fixed UTC offset. z.datetime.isLeapYear and z.datetime.daysInMonth expose the same rules. See Date & Time for formatting, timestamps and comparison.

Predicates ​

The same checks are exported without a schema:

zig
z.validators.isEmail("a@example.com");
z.validators.isIpv6("2001:db8::1");
z.validators.isCreditCard("4242 4242 4242 4242");
z.validators.luhn("4242424242424242");
z.validators.isIsoDuration("PT30M");

All predicates take []const u8, allocate nothing and return bool. They are safe in hot paths and in refine callbacks.

Integers ​

z.integer(T, .{}) produces any signed or unsigned integer type T.

OptionTypeDefaultFailure
min?comptime_intnullE002
max?comptime_intnullE003
exclusive_min?comptime_intnullE002
exclusive_max?comptime_intnullE003
evenboolfalseE005
multiple_of?comptime_intnullE005
description[]const u8""none

Bounds must fit T; a bound outside the type's range is a compile error. A value outside T is E016, a fractional JSON number is E018, and a non-numeric JSON value is E001. JSON numbers that arrive as floats are converted with f64 precision: whole numbers that round past the type's range report E016 instead of crashing, so exact large integers should use an integer JSON form or a number string rather than a float.

zig
z.integer(i32, .{ .min = -10, .max = 10, .multiple_of = 2 });
z.integer(u8, .{ .even = true });

Floats ​

z.float(T, .{}) produces f16, f32, f64, f80 or f128.

OptionTypeDefaultFailure
min?comptime_floatnullE002
max?comptime_floatnullE003
exclusive_min?comptime_floatnullE002
exclusive_max?comptime_floatnullE003
finitebooltrueE017
description[]const u8""none

NaN and infinities are rejected unless .finite = false. Encoding a non-finite value returns error.Unsupported.

Numeric presets ​

zig
z.positiveInt(u16, .{});
z.negativeInt(i32, .{});
z.nonNegativeInt(i64, .{});
z.nonPositiveInt(i64, .{});
z.safeInt(i64, .{});
z.unsignedInt(u32, .{});
z.percentage(f32, .{});
z.probability(f64, .{});

Each preset accepts the normal integer or float config, so metadata and extra constraints compose:

zig
const Ratio = z.probability(f64, .{ .description = "success ratio" });

positiveInt keeps min at least 1, negativeInt keeps max at most -1, nonNegativeInt keeps it at least 0, nonPositiveInt keeps it at most 0, safeInt bounds the value to the IEEE-754 safe integer range, and unsignedInt rejects signed types at compile time. percentage bounds to 0..100 and probability bounds to 0..1.

Binary data ​

z.bytes(.{}) decodes textual binary data or a JSON byte array into []const u8. Decoded bytes are owned by the parse result arena; encoding uses the stringify scratch arena.

OptionTypeDefault
encodingz.ByteEncoding.base64
minusize0
maxusizemaxInt(usize)
len?usizenull
description[]const u8""

z.ByteEncoding is .base64, .base64url (unpadded), .hex or .array. Length constraints apply to the decoded bytes, not the encoded text.

zig
const Digest = z.bytes(.{ .encoding = .hex, .len = 32 });

Temporal values ​

z.date(.{}), z.time(.{}) and z.dateTime(.{}) decode ISO text directly into z.Date, z.Time and z.DateTime. They reject invalid calendar and clock values at decode time, are allocation-free, and encode back to ISO text.

zig
const Day = z.date(.{});
const Moment = z.dateTime(.{});

Booleans, enums, literals and null ​

zig
z.boolean(.{ .description = "feature flag" });
z.enumType(Role, .{});          // Role is a Zig enum
z.literal("created");           // exact string
z.literal(200);                 // exact integer
z.literal(true);                // exact boolean
z.nullValue(.{});               // void, matches JSON null
  • z.boolean accepts JSON booleans; with .coerce = true it also accepts the strings "true", "false", "1", "0" and the integers 1 and 0.
  • z.enumType matches enum member names exactly and reports E010.
  • z.literal reports E011 with the literal as the expected value.
  • z.nullValue produces void and reports E001 for any non-null value.

Released under the MIT License.