Typed, a runtime type validation library for luau

typed

this is my re-imagination of the already existing library, “t” created by Osyris, borrowed some features from zod, and sprinkled in some typechecking magic

caution: library is still under development; use at your own risk. report any bugs you find if you decide to use it

what is typed

typed is a runtime type validation library; a library that allows you to verify data that came from arbitrary sources. also, as a nice addition, typed is nicely typed with the new type solver. as you build out your schemas, they will, internally, figure out what type to generate alongside your schema that you have defined. you can access them via the included infer type function t.Infer<typeof(schema)>. this also means you would need to have enabled the new type solver in order to have access to the schema inference type, or have schema generate types

example usage

const t = require("path/to/typed")

const schema = t.table({
	hello = t.string(),
	world = t.transform(t.number())(function(v: number)
		return v + 1
	end),
	notHere = t.optional(t.boolean()),
})

const data = {
	hello = "hello",
	world = 41,
}

print(schema.parse(data)) -- { ok = true, value = { hello = "hello", world = 42 } }

quick api overview

these are the following methods that exist under the typed module:

primitive schemas

t.boolean()
t.number()
t.string()
t.table(shape)
t.array()
t.none() or t["nil"]()
t.vector()
t.buffer()
t.callable() or t["function"]()
t.userdata()

utility

t.any()
t.literal(value)
t.optional(a)
t.transform(a)
t.union(a, b)
t.intersect(a, b)

schema type

schema._infer phantom data
schema.validate(value)
schema.parse(value)
schema.unwrapParse(value)
schema.enforce(value: T) - it just enforces a value to adhere to the _infer type without needing you to do :: type or const variable: type

i’ll probably get some better documentation later on, for now you can just inspect the code directly to get an idea on how things work. you could also just ask me here too if needed


you can find the source code in the link below:

if you find any bugs, or would like to suggest any features, please let me know below or make pull requests / issues in the github linked above

this is one of my first ever posts on the community resources topic, hope you enjoy this

3 Likes

0.3.0 released

added a new schema type iterable to allow for { [key]: value } types

quick demo of it:

const t = require("path/to/typed")

const schema = t.iterable(t.string(), t.number()) -- generates the type { [string]: number }

print(schema.validate({ a = 1, b = 2 }).ok) -- true

print(schema.validate({ a = 1, b = "not good" }).ok) -- false; returns a table of issues

i also have updated the appendContext function to append from the left instead of the right. this should in turn make issues that references arrays, iterables, or tables look a lot more readable

for example:

const t = require("path/to/typed")

const schema = t.iterable(t.string(), t.array(t.number())) 

print(schema.validate({ a = {1, 2}, b = {3, true} }))
--[[
{
	["issues"] =  {
		[1] = {
			["context"] = "at index b; at index 2",
			["message"] = "expected number, got boolean",
			["type"] = "invalid_type"
		}
	},
	["ok"] = false
}
]]

ignore the fact i didn’t post any other releases here haha

oh yeah, i’ll probably release a roblox model later for an easier way to import this library into your games

Very nice! Any plans for instance support? that would be great. Not sure how hard that would be to add.

at the moment, i am not planning on adding that, however i’ll probably release a specific roblox version which does include those schemas in the future. tbh its just a matter of just adding more builders to support those types which isnt that hard based on how i designed the library lol

for now, just keeping things simple so the library is usable on and off of roblox

0.6.0 released

  • lots of internal code refactoring
  • optional schema modifier now internally just creates a union between nil schema and your passed schema
  • removed key_missing issue, optional field from schema, and the missing_key checks in the table schema due to the change above
  • array now internally uses iterable schema with number as the key and your inputted schema
  • added new is schema
  • and maybe some other changes i cant think from the top of my head

also i created some sort of documentation for the library, sooo you can go read it here

and the github also releases rbxm files if you prefer to install libraries via that. go find them here: Releases · metamethods/typed · GitHub