Forest Logo
search
package_2

typeguard

By @itzbbbbas

Roblox

Mirrored from Wally

Typeguard

One runtime type checker for Roblox Luau. Each builder returns its spec cast to the Luau type it checks, so typeof(spec) is the static type and a shape is written once. Remote payloads, attribute schemas, content registries and DataStack store templates share it.

Typeguard = "itzbbbbas/typeguard@0.1.0"
local Typeguard = require(ReplicatedStorage.Packages.Typeguard)
local T = Typeguard

local GRAB = T.Struct({
	uid = T.String({ max_length = 64 }),
	at = T.Vector3({ max_magnitude = 4096 }),
	leaf = T.Optional(T.Int({ min = 1 })),
})
type Grab = typeof(GRAB)

local reason = Typeguard.Check(payload, GRAB, "payload")
if reason then
	warn(reason) -- payload.at: magnitude 9000 above max 4096
	return
end
local grab = payload :: Grab

Builders

Every builder takes one options table. default rides along for a template or a schema and is never checked.

BuilderOptionsRejects
T.Numberdefault, min, max, nana non-number, NaN or infinity unless nan = true, a value outside min..max
T.Intdefault, min, maxT.Number plus a fraction
T.Stringdefault, max_length, one_of, patterna non-string, a long string, a value outside one_of, a string.match miss
T.Booldefaulta non-boolean
T.Anydefaultnothing
T.Enummembers, defaulta value not in members, compared with ==
T.Literal(value)anything but value
T.Bigdefaulta value that is not a { m, e } big number
T.Vector3default, max_magnitudea NaN or infinite component, a long vector
T.CFramedefaulta NaN or infinite position
T.Color3, T.EnumItemdefaultthe wrong typeof
T.Functiona non-function
T.Instanceclassa non-Instance, or one that fails IsA(class)
T.Struct(shape)exact, max_keysa missing or failing field, an unknown key unless exact = false, too many keys
T.Dict(value)key, max_count, defaulta failing value, a key of the wrong type, too many entries
T.Array(value)max_count, defaulta hole, a failing element, too many elements
T.Optional(inner)a non-nil value that fails inner
T.OneOf(a, b, ...)a value that fails every shape
T.ServerOnly(inner)what inner rejects; sets server_only for DataStack replication
T.Of(value)a value whose typeof differs, or a table that does not match value exactly

A raw value inside a shape is an implicit T.Of. A raw table is an exact struct, so a plain template checks as itself.

Functions

FunctionReturns
Typeguard.Check(value, spec, path?)nil on pass, else path.key: reason
Typeguard.Ok(value, spec)boolean
Typeguard.Defaults(spec)the default tree; an Optional is nil, a Dict or Array is empty
Typeguard.Is(value)true when a builder made it
Typeguard.Kind(spec)"int", "struct", …, or "raw"
Typeguard.Unwrap(spec)the spec under Optional and Of
Typeguard.Emit(spec, name?)Luau type source, <name> and <name>Paths
Typeguard.BigMathNew, Add, Sub, Mul, Div, Compare, Short, ToString for big numbers

Nothing touches a Roblox service at load, so the package runs under Lune and zune. Roblox datatype checks use typeof, so they need Roblox or a runtime that provides those datatypes.

Develop

zune run tests/lib.spec.luau
stylua --check src tests
selene src
npm run docs:check

Package Details

Install command (Click to copy)


Version

0.1.0

License

MIT

check_circle

Safe for commercial use

Automated license review — not legal advice.