wee-schema
v0.0.4
Published
Miniature schema assertion for typescript (with typeguards)
Downloads
4
Maintainers
Readme
Wee Schema
Miniature schema validator for Typescript (with type guards)
Usage
import { weeschema, WeeSchema } from 'wee-schema'
interface MyType {
foo: string
bar: number | string
}
// === Create the schema
const schema : WeeSchema<MyType> = weeschema<MyType>({
'foo': ['string'],
'bar': ['number', 'string']
})
// === Use the schema
function receive(thing: unknown) {
thing.foo // ❌ generates a TS error
// --- Using a type guard
if (schema.ok(thing)) {
console.log(thing.foo); // ✅ compiles + intellisense
}
schema.assert(thing);
console.log(thing.foo); // ✅ compiles + intellisense
}
Schema methods
ok
Returns true if the object is of the correct typeassert
Throws an error if the object is not of the right typeinspect
Returns a list of error messages (if any)