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