Documentation

Start with a literal.

A litetype schema is either a leaf such as string, or a plain object whose values are schemas. That is the whole model.

Terminal
npm i litetype
TypeScript
import { string, number, type Infer, parse } from 'litetype'

const User = {
  name: string.min(1),
  age: number.min(0),
  'email?': string.email(),
}

type User = Infer<typeof User>
const user = parse(User, input)

Choose a validation verb

JobUse
Return data or throwparse(schema, input)
Return a result unionsafeParse(schema, input)
Narrow an unknown valuecheck(schema, input)
Predicate for a hot loopcompile(schema)

Schemas compile automatically on first use. Call compile only when a hot loop should avoid the cache lookup.

Optional key ≠ undefined value

TypeScript
import { string } from 'litetype'

const A = { 'bio?': string }          // bio may be omitted
const B = { bio: string.undefinable() } // bio is required, value may be undefined

The trailing ? mirrors a TypeScript optional property. It belongs to the key. Nullability belongs to the value.

Object policy is explicit

TypeScript
import { parse, strict, string, strip } from 'litetype'

parse({ name: string }, input)         // keep extra keys, no copy
parse(strip({ name: string }), input)  // remove extra keys
parse(strict({ name: string }), input) // reject extra keys

Forms: map, then check

TypeScript
import { coerce, parse, preprocess, string } from 'litetype'

parse(preprocess(v => v === '' ? undefined : v, string.undefinable()), '')
parse(coerce.number(), '42') // 42