Skip to content

Types API ​

Complete API reference for all zigantic types.

Custom Messages ​

Most parameterized types accept an f suffix variant with a comptime messages parameter to override error messages:

zig
const Name = Stringf(1, 50, .{ .too_short = "name is required" });
const Age = Intf(i32, 18, 120, .{ .too_small = "must be 18 or older" });
const Pwd = StrongPasswordf(8, 100, .{ .weak_password = "needs upper, lower, digit" });

// Get custom message for an error
const msg = Name.messageFor(err); // ?[]const u8

The base types (String, Int, etc.) use built-in default messages. Use the f variants when you need custom messages.

Available on: String, NonEmptyString, Trimmed, Lowercase, Uppercase, Alphanumeric, AsciiString, Secret, StrongPassword, Int, UInt, PositiveInt, NonNegativeInt, NegativeInt, EvenInt, OddInt, MultipleOf, Float, Percentage, Probability, PositiveFloat, NegativeFloat, FiniteFloat, List, NonEmptyList, FixedList, HexString, HexColor, MacAddress, IsoDateTime, IsoDate, CountryCode, CurrencyCode, Latitude, Longitude, Port.

String Types ​

TypeDescription
String(min, max)String with length constraints
NonEmptyString(max)Non-empty string
Trimmed(min, max)Auto-trimmed string
Lowercase(max)Lowercase only
Uppercase(max)Uppercase only
Alphanumeric(min, max)Letters and digits
AsciiString(min, max)ASCII only (0-127)
Secret(min, max)Password with strength
StrongPassword(min, max)Requires upper+lower+digit+special

String Methods ​

zig
str.get()           // Get value
str.len()           // Length
str.isEmpty()       // Check empty
str.startsWith(p)   // Prefix check
str.endsWith(s)     // Suffix check
str.contains(n)     // Contains check
str.charAt(i)       // Char at index
str.slice(s, e)     // Substring

Secret Methods ​

zig
pwd.masked()       // "********"
pwd.strength()     // 0-6 score
pwd.hasUppercase() // bool
pwd.hasLowercase() // bool
pwd.hasDigit()     // bool
pwd.hasSpecial()   // bool

Number Types ​

TypeDescription
Int(T, min, max)Signed integer with range
UInt(T, min, max)Unsigned integer with range
PositiveInt(T)> 0
NonNegativeInt(T)>= 0
NegativeInt(T)< 0
EvenInt(T, min, max)Even only
OddInt(T, min, max)Odd only
MultipleOf(T, divisor)Multiple of N
Float(T, min, max)Float with range
Percentage(T)0-100
Probability(T)0-1
PositiveFloat(T)> 0
NegativeFloat(T)< 0
FiniteFloat(T)No NaN/Inf

Number Methods ​

zig
n.get()        // Get value
n.isPositive() // bool
n.isNegative() // bool
n.isZero()     // bool
n.isEven()     // bool
n.isOdd()      // bool
n.abs()        // Absolute value
n.clamp(lo,hi) // Clamp to range

f.floor()      // Floor
f.ceil()       // Ceiling
f.round()      // Round
f.trunc()      // Truncate

Format Types ​

TypeDescription
EmailEmail address
UrlHTTP/HTTPS URL
HttpsUrlHTTPS only
UuidUUID format
Ipv4IPv4 address
Ipv6IPv6 address
SlugURL slug
SemverSemantic version
PhoneNumberPhone number
CreditCardCredit card (Luhn)
Regex(pattern)Pattern matching
Base64Base64 encoded string
HexString(min, max)Hexadecimal string
HexColorHex color (#FF5733)
MacAddressMAC address
IsoDateTimeISO 8601 datetime
IsoDateISO 8601 date
CountryCodeISO 3166-1 alpha-2 (US)
CurrencyCodeISO 4217 currency (USD)
Latitude-90 to 90 coordinate
Longitude-180 to 180 coordinate
PortNetwork port 1-65535
IbanInternational Bank Account
Base58Base58 (crypto addresses)
HslColorHSL color string
DurationISO 8601 duration
CronExpressionCron schedule expression
Isbn10ISBN-10 with checksum
Isbn13ISBN-13 with checksum
AsciiAlphaString(min, max)ASCII letters only
AsciiPrintableString(min, max)ASCII printable
StrongPasswordStrictBuilt-in strong password

Format Methods ​

zig
// Email
email.domain()          // Domain part
email.localPart()       // Local part
email.isBusinessEmail() // Not free email
email.isFreeEmail()     // Free email provider
email.hasTag()          // Has +tag
email.tag()             // Tag portion or null
email.tld()             // Top-level domain

// Url
url.isHttps()   // HTTPS check
url.protocol()  // "http" or "https"
url.host()      // Host part
url.path()      // Path portion
url.query()     // Query string or null
url.fragment()  // Fragment or null
url.port()      // Port number or null
url.hasQuery()  // Has query string
url.hasFragment() // Has fragment
url.filename()  // Last path segment

// Uuid
uuid.version()  // Version number

// Ipv4/6
ip.isPrivate()  // Private range
ip.isLoopback() // Loopback

// PhoneNumber
phone.hasCountryCode() // Has +

// CreditCard
card.cardType() // visa/mastercard/amex
card.masked()   // Last 4 digits

// Base64
b64.estimatedDecodedLen() // Estimated decoded size

// HexString
hex.isLowercase() // All lowercase
hex.isUppercase() // All uppercase

// HexColor
color.getHex()   // Get without #
color.hasHash()  // Has # prefix

// IsoDateTime
dt.getDatePart()  // YYYY-MM-DD
dt.getTimePart()  // HH:MM:SS
dt.hasTimezone()  // Has timezone
dt.isUtc()        // Is UTC (Z)

// IsoDate
date.getYear()   // Year as u16
date.getMonth()  // Month as u8
date.getDay()    // Day as u8

// Latitude/Longitude
lat.isNorthern()  // >= 0
lat.isSouthern()  // < 0
lng.isEastern()   // >= 0
lng.isWestern()   // < 0

// Port
port.isPrivileged() // < 1024
port.isRegistered() // 1024-49151
port.isDynamic()    // > 49151

// Iban
iban.countryCode()      // 2-letter prefix
iban.normalizedLength() // Length without spaces

// Base58
b58.len() // Length

// Duration
dur.hasTime() // Has time component

// CronExpression
cron.fieldCount() // 5 or 6

// StrongPasswordStrict
pwd.masked() // "********"
pwd.len()    // Length

Collection Types ​

TypeDescription
List(T, min, max)List with length
NonEmptyList(T, max)Non-empty list
FixedList(T, len)Exact size

Collection Methods ​

zig
list.get()      // Get items
list.len()      // Length
list.isEmpty()  // Empty check
list.first()    // First or null
list.last()     // Last or null
list.at(i)      // At index or null
list.contains(x) // Contains item
list.slice(s,e) // Sub-slice
list.sum()      // Sum of items
list.all(fn)    // All match predicate
list.any(fn)    // Any match predicate
list.findIndex(fn) // Index of first match

Special Types ​

TypeDescription
Default(T, value)Default value
DefaultFactory(T, fn)Dynamic default
Custom(T, fn)Custom validator
Transform(T, fn)Transform value
Coerce(From, To)Type conversion
Literal(T, value)Exact match
Partial(T)All fields optional
OneOf(T, values)Allowed values
Range(T, s, e, step)Range with step
Nullable(T)Explicit null
Lazy(T)Lazy evaluation

Special Methods ​

zig
// Default
d.isDefault()        // Is default value
D.getOrDefault(opt)  // Get or default

// DefaultFactory
df.initDefault()     // Initialize with dynamic factory function
DF.getOrDefault(opt) // Get or default from factory

// OneOf
o.isFirst()  // First value
o.isLast()   // Last value

// Nullable
n.isNull()      // Null check
n.unwrapOr(d)   // Get or default

// Transform
t.getOriginal() // Original value

Released under the MIT License.